function table4_3

%%% power method
%%% note the values calculated will differ sightly from text because of the
%%% random start

%  define A
n=200;
a=3;
A=zeros(n,n);
for i=1:n
    A(i,i)=a;
end
A(1,n)=1;  A(n,1)=1;

% exact value and number of iterations specified
evalue=1+a;
K=35;

%  start-off info
k=0;
vv=100;
y=rand(n,1);

z=A*y;
v=dot(y,z)/dot(y,y);
err=abs(v-evalue);
fprintf('\n k  \t   x_k \t\t\t  Ratio1  \t  Ratio2 \n')
fprintf(' %d \t %15.12f  \n',k,v)
%fprintf('%d & %15.12f    \n',k,v)

dv=1;
dvv=1;
for k=1:K
    vv=v;
    dvv=dv;
    errv=err;
    y=z/norm(z,2);
    z=A*y;
    v=dot(y,z);
    dv=v-vv;
    err=abs(v-evalue);
    if k==1
        fprintf(' %d \t %15.12f  \t %5.2e  \n',k,v,err/errv)
        %fprintf('\\, %d \\, & \\,\\, %15.12f \\,\\, & \\,\\, %5.2e  \\,\\, & \\,\\,  \\\\ \\hline \n',k,v,err/errv)
    else
        fprintf(' %d \t %15.12f  \t %5.2e   \t %5.2e \n',k,v,err/errv,dv/dvv)
        %fprintf('\\, %d \\, & \\,\\, %15.12f \\,\\, & \\,\\, %5.2e  \\,\\, & \\,\\, %5.2e \\\\ \\hline \n',k,v,err/errv,dv/dvv)
    end
end
