  function [x,nrmHist] = mr(A, b, x, nsteps,tol) 
% function [x,nrmHist] = mr(A, b, x, nsteps,tol) 
% minimal residual method
% A = matrix  
% b = rhs 
% x = initial guess on entry -- app sol on return 
% nsteps = max number of steps
% 
 r = b - A * x;
 nrmHist(1) = norm(r); 
 if (nargin<5), tol = eps;, end
 tol1 = tol*nrmHist(1);
%%
 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 < tol1), break, end   
 end 
          
