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-')


legend('data','linear')