function ShowEvecCond()

% Let's construct a symmetric matrix with some nearly repeated
% eigenvalues...
d = [ 7  5.0000000001  5  4  2.0000001  2  1]';
D = diag(d);
[Q,R] = qr(randn(7,7));
A = Q*D*Q';
%
% Note that Q'*A*Q is the schur decomposition of A

% Now lets perturb A with a small symmetric matrix E with norm O(tol)
tol = .000000000001;
E = randn(7,7); E = tol*(E + E')/2;
A1 = A + E;
% Compute A1's Schur decomp
[Q1,D1] = eig(A1);
[d1,idx] = sort(diag(D1),'descend');
Q1 = Q1(:,idx);
clc
fprintf('Perturb A by O(%8.1e)\n\n',tol)
fprintf('Look at how eigenvalue lambda(k) and eigenvector Q(:,k) change.\n\n')
fprintf('k      lambda(k)        separation from   sine of the angle between Q(:,k)\n')
fprintf('    moves this much      other eigs          and its perturbation\n')
fprintf('-------------------------------------------------------------------------\n')

for k=1:7
    eigDiff = abs(d(k)-d1(k));
    vecDiff = sqrt(1 - (Q(:,k)'*Q1(:,k))^2);
    sep = min(abs(d(k)- d([1:k-1 k+1:7])));
    fprintf('%2d    %10.3e          %10.3e              %10.3e\n',k,eigDiff,sep,vecDiff)
end

