function [gamma, m, x] = rqiter(A, s, maxiter) % RQITER Raylegh quotient iteration for the closest eigenvalue. % Input: % A square matrix % s value close to targeted eigenvalue (complex scalar) % maxiter maximal number of iterations % Output: % gamma sequence of eigenvalue approximations (vector) % m number of required iterations % x final eigenvector approximation % n = length(A); x = ones(n, 1); gamma = zeros(maxiter, 1); gamma(1) = s; I = eye(n); m = maxiter; for k = 2:maxiter s = gamma(k-1); B = A - s*I; y = B\x; x = y/norm(y); gamma(k) = x'*B*x + s; if (k > 3) && abs(gamma(k) - gamma(k-1)) < 1000*eps m = k; break end end end