% power iteration for the dominant eigenvalue
% K. Ming Leung, 9/16/03

clear all; format short e;
A = [3 1
1 3]; % The given matrix
x = [0;1]; % The initial eigenvector

disp('iteration eigenvector eigenvalue');
for k = 1:9
y = A*x;
% To compute the eigenvalue, choose any scalar function which
% depends on a particular linear combination of the components
% of a vector, f(x1,x2,...,xn) = a1*x1+a2*x2+...+an*xn, for
% some chice of constants, a1, a2, ..., an. Then an estimation for
% the eigenvalue r is given by
% r = f(y1,y2,...,yn)/f(x1,x2,...,xn).
% For the example here, the following will work nicely & simply.
r = y(2)/x(2);
x = y;
disp([k x.' r]); % display iteration count, eigenvector & eigenvalue
end