Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 3 (Matlab) In this problem, you will compute the regression line for two

ID: 2991030 • Letter: P

Question

Problem 3 (Matlab) In this problem, you will compute the regression line for two random variables given a set of n = 1000 (x, y) pairs. (1) Generate the (x, y) data by executing the following code: % Generate random (x,y) data n = 1000; x = 2*rand(n,1)-1; y = -4.5*x+3.5+1.2*randn(n,1); (2) Compute the parameters a and b of the regression line using the formulas in the textbook. (3) Plot the scatter diagram for the (z, y) data. Use ?x? as the marker for the points. On the same plot, draw the regression line. In the title of the figure, display the equation of the regression line. (4) Turn in the Matlab code and the plot from (3). Note: Matlab has several built-in ways to perform curve fitting, such as polyfit(). You cannot use any of them. You have to write your own code to compute a and b and plot the data in (3). The only Matlab functions you are allowed to use (beyond simple math operators) are sum (),plot (), hold, and title ().

Explanation / Answer

clear
clc
n=1e+3;
x=2*rand(n,1)-1;
y=-4.5*x+3.5+1.2*randn(n,1);

x_mean=sum(x)/n;
y_mean=sum(y)/n;

num1=0;
den1=0;
for i=1:n
num1=num1+(x(i)-x_mean)*(y(i)-y_mean);
den1=den1+(x(i)-x_mean)^2;
end
b=num1/den1

a=y_mean-b*x_mean


y_new=a+b*x;

plot(x,y,'*')
hold on
plot(x,y_new,'r')
hold off

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote