function lambda=maxeig(A,maxiter,tol)
% estimate the maximum eigenvalue in magintude using the power method
[m,n] = size(A);
if m~=n,error('MATLAB:argchk:squareMatrixRequired','Matrix is not square');end

x = randn(size(A,1),1);
x = x./norm(x);
lambda_old = Inf;
for i=1:maxiter
    Ax = A*x;
    lambda = x'*Ax;
    
    if abs(lambda_old-lambda)/lambda < tol 
        break;
    end
    
    lambda_old = lambda;
    x = Ax/norm(Ax);
end
