function [ a,b ] = lsline( data )
% least squares fitted line
%give data as data=[x1,y1;x2,y2;....;xn,yn]

%---------
%Example
% data=[6	2.7
% 5	2.9
% 4	3.0
% 3	3.3
% 2	3.4
% 1	3.5
% ]
% >>[a,b]=lsline(data)
%a =
%    3.7133
%b =
%   -0.1657
%---------

s=size(data);
if s(2)~=2
    a=NaN;
    b=NaN;
    disp('input error')
    return
end

n=s(1);

X=data(:,1);
Y=data(:,2);
scatter(X,Y);


b=(sum(X.*Y)-sum(X)*sum(Y)/n)/(sum(X.^2)-(sum(X))^2/n);
a=sum(Y)/n-b*sum(X)/n;

refline(b,a)

end

