function [Q,H]=arnoldi_standard(A,v1,N)
%   An implementation of the standard Arnoldi method
%   without restarting.
% 
%   Example:
%    A=gallery('wathen',10,10);
%    e = ones(length(A),1);
%    A = A+5i*spdiags([e 0*e, e], -1:1, length(A), length(A));
% %   A=A+flipud(20*spdiags(tan((1:length(A))'/length(A)),0,length(A),length(A)));
%    n=length(A);
%    v1=ones(n,1); v1=v1/norm(v1);
%    N=30;
%    [Q,H]=arnoldi_standard(A,v1,N)
% 
%    ee=eig(H(1:N,1:N));
%    [Y,I]=sortrows(-abs(ee)); sortedvect=V(I);
%    ee=ee(I) 
%    ee2=eigs(A,5,'lm');
%    [ee(1:5),ee2]  % should be more or less the same
%   
%
    v1=v1/norm(v1);
    
    H=[];
    Q=v1;

    for j=1:N
        x=A*Q(:,end);

        % orthogonalize
        h=Q'*x;
        qtilde=x-Q*h;

        % Reorthogonalize once 
        g=Q'*qtilde;
        qtilde=qtilde-Q*g;
        h=h+g;
        
        % normalize
        p=norm(qtilde);
        q=qtilde/p;
        
        % expand H and Q (inefficient)
        H=[[H;zeros(1,size(H,2))],   [h;p]];  
        Q=[Q,q]; 
    end
    
    
end

