linear_regression2

# linear_regression2 - fprintf('x vs y\n\nBest fit equation

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

function linear_regression2(x,y) %Problem 13.5 srold=0; n=numel(x); maxs=max(x); sumx=sum(x); sumy=sum(y); ymean=sumy/n; xmean=sumx/n; sx2=sum(x.*x); xy=sum(x.*y); sy2=sum(y.*y); a1=(n*xy-sumx*sumy)/(n*sx2-sumx^2); ao=ymean-a1*xmean; %correlation coefficient r=(n*xy-sumx*sumy)/(sqrt(n*sx2-sumx^2)*sqrt(n*sy2-sumy^2)); for i=1:n yi=y(i); xi=x(i); Sr=(yi-ao-a1*xi)^2; Sr=Sr+srold; srold=Sr; end %Standard Error of the estimate syx=sqrt(Sr/(n-2)); x3=[0:.1:maxs]'; y2=ao+a1*x3; if x(1)>0 %this is just plotting the x vs y regression line

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: fprintf('x vs y\n\nBest fit equation y=%7.6f+%7.6fx\n\nStandard error %7.6f\n\nCorrelation coefficient %7.6f\n\n',ao,a1,syx,r); plot(y2,x3); AXIS=([0 20 0 12]); hold off else fprintf('y vs x\n\nBest fit equation y=%7.6f+%7.6fx\n\nStandard error %7.6f\n\nCorrelation coefficient %7.6f\n\n',ao,a1,syx,r); AXIS=([0 20 0 12]); plot(x,y,'s',x3,y2) hold on end legend('y','y vs x','x vs y'); xlabel('x'); ylabel('y'); grid...
View Full Document

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

### Page1 / 2

linear_regression2 - fprintf('x vs y\n\nBest fit equation

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

View Full Document
Ask a homework question - tutors are online