 function [vec, val, res] = arn0 (A,m,v,lam0)
%--------------------------------------------
% function [vec, val, res] = arn0 (A,m,v,lam0)
% m steps of the Arnoldi process:
% A   = sparse matrix
% m   = dimension of Krylov subspace
% v   = initial vector,
% lam0 = seek eigenvalue closest to lam0
% return: 
% vec = approximate eigenvector
% val = approximate eigenvalue
% res = residual norm history
% Note: code is without restart and
% projection done at each step (not in practice)   
%--------------------------------------------
 n = size(A,1);
%-------------------- start 
 ro = norm(v,2)  ; 
 t = 1.0/ ro   ;
 V(:,1) = v*t; 
%% 
 for i=1:m  
   i1 = i + 1  ; 
   V(1:n,i1) = A*V(1:n,i)  ;
%-------------------- Arnoldi loop(modified GS)
   for j=1:i 
     t = V(:,j)'*V(:,i1)  ;
     H(j,i) = t  ;
     V(:,i1) = V(:,i1) - t*V(:,j)  ; 
   end  
   t = norm(V(:,i1),2)  ;
   H(i1,i) = t  ;
   if (t  ~= 0.0)  
     t = 1.0 / t  ;
     V(:,i1) = V(:,i1)*t  ;  
   end  
%-------------------- define Arnoldi Objects 
   Vm = V(:,1:i);
   Hm = H(1:i,1:i);
%-------------------- get desired eigenvector  
   [X,vals] = eig(Hm);
   [~,k]  = min(abs(lam0 - diag(vals)));
   res(i) = abs(X(i,k)*H(i+1,i));
 end
 val = vals(k) ;
 vec = Vm*X(:,k);

