
function p=linearregression(x,y)
%Solveslinear regression problem
%INPUT
%x - nodes in a vector of size n+1
%y - a vector of n+1 values 
%OUTPUT 
%p - coefficients of the linear  polynomial in [x^(n),..,x,1] basis
%   minimizing \sum_k |p(2)x(k)+p(1)-y(k)|^2 
%EXAMPLE
%x=1:4;y=[1,-1,2,4]';p=linearregression(x,y);plot(s=linspace(1,4,300),polyval(p,s),x,y,"*")
%x=[0 1];y=[1,-2];p=linearregression(x,y);plot(s=linspace(1,4,300),polyval(p,s),x,y,"*")
  if(rows(y)!=length(x)) 
   warning("no of rows of y <> no of nodes in x");
   y=y'; %exit the function
  end
 
  if(rows(x)==1) x=x';end
  
  A=[x,ones(size(x))];
  p=A\y;   
endfunction
