www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/RepeatedEigenvaluesOfSymmetricPositiveDefiniteSparseMatrExample.m

    %% Repeated Eigenvalues of Symmetric Positive Definite Sparse 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. 
%%
% Use the |eig| function to compute all 632 eigenvalues and the |eigs|
% function to compute the six largest and smallest magnitude eigenvalues.
A = delsq(numgrid('C',30)); 
d = sort(eig(full(A))); 
dlm = eigs(A); 
dsm = eigs(A,6,'sa');
%%
% Plot the results from |eig| and |eigs| for the six largest and smallest
% magnitude eigenvalues.
subplot(2,1,1) 
plot(dlm,'k+')
hold on 
plot(d(end:-1:end-5),'ks')
hold off 
legend('eigs(A)','eig(full(A))') 
xlim([0.5 6.5])

subplot(2,1,2) 
plot(dsm,'k+')
hold on 
plot(d(1:6),'ks')
hold off 
legend('eigs(A,6,''sa'')','eig(full(A))','Location','SouthEast') 
xlim([0.5 6.5])
%%
% The repeated eigenvalue at 4 must be handled more carefully. The call
% |eigs(A,20,4.0)| to compute 20 eigenvalues near 4.0 tries to find
% eigenvalues of |A - 4.0*I.| This involves divisions of the form
% |1/(lambda - 4.0),| where |lambda| is an estimate of an eigenvalue of
% |A|. As |lambda| gets closer to 4.0, |eigs| fails. You must use a value
% of |sigma| that is near but not equal to 4 to find those eigenvalues.
sigma = 4 - 1e-6;
D = sort(eigs(A,20,sigma));
%%
% Compute the 20 eigenvalues closest to 4 using |eig|, and the 20
% eigenvalues closest to 4 - 1e-6 using |eigs|.
figure(2)
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')