# findging eigenvectors with eigenvalues

34 views (last 30 days)
matlabnm on 25 May 2024 at 4:55
Commented: matlabnm on 27 May 2024 at 3:17
clc; clear;
% Given matrix A
A = [3.6574, -1.9723, 0, 0.8581, 3.0865;
0.5177, 1.4469, 0, 1.1376, -3.7955;
-0.7951, 0.1142, 0.7290, -0.5829, -0.2565;
-0.7273, 2.1819, 0, 2.2816, -6.5275;
0.4301, -1.2904, 0, -0.8602, 3.5590];
% Convergence criterion
tol = 1e-5;
% Number of eigenvalues to find
num_eigenvalues = 3;
% Allocate space for eigenvalues and eigenvectors
eigenvalues = zeros(num_eigenvalues, 1);
eigenvectors = zeros(size(A, 1), num_eigenvalues);
% Function to compute eigenvalues and eigenvectors using inverse iteration and deflation
function [lambda, v] = inverse_iteration(A, x0, tol, max_iter)
v = x0 / norm(x0); % Normalize initial vector
I = eye(size(A));
for k = 1:max_iter
w = (A - I) \ v; % Solve linear system
v_new = w / norm(w); % Normalize new vector
lambda = v_new' * A * v_new; % Estimate eigenvalue
if norm(v_new - v, inf) < tol % Check for convergence
v = v_new;
return;
end
v = v_new;
end
end
% Initial guess for eigenvector
x0 = [1; 0; 0; 0; 0];
% Find the three smallest eigenvalues and eigenvectors using inverse iteration and deflation
for i = 1:num_eigenvalues
[lambda, v] = inverse_iteration(A, x0, tol, 1000);
eigenvalues(i) = lambda;
eigenvectors(:, i) = v;
% Deflation step
A = A - lambda * (v * v');
% Restart with a new initial vector
x0 = rand(size(A, 1), 1);
end
% Display results
disp('Eigenvalues:');
disp(eigenvalues);
disp('Eigenvectors:');
disp(eigenvectors);
This question is about finding 3 smallest eigenvalues and corresponding eigenvectors. If I run this code eigenvlaues work well but each of eigenvalue's eigenvector comes out wrong could someone modify my code do it can work well?
1. Use the inverse iteration and deflation to find three smallest (in magnitude) eigenvalues and corresponding eigenvectors of the following matrix. Start with an initial eigenvector of (1, 0, 0, 0, 0)T for the power method, and use ∥x(k+1) − x(k)∥∞ ≤ 10−5 as a stopping criteria for your iteration. Approximate the eigenvalues using the Rayleigh Quotient.

John D'Errico on 25 May 2024 at 14:25
Edited: John D'Errico on 25 May 2024 at 14:50
Most of the time when I see someone worry their eigenvectors are not correct, it is just because of a sign error on the eigenvectors. But in this case, your code is incorrect, and I would suggest you have one other subtle issue. First, I would note that your eigenvalues are not coming out in the correct order. The inverse power method should find the smallest eigenvalue first. And that alone tells me there is an issue in your code.
A = [3.6574, -1.9723, 0, 0.8581, 3.0865;
0.5177, 1.4469, 0, 1.1376, -3.7955;
-0.7951, 0.1142, 0.7290, -0.5829, -0.2565;
-0.7273, 2.1819, 0, 2.2816, -6.5275;
0.4301, -1.2904, 0, -0.8602, 3.5590];
[V,D] = eig(A);
D = diag(D)
D = 5x1
0.7290 6.9999 3.0000 0.8270 0.1180
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Now, suppose you find the smallest eigenvalue of A, and then deflate A?
vec = V(:,5);
lambda = D(5);
Ahat = A - lambda*vec*vec'
Ahat = 5x5
3.6574 -1.9723 0.0000 0.8581 3.0865 0.5177 1.3795 -0.0337 1.1039 -3.8292 -0.7951 0.0805 0.7121 -0.5998 -0.2734 -0.7273 2.1482 -0.0169 2.2647 -6.5444 0.4301 -1.3241 -0.0169 -0.8771 3.5421
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Now, what are the eigenvalues of Ahat, after deflation?
eig(Ahat)
ans = 5x1
6.9999 3.0000 0.0000 0.8270 0.7290
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
What is the SMALLEST eigenvalue of Ahat? ZERO. So now what will the inverse power method find when you apply it to the matrix Ahat? Things will now get confusing. The inverse power method will now "think" the smallest eigenvalue of Ahat is ZERO.
This tells us that we cannot use a simple deflation scheme, shifting things so the smallest eigenvalue is zero.
What else can we do? If we are using the inverse power method, one idea might be to shift that eigenvalue to something large instead.
lambdashift = max(sum(abs(A),2))*1.1;
You should be able to show that lambdashift is indeed larger in magnitude than any possible eigenvalue of A. We can try using that as a shift instead.
Ahat2 = A + (lambdashift - lambda)*vec*vec';
eig(Ahat2)
ans = 5x1
12.8901 6.9999 3.0000 0.7290 0.8270
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
As you should see, this leaves the other small eigenvalues of A unchanged. And now the inverse power method will be able to again find the smallest eigenvalue.
Finally, there is the issue of your code to solve for the smallest eigenvalue/vector, using the inverse power method. That still needs to be fixed...
matlabnm on 27 May 2024 at 3:17
Thank you very much for leaving a good comment.

### Categories

Find more on Linear Algebra in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!