function [b0,b1]=Line2(X) %This function returns the line ax+by+c=0 that minimizes the error. % This is output as the line y=b0 x + b1, since that is standard form. % Therefore, b0 = -a/b and b1 = - c/b %obtained by orthogonally projecting the data to the line. x=X(:,1); y=X(:,2); clear X n=length(x); barx=mean(x); bary=mean(y); xh=x-barx; yh=y-bary; C=sum(xh.*yh); B=sum(xh.^2-yh.^2); A=-C; Disc=B^2-4*A*C; if Disc<0 error('Something is wrong- We should not get complex slopes\n'); end b0=-(-B+sqrt(Disc))/(2*A); b1=-b0*barx+bary;