function [x,flg] = PowerMethod(A,x0,Iter,tol)
%
% This code performs a power iteration
% Written by Ming Gu for Math 128B, Spring 2009
%
% flg = 1 indicates failure to converge within
% Iter iterations.
%
x = x0/norm(x0);
flg = 1;
for k = 1:Iter
    x = A * x;
    x = x/norm(x);
    lambda = x'*(A*x);
    if (norm(A*x-lambda*x)<=tol)
       flg = 0;
    end
end
