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