%
% This is an example of the Steep Decent method for 
% computing the smallest eigenvalue of a Hermitian matrix
%

load SiNa
load eigSiNa
A=Problem.A;
[m,n]=size(A);
if m~= n
   disp('A is NOT square');
end

x = ones(n,1); %x=randn(n,1); 
nrmx=norm(x); x=(1/nrmx)*x;
lambs=[]; maxitn=200; resd=[];

for i=1:maxitn,
    Ax=A*x; 
    rho=x'*Ax; 
    lambs = [ lambs; rho];
    r=Ax-rho*x; 
    nrmr=norm(r); 
    r=(1/nrmr)*r; 
    resd=[resd; nrmr];
    Ar=A*r; 
    rhos=[rho r'*Ar];
    [y, t]=onestep0(Ax,x,r,rhos);
    x=y; 
    nrmx=norm(x); 
    x=(1/nrmx)*x;
end

err=lambs-eigA(1);
figure(1);
plot1=semilogy(err,'r-'); 
hold on
plot2=semilogy(resd,'g--');
legend([plot1 plot2],'\rho_i-\lambda_1', '||A x_i-\rho_i x_i||');
xlabel('i')
title('Steep Decent'); 
hold off

figure(2)
m=length(err);
plot(err(2:m)./err(1:m-1));  hold on
xi=(eigA(2)-eigA(1))/(eigA(n)-eigA(1));
rate=( (1-xi)/(1+xi) )^2;
plot([1 maxitn],[rate rate],'r--');
title('SD: (\rho_{i+1}-\lambda_1)/(\rho_i-\lambda_1)');
hold off
