  function Feb27Lec()
% Compare two methods for solving the full rank least squares problem
% min || Ax - b ||

m = 6;
n = 4;
% Zero residual example meaning that b is in the range of A
for kappa = [10^3 10^4 10^5 10^6 10^7]
   A = gallery('randsvd', [m,n], kappa);
   b = A*ones(n,1);
   % Normal equations...
   G = chol(A'*A,'lower');
   xNormalEqn = G'\(G\(A'*b));
   relerrN = norm(xNormalEqn-ones(4,1))/norm(ones(4,1));
   % QR method...
   [Q,R] = qr(A,0);
   xQR = R\(Q'*b);
   relerrQ = norm(xQR-ones(4,1))/norm(ones(4,1));
   clc
   fprintf('2-norm condition = %10.3e\n\n',kappa)
   fprintf('   Normal Equations           QR\n')
   disp('-----------------------------------------------')
   for i=1:n
       fprintf('%20.15f     %20.15f\n',xNormalEqn(i),xQR(i))
   end
   fprintf('\n\nRel Err = %10.3e       Rel Err = %10.3e\n',relerrN,relerrQ)     
   pause
end
