function fitscripts
clear all
tdata=1:5;
ydata=[8.38 9.82 10.33 12.14 13.25];
coefs=polyfit(tdata, ydata, 1);
t=1:0.1:5;
y=polyval(coefs,t);
plot(t,y,tdata,ydata,'o')
disp('press any key to continue')
pause

x=[1; 2; 3; 4; 5];
y=[0.9; 7.0; 28.3; 62.1; 122.4];
numpts=max(size(x));
zin(1)=1; %guess for first parameter
zin(2)=3; %guess for second parameter
zout=fminsearch(@(z) sumoferrs(z,x,y), zin);
xplot=x(1):(x(end)-x(1))/(10*numpts):x(end);
yplot=curve(xplot,zout);
plot(x,y,'+',xplot,yplot)
% The following lines attempt to assess the quality of the fit
datamean=mean(y);
errorsum=0;
othersum=0;
for  i=1:numpts
    errorsum=errorsum+(curve(x(i),zout)-y(i))^2;
    othersum=othersum+(datamean-y(i))^2;
end
rsquared=1-errorsum/othersum;
fprintf('The r^2 value for this fit is %f\n',rsquared)

function f=curve(x,z)
a=z(1);
n=z(2);
f=a*x.^n; 

function f=sumoferrs(z, x, y)
f=sum((curve(x,z)-y).^2);

