%% Perturbation Effect on Eigenvalues of a Matrix
% Create the test matrix gallery(3). The test matrix is ill conditioned with eigenvalues that are sensitive to perturbations.

A = gallery(3)

% Compute the eigenvalues of A by using eig.

e = eig(A)

% Compute the eigenvalue condition numbers by using condeig.

c = condeig(A)

% The condition numbers indicate that perturbations in the matrix elements of A can result in perturbations in its eigenvalues with upper bounds that are about 200 to 600 times larger.
% Next, make a small perturbation to A by adding a matrix of uniformly distributed random numbers. Set the seed of the random number generator to its default value. Add a random matrix with elements in the interval from 0 to 0.001, exclusive, to A.

rng('default')
Ap = A + 1e-3*rand(3)

% Compute the eigenvalues of the perturbed matrix Ap.

ep = eig(Ap)

% Show the difference between the perturbed and the original eigenvalues.

delta = ep - e

% Compare the change in the eigenvalues with the upper bounds provided by the eigenvalue condition numbers. The upper bounds have roughly the same order of magnitude as the eigenvalue perturbations.

delta_upper = 1e-3*c

%
% Copyright 2019 The MathWorks, Inc.