function [m,b,rsq]=linreg(x,y)
%function [m,b,rsq]=linreg(x,y)
%
%y=mx+b
%rsq=regression coef.
%
denan([x(:),y(:)]);
x=ans(:,1);
y=ans(:,2);
a(1,1)=length(y);
a(2,1)=sum(x);
a(1,2)=a(2,1);
a(2,2)=sum(x.*x);
bb(1)=sum(y);
bb(2)=sum(x.*y);
c=a\bb';
m=c(2);
b=c(1);
bbsq=bb(1)*bb(1)/a(1,1);
rsq=(b*bb(1)+m*bb(2)-bbsq )/(sum(y.*y)-bbsq);
ynew=m*x+b;
plot(x,y,'x',x,ynew)
title(['m='num2str(m),'  b=',num2str(b),'  r**2=',num2str(rsq)])
pause
