iterative estimation of eigenvalues and eigenvectors by the inverse power method. Your MATLAB/Octave code shouldn’t be hardcoded. ● Calculate the eigenvectors and eigenvalues
34 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
function [lambda, v] = inverse_power_method(A, tol, max_iter)
% This function uses the inverse power method to estimate the smallest
% eigenvalue and corresponding eigenvector of matrix A.
% Inputs:
% A - The input square matrix
% tol - The tolerance for convergence (e.g., 1e-6)
% max_iter - The maximum number of iterations
% Outputs:
% lambda - The estimated smallest eigenvalue
% v - The estimated corresponding eigenvector
n = size(A, 1); % Get the size of the matrix
v = rand(n, 1); % Initialize a random vector
v = v / norm(v); % Normalize the vector
% Perform the inverse power iteration
for k = 1:max_iter
w = (A \ v); % Solve A * w = v
v_new = w / norm(w); % Normalize the vector
% Check for convergence
if norm(v_new - v) < tol
break;
end
v = v_new; % Update the vector
end
% Calculate the corresponding eigenvalue
lambda = v' * A * v;
% Normalize the eigenvector to have unit length
v = v / norm(v);
end
% Define the matrix A
A = [1, -3, 3;
3, -5, 3;
6, -6, 4];
% Set the tolerance and maximum number of iterations
tol = 1e-6;
max_iter = 1000;
% Call the inverse power method function
[lambda, v] = inverse_power_method(A, tol, max_iter);
% Display the results
fprintf('Estimated smallest eigenvalue: %.6f\n', lambda);
fprintf('Estimated corresponding eigenvector:\n');
disp(v);
1 comentario
Respuestas (1)
John D'Errico
el 22 de Mayo de 2024
Editada: John D'Errico
el 22 de Mayo de 2024
Let me guess, you have a problem with this matrix.
Does the matrix have any special properties? What, for example, are the eigenvalues of A?
A = [1, -3, 3;
3, -5, 3;
6, -6, 4];
[V,D] = eig(A)
Do you see the smallest eigenvalue is actually a double one, and it is negative. Negative is not an issue. But the higher multiplicity throws a curve at you. And, while your code does work, returning -2 as the eigenvalue, it did not generate the same eigenvector as one of those from eig?
[lambda, vec] = inverse_power_method(A, 1e-6,10000)
Now you need to read your class notes again. I assume it was in there. Well, maybe not. Or not yet. This may be in the notes from tomorrow. You should have learned that if there is an eigenvalue of multiplicity higher than 1, then the eigenvectors form a subspace. So the eigenvector you will find will generally be some linear combination of the two eigenvectors eig would return. HOWEVER, this matrix is what we would call a "defective" matrix. That may have been a mean thing to do to you as homework, to give you such a matrix, and then have you try to figure out what is wrong. If that is the case, then the eigenvectors of A will not form a complete basis of linearly independent vectors. Do some reading here:
We can do this, and if the product V'*V is not effectively an identity matrix, then there is an issue with A.
V'*V
Ah yes. So A is in fact a defective matrix. But is lambda an eigenvalue? Yes.
[A*vec,lambda*vec]
And we see that vec as found does have the property of being an eigenvector, lambda an eigenvalue. But if you re-run your code, you will get a totally different eigenvector, at random. And neither of them will match the vectors that will be returned from eig. You did start with a random vector, so that is not surprising, if the eigenvector is not unique.
Does this resolve your question? As I said, it may have been a slightly mean trick to throw such a matrix at you, without giving you some background for that class of matrix, and what to expect as a result. Or maybe it is in your notes.
The classic example of a defective matrix is this one:
Adef = [1 1; 0 1];
Clearly the eigenvalues are 1 and 1.
[V,D] = eig(Adef)
You can see the eigenvectors returned from eig are not at all orthogonal.
We get spoiled, and so often expect all matrices to be simple things. For example...
Asimpl = rand(3);Asimpl = Asimpl + Asimpl'
So Asimpl is a nice, well-behaved symmetric matrix with distinct eigenvalues, and your code has no problems...
[V,D] = eig(Asimpl)
[lambda, v] = inverse_power_method(Asimpl,1e-8,1000)
function [lambda, v] = inverse_power_method(A, tol, max_iter)
% This function uses the inverse power method to estimate the smallest
% eigenvalue and corresponding eigenvector of matrix A.
% Inputs:
% A - The input square matrix
% tol - The tolerance for convergence (e.g., 1e-6)
% max_iter - The maximum number of iterations
% Outputs:
% lambda - The estimated smallest eigenvalue
% v - The estimated corresponding eigenvector
n = size(A, 1); % Get the size of the matrix
v = rand(n, 1); % Initialize a random vector
v = v / norm(v); % Normalize the vector
% Perform the inverse power iteration
for k = 1:max_iter
w = (A \ v); % Solve A * w = v
v_new = w / norm(w); % Normalize the vector
% Check for convergence
if norm(v_new - v) < tol
break;
end
v = v_new; % Update the vector
end
% Calculate the corresponding eigenvalue
lambda = v' * A * v;
% Normalize the eigenvector to have unit length
v = v / norm(v);
end
0 comentarios
Ver también
Categorías
Más información sobre Linear Algebra en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!