function evals=QRalg(A,maxiter) % This program implements the QR Algorithm for finding eigenvalues [m n]=size(A); if m~=n error('A must be square') end figure(1) clf spy(A) %imagesc(A) %colorbar title('Original matrix A') fprintf('\nHit any key to continue\n') pause if ~exist('maxiter','var') maxiter=10*n; end tic % For problem 7, uncomment out this next line %A=hess(A); for i=1:maxiter [Q R]=qr(A); A=R*Q; % Note that this means that Anew = Q'QRQ=Q'AQ if ~mod(i,n) | i==maxiter %imagesc(A) %colorbar spy(abs(A)>sqrt(eps)) title(sprintf('Check for convergence at iter=%d: Is this matrix either upper-triangular or diagonal?',i)) fprintf('\nCheck for convergence in figure(1); Hit any key to continue\n') pause end end evals=diag(A); toc