# Matlab

I need support with this MathLab question so I can learn better.

You should define x as a vector recording the data for the GDP and y as a vector recording the life satisfaction index. You can save your data into a ‘mat’ file by the MATLAB function save(’data.mat’,’x’,’y’). Next time when you retrieve the data, use the MATLAB function load(’data.mat’). Perform the following tasks:

1

1. (2pt) Find the (sample) means of the GDP and the LSI and store them as mX and mY .

• (1pt) You may wish to try the MATLAB functions mean(x) and mean(y).

• (1pt) 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 X N 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)

2. (2pt) Find the (sample) standard deviation of the GDP and the LSI and store them as stdX and stdY .

• (1pt) You may wish to try the MATLAB functions std(x) and std(y).

• (1pt) 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 = vuut 1 N − 1 X N 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).

3. (2pt) Determine the median of the GDPs and store it as medianX

• (1pt) You may wish to try the MATLAB function median(x).

• (1pt) 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).

4. (1pt) Illustrate the data in the table using a scatter plot with the horizontal axis representing the GDP and the vertical axis representing the LSI. From the plot, determine whether the GDP and the LSI 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.

5. (1pt) Find the sample covariance of the GDP and the LSI and store it as covXY . Suggestion: You may wish to try the MATLAB command N/(N − 1) ∗ mean((x − mx). ∗ (y − my)).

6. (2pt) Find the correlation coefficient from the sample covariance and the standard deviations of the GDP and the LSI. Print the obtained correlation coefficient, and determine if the calculation confirms with your observation from the scatter plot.