%% Sigma Value Near Eigenvalue
% Compute eigenvalues near a numeric sigma value that is nearly equal to an eigenvalue.
% The matrix A = delsq(numgrid('C',30)) is a symmetric positive definite matrix of size 632 with eigenvalues reasonably well-distributed in the interval (0 8), but with 18 eigenvalues repeated at 4.0. To calculate some eigenvalues near 4.0, it is reasonable to try the function call eigs(A,20,4.0). However, this call computes the largest eigenvalues of the inverse of A - 4.0*I, where I is an identity matrix. Because 4.0 is an eigenvalue of A, this matrix is singular and therefore does not have an inverse. eigs fails and produces an error message. The numeric value of sigma cannot be exactly equal to an eigenvalue. Instead, you must use a value of sigma that is near but not equal to 4.0 to find those eigenvalues.
% Compute all of the eigenvalues using eig, and the 20 eigenvalues closest to 4 - 1e-6 using eigs to compare results. Plot the eigenvalues calculated with each method.

A = delsq(numgrid('C',30)); 
sigma = 4 - 1e-6;
d = eig(A);
D = sort(eigs(A,20,sigma));

%
%

plot(d(307:326),'ks')
hold on
plot(D,'k+')
hold off
legend('eig(A)','eigs(A,20,sigma)') 
title('18 Repeated Eigenvalues of A')

% Copyright 2012 The MathWorks, Inc.