power method eigenvalue and eigenvector in matlab

In MATLAB, you can use the eig() function to compute the eigenvalues and eigenvectors of a matrix. Here's how to calculate them using the power method:

main.m
% define matrix A
A = [1 2; 3 4];

% set initial guess for eigenvector x
x0 = [1; 1];

% set tolerance level
tolerance = 1e-10;

% run power method
while true
    % calculate y = A*x
    y = A*x0;
    
    % calculate eigenvalue lambda
    lambda = max(abs(y));
    
    % normalize y to get eigenvector x
    x = y / lambda;
    
    % check for convergence
    if norm(x - x0) < tolerance
        break;
    end
    
    % update initial guess for next iteration
    x0 = x;
end

% display results
disp('Eigenvalue:');
disp(lambda);
disp('Eigenvector:');
disp(x);
566 chars
35 lines

This will output the largest eigenvalue and its corresponding eigenvector for the matrix A. Note that the power method only finds the dominant eigenvalue and eigenvector of a matrix, i.e. the ones with the largest absolute value. If you need to find all eigenvalues and eigenvectors, you can use the eig() function directly.

related categories

gistlibby LogSnag