Lecture+30-Linear+_+quadratic+regression

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

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

View Full Document Right Arrow Icon
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 - = - = - - =
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
Modified MATLAB M-File
Image of page 3

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

View Full Document Right Arrow Icon
» 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
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern