13.11. Least Square Regression Homework

13.11.1. Part 1 - Linear Regression

Enter the following commands into the MATLAB Command Window for an example of solving a linear regression problem using linear algebra.

>> t = linspace(0,20);
>> y = 10 - 0.75.*t + 5*randn(1,100);
>> scatter(t,y)
>> A = ones(100,2);
>> A(:,2) = t';
>> x_hat = (A'*A)\(A'*y');
>> p = A*x_hat;
>> hold on
>> plot(t,p)
>> title('Linear Regression')
>> hold off
>> x_hat

Save the plot as a ‘png’ file to submit on Canvas. In the comment area of Canvas, indicate the values of the x_hat vector.

13.11.2. Part 2 - Quadratic Regression

Enter the following commands into the MATLAB Command Window for an example of solving a quadratic regression problem using MATLAB’s polyfit and polyval functions.

>> t = linspace(-10,10);
>> y = 0.2*t.^2 - t + 5 + 3*randn(1,100);
>> scatter(t,y)
>> coef = polyfit(t, y, 2);
>> p = polyval(coef, t);
>> hold on
>> plot(t,p)
>> title('Quadratic Regression')
>> hold off
>> coef

Save the plot as a ‘png’ file to submit on Canvas. In the comment area of Canvas, indicate the values of the coefficients returned from polyfit.