x=[1 3 5 7 10 12 13 16 18 20]';
y=[4 5 6 5 8 7 6 9 12 11]';
figure
plot(x,y,'+')
ymean=sum(y)/length(y);
st=sum((y-ymean).^2);

xx=0:.25:20;

%straight line fit

a=[ sum(x.^2) sum(x)
   sum(x) length(x)];
b=[sum(x.*y); sum(y)];


aa=a\b;

yy=aa(1)*xx+aa(2);
yi=aa(1)*x+aa(2);
err1=sum((yi-y).^2)
r1=sqrt((st-err1)/st)

hold on
plot(xx,yy,'r-')

%quadratic curve fit

a2=[sum(x.^4) sum(x.^3) sum(x.^2)
   sum(x.^3) sum(x.^2) sum(x)
   sum(x.^2) sum(x) length(x)];
b2=[sum(x.^2.*y) ; sum(x.*y) ; sum(y)];

aa2=a2\b2;

yy2=aa2(1)*xx.^2 + aa2(2)*xx +aa2(3);
y2i=aa2(1)*x.^2 + aa2(2)*x +aa2(3);
err2=sum((y2i-y).^2)
r2=sqrt((st-err2)/st)
hold on
plot(xx,yy2,'g-')

%exponential curve fit

a3=a;
b3=[sum(x.*log(y)) ; sum(log(y))];

aa3=a3\b3;

a3b=aa3(1);  a3a=exp(aa3(2));

yy3=a3a*exp(a3b*xx);
y3i=a3a*exp(a3b*x);
err3=sum((y3i-y).^2)
r3=sqrt((st-err3)/st)
hold on

plot(xx,yy3,'y-')


%reciprocal linear least square

a4=a;b4=[sum(x./y); sum(1./y)];
aa4=a4\b4;

yy4=1./(aa4(1)*xx+aa4(2));

y4i=1./(aa4(1)*x+aa4(2));
err4=sum((y4i-y).^2)
r4=sqrt((st-err4)/st)
hold on
yrcp4=1./yy4;
plot(xx,yy4,'b')
legend('data','linear','quadratic','exponential','reciprocal linear')