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

Lab Exercise 3: Linear Regression Create a function using MATLAB called \"Linear

ID: 3731033 • Letter: L

Question

Lab Exercise 3: Linear Regression Create a function using MATLAB called "LinearRegression" to perform linear least-squares regression using the following two models 1. Linear model: y-+ajx 2. Exponential model: y = 1e1x (using linearization): In(y)-In () + 1x For the exponential model, y values are replaced with ln(y) in calculating the constants ao and ai, and the constants ,-ead and ,-al The function should take in values of x and y, and solve for the coefficients for both models (ao ai 1 ) and the coefficient of determination (r2) for the linear model only. Use the fprintf0 command to display the uncertainty. For example >> 78.1% of the original uncertainty has been explained by the linear model Use the subplot command to plot each fit, one above the other, on a linear scale. Use a solid line for each model and markers with no line to represent the raw data points for each plot. Label the x and y axes Submit the following through Moodle Your MATLAB (*.m) file of your function A screenshot of your function running. In the shot, please try to capture the command window, workspace showing the inputs, and plots. 1. 2.

Explanation / Answer

function LinearRegression(x,y)
Y=log(y);
a=x-mean(x);
b=y-mean(y);
c=sum(a.*b);
d=sum(a.*a);
a1=c/d;
a0=mean(y)-a1*mean(x);
yf=a1*x+a0;
SSR=sum((yf-mean(y)).^2);
SSTO=sum((y-mean(y)).^2);
r2=SSR/SSTO*100;
fprintf('%.2f of the original uncertainty has been explained by the linear model ',r2)
a=x-mean(x);
b=Y-mean(Y);
c=sum(a.*b);
d=sum(a.*a);
beta1=c/d;
lnalpha1=mean(Y)-beta1*mean(x);
alpha1=exp(lnalpha1);
ye=alpha1*exp(beta1*x);
subplot(2,1,1)
plot(x,yf,x,y,'*')
subplot(2,1,2)
plot(x,y,x,ye,'o')
end

%provide data to show screenshots