clear

format long

N=5;
A=HilbertMatrix(N);
e=eig(A)

v=[1 zeros(1,N-1)]';
for k=1:50
    Av=A*v;
    [m,index]=max(abs(Av));
    mu=Av(index);
    v=Av/mu;
end
mu
v1=v/norm(v);
R=(v'*A*v)/(v'*v);
lambda1=R;

% Hotelling's deflation
B=A-lambda1*v1*v1';
v=[1 zeros(1,N-1)]';
for k=1:50
    Bv=B*v;
    [m,index]=max(abs(Bv));
    mu=Bv(index);
    v=Bv/mu;
end
mu
v2=v/norm(v);
R=(v'*A*v)/(v'*v);
lambda2=R;

B=A-lambda1*v1*v1'-lambda2*v2*v2';
v=[1 zeros(1,N-1)]';
for k=1:50
    Bv=B*v;
    [m,index]=max(abs(Bv));
    mu=Bv(index);
    v=Bv/mu;
end
mu
v;
R=(v'*A*v)/(v'*v);
lambda3=R

% B=A-lambda1*eye(N)
% v=[1 zeros(1,N-1)]';
% for k=1:50
%     Bv=B*v;
%     [m,index]=max(abs(Bv));
%     mu=Bv(index);
%     v=Bv/mu;
% end
% mu
% v
% R=(v'*A*v)/(v'*v)
% lambda2=R