# Lecture+30-Linear+_+quadratic+regression - Linear Least...

This preview shows pages 1–5. Sign up to view the full content.

Linear Least Squares Given ( xi, yi ), i=1, …, n y = a0 + a1x or 2 1 1 1 1 , , , n n xx i x i i i n n xy i i y i i i S x S x let S x y S y = = = = = = = = 0 1 y x xy x xx S n S a S S S a   = 0 1 2 2 , xx y xy x xy x y xx x xx x S S S S nS S S a a nS S nS S - - = = - - ( 29 n x a n y x a y a x x n y x y x n a i 1 i 1 0 2 i 2 i i i i i 1 - = - = - - =

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Linear Least Square 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)/sqrt(n*sx2-sx^2)/sqrt(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 on
Modified MATLAB M-File

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
» x=1:7 x = 1 2 3 4 5 6 7 » y=[0.5 2.5 2.0 4.0 3.5 6.0 5.5] y = 0.5000 2.5000 2.0000 4.0000 3.5000
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern