Lecture+30-Linear+_+quadratic+regression

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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 = = = = = = = = ∑ ∑ ∑ ∑ 1 y x xy x xx S n S a S S S a = 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 2 i 2 i i i i i 1 ∑ ∑ ∑ ∑ ∑ ∑ ∑- =- =-- = 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 » 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 6.0000 5.5000 » s=linear_LS(x,y) a0 = 0.0714 a1 = 0.8393 c err = 2.9911 Syx = 0.7734 r = 0.93180....
View Full Document

This note was uploaded on 11/07/2011 for the course EGM 3344 taught by Professor Raphaelhaftka during the Spring '09 term at University of Florida.

Page1 / 24

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

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online