% Matlab code qrplt.m
% For "Applied Numerical Linear Algebra",  Question 4.15
% Written by James Demmel, Oct 25, 1995
%                Modified, Jun  5, 1997
%
%  Plot the diagonal entries of a matrix undergoing unshifted QR iteration
%
%  Inputs:
%    a = matrix
%    m = number of QR iterations
%
%  Outputs:
%    n curves, one for each diagonal entry of a 
%
function qrplt(a,m,extraplot)

hold off
e=diag(a);
lam = eig(a);
for i=1:m,
   [q,r]=qr(a);
   dd=diag(sign(diag(r)));
   r=dd*r;
   q=q*dd;
   a=r*q; 
   e=[e,diag(a)];
   er(:,i) = diag(a); 
end
lam = er(:,end);
a
for i = 1:m
    erc(:,i) = abs(er(:,i)-lam)./abs(lam);
end
if nargin == 3
    figure
end
plot(e'),grid
title('plot of each diagonal matrix entry during QR iteration')
if nargin == 3
    figure
    semilogy(erc')
end
shg