  function [x,nrmHist] = mr(A, b, x, nsteps,tol) 
% function [x,nrmHist] = mr(A, b, x, nsteps,tol) 
% minimal residual method
% A = matrix  
% x = initial guess on entry -- app sol on return 
% b = rhs 
% nsteps = max number of steps
% 
 r = b - A * x;
 ro = norm(r);
 nrmHist(1) = ro;
 if (nargin<5), tol = eps;, end
 tol_rel = tol*ro;
%%
 for i=1:nsteps
   ar  = A * r; 
   alp = (ar'*r) / ( ar' * ar);
   x   = x + alp * r;
   r   = r - alp* ar;
   ro = norm(r);
   nrmHist(i+1) = ro; 
   if (ro < tol_rel), break, end   
 end 
          
