power method with deflation in matlab

Here is an implementation of the Power Method with Deflation in MATLAB:

main.m
function [eigenvalue, eigenvector] = power_method_with_deflation(A, tol, maxiter)
% A: square matrix
% tol: tolerance
% maxiter: maximum number of iterations

n = size(A,1);
x = ones(n,1)/sqrt(n);
lambda_old = 0;

for k = 1:maxiter
    y = A*x;
    [eigenvalue, ind] = max(abs(y));
    eigenvector = y/eigenvalue;
    if abs(eigenvalue - lambda_old) < tol
        break;
    end
    lambda_old = eigenvalue;
    A(:,ind) = [];
    A(ind,:) = [];
    x = eigenvector;
end

end
476 chars
24 lines

The function takes a square matrix A, a tolerance level tol, and a maximum number of iterations maxiter as inputs. It initializes a starting vector x and old eigenvalue lambda_old. It then iterates to find the biggest eigenvalue and the corresponding eigenvector. After each iteration, it updates the matrix A by removing the row and column corresponding to the eigenvector previously found. This process is repeated until the desired tolerance or maximum number of iterations is reached. Finally, it returns the computed eigenvalue and eigenvector.

Note that this implementation assumes that A has distinct eigenvalues. If A has repeated eigenvalues, then additional modification is required to the code.

gistlibby LogSnag