linear_regression2

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

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

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

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

View Full DocumentRight Arrow Icon
Background image of page 2
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

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 Right Arrow Icon
Ask a homework question - tutors are online