Use MATLAB. Use the function \"linregr\" at page 350 of your textbook to perform
ID: 3867003 • Letter: U
Question
Use MATLAB.
Use the function "linregr" at page 350 of your textbook to perform a linear regression on the following x and y data, where y is a function of x. Report the slope and the intercept of the line. Also report the r^2 of the fit. Submit the m file of "inregr" function as well. An M-file to implement linear regression. function [a, r2] = linregr(x, y) % linregr: linear regression curve fitting % [a, r2] = linregr (x, y): Least squares fit of straight % line to data by solving the normal equations % input: % x = independent variable % y = dependent variable % output: % a = vector of slope, a(1), and intercept, a(2) % r2 = coefficient of determination n = length(x): if length(y) ~= n, error('x and y must be same length'): end x = x(:): y = y(:): % convert to column vectors sx = sum(x): sy = sum(y): sx2 = sum(x.*x): sxy = sum(x.*y): sy2 = sum(y.*y): a(1) = (n*sxy-sx*sy)/(n*sx2-sx^2); a(2) = sy/n-a(1) *sx/n: r2 = ((n*sxy-sx*sy)/ squareroot (n*sx2-sx^2)/ squareroot (n*sy2-sy^2))^2: % create plot of data and best fit line xp = linspace(min(x), max(x), 2): yp = a(1) *xp+a(2): plot (x, y, 'o', xp, yp) grid onExplanation / Answer
Answer for the given Question:
See the below matlab code for the linear regression for the given problem statement.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.