function ShowSD(A1,P1,phi1,A2,P2,phi2)
%
%  Pre:  (A1,P1,phi1) and (A2,P2,phi2) are orbits.
%
%  Post: Applies the method of steepest descent with 
%        with line searches to the sep function.
%
%  Usage: ShowSD(10,2,pi/8,4,1,-pi/7)

   close all
   plist = [A1 P1 phi1 A2 P2 phi2];
  
   Lmax = 1;           % Max line step 
   nSteps   = 25;      % Total number of steps.
   con_size = 20;      % Size of the contour plot
  
  
   % Plot the two orbits.
  
   figure
   t=linspace(0,2*pi);
   Orbit(t,A1,P1,phi1,'-');
   hold on
   Orbit(t,A2,P2,phi2,'--');
   hold off

   % Display contours of the sep function.
  
   tRange = linspace(0,2*pi,con_size);
   Z = zeros(con_size,con_size);
   for i=1:con_size
      for j=1:con_size
	     t = [tRange(j);tRange(i)];
	     Z(i,j) = Sep(t,plist);
      end
   end
   figure
   C = contour(tRange,tRange,Z,10);
   title('Enter Contour Labels (Optional). To quit, strike <return>.')
   xlabel('t1')
   ylabel('t2')
   Clabel(C,'manual') 
  
   % Get ready for the iteration.
  
   title('Enter Starting Point for Steepest Descent')
   tc = zeros(2,1);
   [tc(1),tc(2)] = ginput(1);
   fc = Sep(tc,plist);
   gc = gSep(tc,plist);
   tvals = tc;
   fvals = fc;
   gvals = norm(gc);
   title('')
  
   % Steepest Descent with line search:
  
   clc
   
   disp('Step       sep            t(1)         t(2)       norm(grad)')
   disp('----------------------------------------------------------------')
   disp(sprintf('%2.0f     %10.6f     %10.6f   %10.6f      %8.3e ',0,fc,tc,norm(gc)))
   figure(3)
   
   for step = 1:nSteps
      if step<=2  
	     % Manual Line Search
         [tnew,fnew,gnew] = SDStep('Sep','gSep',tc,fc,gc,plist,Lmax,0);
	  else
	     % Automated Line Search
	     [tnew,fnew,gnew] = SDStep('Sep','gSep',tc,fc,gc,plist,Lmax,1);
      end
	  tvals = [tvals tnew];       tc = tnew;
	  fvals = [fvals fnew];       fc = fnew;
	  gvals = [gvals norm(gnew)]; gc = gnew;
	  disp(sprintf('%2.0f     %10.6f     %10.6f   %10.6f      %8.3e ',step,fc,tc,norm(gc)))
   end
  
   % Show solution on orbit plot:
  
    
   figure(1)
   hold on
   last = nSteps+1; 
   Orb1 = Orbit(tvals(1,last),A1,P1,phi1,'*');
   Orb2 = Orbit(tvals(2,last),A2,P2,phi2,'*');
   plot([Orb1(1) Orb2(1)],[Orb1(2) Orb2(2)])
   title(sprintf('min Sep = %8.4f',fvals(last)))
   hold off
  
   % Show the descent path on the contour plot:
  
   figure(2)
   hold on
   plot(tvals(1,:),tvals(2,:),tvals(1,1),tvals(2,1),'o')
   hold off
   title(sprintf('tmin = (%8.3f,%8.3f)   norm(gmin)= %8.4e',tvals(1,last),tvals(2,last),gvals(last)))
  
 
   % Plot the descent of the sep and its gradient:
  
   figure(4)
   subplot(2,1,1)
   plot(fvals)
   title('Value of Sep')
   xlabel('Iteration')
   subplot(2,1,2)
   semilogy(gvals)
   title('Value of norm(gSep).')
   xlabel('Iteration')
  
  return
end

