lsregres - yy=1/y xx=1/x n=size(xx A=[ones(n xx coeff=A\yy...

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

View Full Document Right Arrow Icon
function lsregres(x,y) %Problem 14.14 %Least-squares regression to fit a straight line %--------------------------- sumx=sum(x); sumy=sum(y); sumxy=sum(x.*y); x2=sum(x.*x); y2=sum(y.*y); n=length(x); meany=sumy/n; meanx=sumx/n; m=(n*sumxy-sumx*sumy)/(n*x2-sumx^2); b=meany-m*meanx; xx=linspace(min(x),max(x),2); ynew=m.*xx+b; plot(x,y,'o',xx,ynew); title('a) Linear Model'); fprintf('\nlinear model y=%5.4fx+%3.1f\n\n',m,b); pause St=sum((meany-y).^2); r1=((sumxy*n-sumx*sumy)/(sqrt(n*x2-sumx^2)*sqrt(n*y2-sumy^2)))^2; %----------------------------- %lsr to fit a power equation xx=log10(x); a=log10(y); n=length(x); c=ones(n,1); CM=[c xx]; %Coeff Matrix ans=CM\a; aa=10^(ans(1)); fprintf('\npower model y=%5.4fexp^(%5.4fx)\n\n',aa,ans(2)); yy=aa.*x.^ans(2); plot(x,y,'s m',x,yy); title('b) Power model'); pause Sr=sum((y-aa.*x.^ans(2)).^2); r2=1-(Sr/St);
Background image of page 1

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

View Full Document Right Arrow Icon
%-------------------------------- %lsr for a saturation growth-rate-equation
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: yy=1./y; xx=1./x; n=size(xx); A=[ones(n) xx]; coeff=A\yy; alpha=1/coeff(1); beta=coeff(2)*alpha; y2=alpha.*(x./(beta+x)); plot(x,y,'o',x,y2); title('b) Saturation-growth rate model'); fprintf('\nSaturation-growth rate model y=%5.4f x/(x+%5.4f)\n\n',alpha,beta); pause Sr=sum((y-alpha.*(x./(beta+x))).^2); r3=1-Sr/St; %---------------------------------%lsr to fit a parabola Z=[ones(n) x x.^2]; a=Z\y; ynew=a(1)+a(2).*x+a(3).*x.^2; plot(x,y,'o',x,ynew); title('c) Parabola'); fprintf('\nParabola model y=%5.4fx^2+%5.4fx+%5.3f\n\n',a(3),a(2),a(1)); a1=a(1); a2=a(2); a3=a(3); Sr=sum((y-a1-a2.*x-a3.*x.^2).^2); r4=1-(Sr/St); fprintf('Coefficient of determination linear=%5.4f power=%5.4f sat-grow=%5.4f parabola=%5.4f\n\n',r1,r2,r3,r4); disp('Clearly the quadratic graph is the best fit');...
View Full Document

{[ snackBarMessage ]}

Page1 / 2

lsregres - yy=1/y xx=1/x n=size(xx A=[ones(n xx coeff=A\yy...

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