function sddemo1(lambda)
%SDDEMO Graphical demonstration of steepest descent iteration
%
%  Shows the performance of steepest descents on a quadratic
%  minimization problem in 2 variables: minimize x'*A*x/2-x'*b,
%  where A is positive definite.  Of course the solution is A\b.
%
%  The eigenvalues of A are 1 and lambda, the input parameter,
%  which defaults to 2.
%
%  example: call as sddemo1, sddemo1(5), sddemo1(10)
% Douglas N. Arnold, 2009-10-01

if nargin == 0
  lambda = 2;
end

clf
ncontours = 60;

% Build a 2x2 matrix with eigenvectors parallel to and orthogonal
% to e1, and eigenvalues 1 and lambda
e1 = [.5;1];
e1 = e1/norm(e1);
e2 = [ -e1(2);e1(1)];
A = e1*e1'+lambda*e2*e2';

% xstar is the exact solution, x the initial guess
xstar = [2;2.5];
b = A*xstar;
x = [0;0];
xold = x;

% set convenient axes
xxmin = -1;
xxmax =  3;
yymin = -1;
yymax =  3;
axis([xxmin,xxmax,yymin,yymax])
axis square
hold on

% plot a contour plot of F(x)=x'*A*x/2-x'*b
[xx,yy] = meshgrid(linspace(xxmin,xxmax,100),linspace(yymin,yymax,100));
zz= (A(1,1)*xx.^2+2*A(1,2)*xx.*yy+A(2,2)*yy.^2)/2-b(1)*xx-b(2)*yy;
cmin=-xstar'*b;
cmax=max([[-1,-1]*A*[-1;-1]/2-[-1,-1]*b,...
          [-1,3]*A*[-1;3]/2-[-1,3]*b,...
          [3,-1]*A*[3;-1]/2-[3,-1]*b,...
          [3,3]*A*[3;3]/2-[3,3]*b]);
contourf(xx,yy,zz,linspace(cmin,cmax,ncontours))

% plot the exact solution at the initial iterate
p=plot(xstar(1),xstar(2));
set(p,'marker','diamond','markersize',5,'markerfacecolor','white','markeredgecolor','none')
p=plot(x(1),x(2));
set(p,'marker','o','markersize',10,'markerfacecolor',[.75,.25,.25],'markeredgecolor','none')
p=plot(x(1),x(2));
set(p,'marker','o','markersize',10,'markerfacecolor',[.75,.25,.25],'markeredgecolor','none')
pause

% plot the steepest descent iteration
r = b-A*x;
for i = 1:20
  p = A*r;
  l = r'*r/(r'*p);
  x = x + l*r;
  r = r - l*p;
  q=plot([xold(1),x(1)],[xold(2),x(2)]);
  set(q,'linewidth',2,'color',[.75,.25,.25],'marker','o','markersize',10,'markerfacecolor',[.75,.25,.25],'markeredgecolor','none')
  xold = x;
  pause
end
