  function Nov4a()
% Use the rabbit-fox simulation to illustrate the use of event functions 

close all

% The population densities for rabbits and foxes at time t=0...
r0 = 300;      
f0 = 150; 
y0 = [r0;f0];
t0 = 0;
tFinal = 20;

% Integrate across the [t0,tFinal] and display...

[tVals,yVals] = ode23(@MyF,[t0 tFinal],y0);
plot(tVals,yVals(:,1),'k','linewidth',2)
hold on
plot(tVals,yVals(:,2),'r','linewidth',2)
legend('Rabbits','Foxes')
hold off



% Integrate across the [t0,tFinal] and highlight all the
% the places where the rabbit and fox densities are the same
options = odeset('events',@g);
[tVals,yVals,tE,yE] = ode23(@MyF,[t0 tFinal],y0,options);
figure
plot(tVals,yVals(:,1),'k','linewidth',2)
hold on
plot(tVals,yVals(:,2),'r','linewidth',2)
axis([0 20 0 500])
legend('Rabbits','Foxes')
plot(tE,yE(:,1),'.c','markersize',25)
hold off



  function yp = MyF(t,y)
% y1 = rabbits, y2 = foxes
alpha = .01;
yp = [2*y(1)-alpha*y(1)*y(2) ; -y(2)+alpha*y(1)*y(2)];



  function [gStop,isterminal,direction] = g(t,y)
% Event function that looks for local mins for the rabbits
yp = MyF(t,y);
gStop = y(1)-y(2);
isterminal = 0;        % Make a note of the event, but keepintegrating
direction = 0;         % 
