%
%  gradient flow example -- lect03e.m
%
%- - - - - - - - - - - - - - - - - - - - - - - - 

%  coordinate matrices

u = -4:0.04:4;   up = u;  [u1 u2] = meshgrid(u,up);

% topography

global A;  A = 1/8;

k = (u1.^4 + u2.^4)/(6^4) + A*u1 + exp(-(u1.^2 + u2.^2)).*(u2.^3 - u1.^3);

%  contourplot

clf;  contour(u,up,k,24);  hold on;  colorbar
title('\bf trajectories for a gradient flow');
xlabel('\bf x(t)');  ylabel('\bf y(t)')

while(1)

%  ODE file, IVs, final time, method
%      & options

Xode = 'X3e';  T = 200;  method = 'ode45';  tol = 1e-2;

disp(' ');  disp(' clik to choose initial point'); 
disp(' return to quit');  disp(' ');  

%  input IVs

clear IVs;  IVs = ginput(1);  
if(isempty(IVs))
  break
else
  plot(IVs(1),IVs(2),'kx')
end

options = odeset('reltol',tol*1e-3,'abstol',tol*1e-6,'refine',1, ...
		'maxstep',T/20,'stats','off','events','off');

disp(' ');  disp([method ' for ' Xode]);  disp(' ')

%  ODE solve & plot

[t y] = feval(method,Xode,[0 T],IVs,options);

plot(y(:,1),y(:,2),'k')

end





