function ShowSteepDes()
% Illustrate the method of steepest descent with exact line search as
% applied to the objective function (x'Ax)/2 - x'b

% Generate an exampe with prescribed condition...
n = 1000;
kappa = 1000; 
nSteps = min(10000, ceil(-14/log10(1-1/kappa)));
A = sprandsym(n,.01,1/kappa,1);
b = randn(n,1);
xExact = A\b;
[xStar,errVec] = SD(A,b,nSteps,xExact);
semilogy(errVec)
title(sprintf('cond(A) = %10.3e',kappa),'fontsize',18)
shg


  function [xStar,errVec] = SD(A,b,nSteps,xExact)
% Performs nSteps iterations of steepest descent applied
% to (x'Ax)/2 - x'*b. 
% xExact is the ``exact'' solution and errVec(k) is the absolute
% error of the kth iterate.
% xStar is the final iterate.
n = length(b);
x = zeros(n,1);
errVec = zeros(nSteps,1);
for k=1:nSteps
    g = A*x-b;
    mu = (g'*g)/(g'*A*g);
    x = x- mu*g;
    errVec(k) = norm(x - xExact);
end
xStar = x;
