%% Rayleigh Quotient Iteration for a Companion Matrix
%
% Newton's example polynomial was $p(z) = z^3 - 2z - 5 = 0$.
%
C = [0 0 1.67608204095197550; 1 0 2; 0 1 -0.66478359180960489;];
x0 = -6 + 5i;
x = @(z) [-C(2,end)+z*(C(3,end)+z); C(3,end)+z; 1];
niters = 19;
xi  = zeros( niters, 1 );
xia = zeros( niters, 1 );
% Now solve at each step for new eigenvector.
xi(1) = x0;
xia(1)= x0;
x1 = x(x0); % Initial eigenvector
xa = x1;
x1 = x1/norm(x1,2);
for i=2:niters,
    x1 = (C-xi(i-1)*eye(3))\x1;
    x1 = x1 / norm(x1,2) ;
    xi(i) = (x1' * C * x1 ); %(x1'*x1) =1
    xia(i) = (xa' * C * xa )/(xa'*xa);
    xa = x(xi(i)); % analytic eigenvector formula
end
ers = xi(:) - xi(end);
close( figure(1) )
figure(1), semilogy( abs(ers), 'ko' ), set(gca,'fontsize',16),hold on
ersa = xia(:)-xi(end);
semilogy( abs( ersa ), 'kS' )
