Overview
1MATLAB for Simple Data Analysis Tasks
The attached Excel file contains a data set on electricity usage across 50 U.S. states in the year of 2022. We want to examine if the electricity charge rate per kWh has correlation with the electricity usage. In practice, there are many other factors that affect the energy usage. We’re only concerned about the electricity charge per kWh here. You will need to load the Excel file (a quick Google search to find how) and define x as a vector recording the data for the RATE per kWh, b as a vector recording the energy consumption, and c as a vector recording the population across the 50 states. You can save your data into a ‘mat’ file by the MATLAB function save(’data.mat’,’x’,’b’,’c’). Next time when you retrieve the data, use the MATLAB function load(’data.mat’). Perform the following tasks: 1. You need to define a vector y as a vector recording the energy consumption in MWh per capital. It can be obtained from b and c. 2. (1pt) Find the (sample) means of the RATE and the USAGE and store them as mX and mY . • You may wish to try the MATLAB functions mean(x) and mean(y). • Write your own function my mean(x) that takes input as a data vector x and returns its mean mX . This function should provide the following result mX = 1 N NX i=1 xi where N is the length of the data vector x and xi is its ith element. Compare the result you get from my mean(x) and my mean(y) with mean(x) and mean(y) 3. (1pt) Find the (sample) standard deviation of the RATE and the USAGE and store them as stdX and stdY . • You may wish to try the MATLAB functions std(x) and std(y). • Write your own function my std(x) that takes input as a data vector x and returns its standard deviation mX . This function should provide the following result stdX = v u u t 1 N ? 1 NX i=1 (xi ? my mean(x))2. You need to use your own function my mean(x) to get the mean of x. Compare the result you get from my std(x) and my std(y) with std(x) and std(y). 4. (1pt) Determine the median of the RATEs and store it as medianX • You may wish to try the MATLAB function median(x). • Write your own function my median(x) that takes input as a data vector x and returns its median medianX . You must sort the data vector x in an ascending order into xa first, and then proceed to find the median (the mid point). You must write a script that is usable for both odd and even number of samples. Print your result and compare it with the result obtained from the MATLAB function median(x). 1
5. (1pt) Illustrate the data in the table using a scatter plot with the horizontal axis representing the RATE and the vertical axis representing the USAGE. From the plot, determine whether the RATE and the USAGE have positive correlation, negative correlation or no correlation. Suggestion: You may wish to try the MATLAB function scatter(x, y). Attach the plot in your report. 6. (1pt) Find the sample covariance of the RATE and the USAGE and store it as covXY . Suggestion: You may wish to try the MATLAB command N/(N ? 1) ? mean((x ? mx). ? (y ? my)). 7. (2pt) Find the correlation coefficient from the sample covariance and the standard deviations of the RATE and the USAGE. Print the obtained correlation coefficient, and determine if the calculation confirms with your observation from the scatter plot. 2 MATLAB for Numerical Simulations In numerous engineering problems, you have to use software tools (like MATLAB) for numerical simulations and verifying your solutions. In this section, you are required to dig in MATLAB for some built-in functions that can be used to generate different type of random variables (try searching MATLAB help). Example: x = randn(1, 100000) would generate a vector x of 100000 samples of a standard normally distributed random variable. If you call hist(x, 50), you will observe the histogram (approximately the PDF) of a normally distributed random variable. You tasks in this section are as follows: • Generate and plot the histogram of a vector of 100, 000 samples of a normally distributed random variable with mean 1 and variance of 4. • Generate and plot the histogram of a vector of 100, 000 samples of an exponentially distributed random variable with parameter ? = 3. • Generate and plot the histogram of a vector of 100, 000 samples of a binomial distributed random variable with parameter p = 0.4 and n = 30. • Generate and plot the histogram of a vector of 100, 000 samples of a continuously uniformly distributed random variable between 10 and 30. • Generate and plot the histogram of a vector of 100, 000 random integers between 10 and 20. Please turn in your MATLAB script in one section (copy and paste) and the results/plots in another section. Attach all in one Word document (or exported to PDF) and upload to the Project submission link on Canvas.