function [r2,m,b] = leastsquaresline(x,y)
  n=min(length(x),length(y));	
  sumxy=0.0;
  sumx2=0.0;
  sumy2=0.0;
  meanx=0.0;
  meany=0.0;
  for i=1:n,
    meanx=meanx+x(i);
    meany=meany+y(i);
    sumxy=sumxy+x(i)*y(i);
    sumx2=sumx2+x(i)^2;
    sumy2=sumy2+y(i)^2;
  end;
  meanx=meanx/n;
  meany=meany/n;

  m = (sumxy - n*meanx*meany) / (sumx2 - n*meanx^2);

  b = meany - m * meanx;

  SST = sum((y-meany).^2);
  SSE = sum((y-(m*x+b)).^2);

  r2= (SST-SSE)/SST;
