function [lambda, x, conv] = myinversepower(A, x, q, tol, maxiter)
%MYINVERSEPOWER Find the eigenvalue of matrix A  that is close to q
%        and corresponding eigenvector using inverse power method
    [n, ~] = size(A);
    x = x/norm(x);
    A = A - q*eye(n);
    xn = A\x;
    ray = x'*xn;
    lambdaold = 1/ray + q;
    x = xn/norm(xn);
    conv = lambdaold;
    iter = 0;
    rel = 2*tol;
    while (iter < maxiter) && (rel > tol)
        xn = A\x;
        ray = x'*xn;
        lambda = 1/ray + q;
        rel = abs((lambda - lambdaold)/lambda);
        lambdaold = lambda;
        conv = [conv lambdaold];
        iter = iter + 1;
        x = xn/norm(xn);
     end
end

