%power method for estimating smallest (in absolute value)
%eigenvalue and eigenvector of a square matrix A

A = [1,2,3;4,5,6;7,8,10];
n = 10;        %number of iterations
x = [1;2;3]; 
u = [1,1,1];

[L,U] = lu(A);   %LU factorization is used here to avoid inverting A

for i=1:n
    z = linsolve(L,x);
    y = linsolve(U,z);
    lambda = (u*y)/(u*x);
    x = y/max(abs(y));
end

disp('estimate of smallest (in magnitude) eigenvalue is...')
1/lambda

disp('corresponding eigenvector is...')
x
