function table4_9

% orthogonal iteration

% number of iterations nd error tolerance
K=8;
tol=1e-5;

% matrix
n=2; A(1,1)=2;  A(1,2)=1;  A(2,1)=1; A(2,2)=2;

%B=rand(n,n);
B(1,1)=-1;  B(1,2)=2;  B(2,1)=3; B(2,2)=-1;

% step 0
[Q,R]=qr(B);
for ic=1:n
    eigens(ic)= dot(Q(:,ic),A*Q(:,ic))/dot(Q(:,ic),Q(:,ic));
end
k=0;
fprintf('\n k  \t   lambda_1 \t   lambda_2 \n')
fprintf(' %d \t %12.8f \t %12.8f \n',k,eigens(1),eigens(2))
%fprintf('%d & %12.8f & %12.8f \n',k,eigens(1),eigens(2))
eigens0=eigens;

for k=1:K
    B=A*Q;
    [Q,R]=qr(B);
    for ic=1:n
        eigens(ic)= dot(Q(:,ic),A*Q(:,ic))/dot(Q(:,ic),Q(:,ic));
    end
    err=norm(eigens-eigens0,inf);
    eigens0=eigens;
    fprintf(' %d \t %12.8f \t %12.8f \n',k,eigens(1),eigens(2))
    %fprintf('%d & %12.8f & %12.8f \n',k,eigens(1),eigens(2))
    if err < tol
        break
    end
end
fprintf('\n')












