
function [r,x,it]=powerr(A, tol=1e-12, x0=rand(length(A(1,:)),1),maxit=200)
#function [r,x,it]=powerr(A, tol, x0,maxit)
#implementation of power method
#Input 
#A matrix 
#x0 -starting vector
#tol - stopping criteria |r_k-r_{k-1}|<tol
#maxit - maximal no of iterations

#output 
#r - Raileigh quotient - approx of eigenvalue
#x- approximation of eigenvector for the largest eigenvalue
#it - no of iterations

  
  xp=x0/norm(x0,2);
  x=A*xp;
  it=1;
  n=norm(x,2);
  r=x'*xp; 
  rp=r+1; 
  while((abs(r-rp)>tol)&&(it<=maxit))
     xp=x/n;
     x=A*xp;
     it++;
     n=norm(x,2);
     rp=r;
     r=x'*xp;
  endwhile
  if(it>maxit)
    printf("\n\nWarning. Maximal no of iterations (%d)  is exceeded\n\n",maxit);
  endif
  x=x/n;

endfunction
