%  Plot eigenvalues of a matrix and eigenvalues of
%  random perturbations of the same matrix. 
%  This illustrates the sensitivity of eigenvalues to perturbations.
%
%  Inputs:
%     a = input matrix
%     err = size of perturbation
%     m = number of perturbed matrices to compute
%
%  Outputs:
%     Three plots where each symbol is the location of an eigenvalue of 
%     a perturbed matrix:
%        'o' marks the location of each unperturbed eigenvalue. 
%        'x' marks the location of each perturbed eigenvalue, where a
%                  real perturbation matrix of norm err is added to a. 
%        '.' marks the location of each perturbed eigenvalue, where a
%                  complex perturbation
%

%  Compute data
%  ea contains eigenvalues of unperturbed matrix a
%  er contains eigenvalues of matrix with real perturbations
%  ei contains eigenvalues of matrix with complex perturbations

ea=conj(eig(a)'); er=ea; ei=ea;
[v,s]=size(a);

for i=1:m, r=rand(v,s)-.5; per=(err/norm(r))*r; er=[er,conj(eig(a+per))']; end
j=sqrt(-1); 
for i=1:m, r=(rand(v,s)-.5)+j*(rand(v,s)-.5); per=(err/norm(r))*r;
  ei=[ei,conj(eig(a+per)')]; end
%
%  Plot data
axis('square'),
hold off,plot(real(er),imag(er),'.r'),hold on,grid,
plot(real(ea),imag(ea),'xw'),title('Real Perturbations'),
disp('pause (hit return to continue)'),pause
hold off,plot(real(ei),imag(ei),'.b'),hold on,grid,
plot(real(ea),imag(ea),'xw'),title('Complex Perturbations'),
disp('pause (hit return to continue)'),pause
hold off,plot(real(ei),imag(ei),'.r'),hold on,grid,
plot(real(er),imag(er),'.b'), plot(real(ea),imag(ea),'xw'),
title('Real and Complex Perturbations')

