function table4_15

% orthogonal iteration with inverse power method

% error tolerance
tol=1e-7;

% number of iterations
K=1000000;

% matrix
a=3;
m=5;
n=200
A=zeros(n,n);
for i=1:n
    A(i,i)=a;
    if i<n
        A(i,i+1)=-1;
    end
    if i>1
        A(i,i-1)=-1;
    end
end
% step zero
B=rand(n,m);
[Q,R]=qr(B,0);
eigens0=ones(1,m);
eigens=ones(1,m);
[L,U] = lu(A);

for k=1:K
    Z = L\Q;
    B = U\Z;
    %B=inv(A)*Q;
    [Q,R]=qr(B,0);
    for ic=1:m
        eigens(ic)= dot(Q(:,ic),A*Q(:,ic));
        errs(ic)=(eigens(ic)-eigens0(ic))/eigens(ic);
    end
    err=norm(errs,inf);
    if err < tol
        break
    end
    eigens0=eigens;
end
k
% exact values
evx=ones(1,m);
evx(1)=a+2*cos(n*pi/(n+1));
evx(2)=a+2*cos((n-1)*pi/(n+1));
evx(3)=a+2*cos((n-2)*pi/(n+1));
evx(4)=a+2*cos((n-3)*pi/(n+1));
evx(5)=a+2*cos((n-4)*pi/(n+1));

% errors
fprintf('\n i  \t lambda_i \t   RIE \t       RE \n')
for im=1:m
    errs2(im)=abs((eigens(im)-evx(im))/evx(im));
    fprintf('%d & %15.12e & %5.2e & %5.2e  \n',n-im+1,eigens(im),errs(im),errs2(im))
end
fprintf('\n')

% format longG
% ee=eig(A);
% ee(1:5)










