function [lambda, x, conv] = myinversepower(A, x, q, tol, maxiter)
% MYINVERSEPOWER find the smallest in absolute value eigenvalue
%                and the corresponding eigenvector of the matrix (A - q*I)
%                using inverse power iterations
   x = x/norm(x);
   [n, ~] = size(A);
   A = A - q*eye(n);
   xn = A\x;
   ray = x'*xn;
   x = xn/norm(xn);
   lambdaold = 1/ray + q;
   conv = [];
   iter = 1;
   abserr = 2*tol;
   while (abserr > tol) && (iter < maxiter)
       xn = A\x;
       ray = x'*xn;
       x = xn/norm(xn);
       lambda = 1/ray + q;
       abserr = abs(lambda - lambdaold);
       lambdaold = lambda;
       conv = [conv abserr];
       iter = iter + 1;
   end
end
