function [ p, np ] = predator_prey_period_ode45 ( )

%*****************************************************************************80
%
%% predator_prey_period_ode45() calls ode45() for period of predator_prey_ode().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    15 December 2021
%
%  Author:
%
%    John Burkardt
%
%  Output:
%
%    real p: an estimate of the period.
%
%    integer np: the number of full periods that were observed.
%

%
%  Get parameter values.
%
  [ alpha, beta, gamma, delta, t0, y0, tstop ] = predator_prey_parameters ( );
%
%  Solve the ODE.
%
  f = @ predator_prey_deriv;
  tspan = [ t0, tstop ];
  options = odeset ( 'Events', @ predator_prey_event );
  [ t, y, te, ye, ie ] = ode45 ( f, tspan, y0, options );
%
%  Count the number of events.
%
  np = length ( te );
  if ( np < 3 )
    p = NaN;
  else
    p = ( te(np) - te(np-2) );
  end

  np = floor ( np / 2 );

  return
end
function [ value, isterminal, direction ] = predator_prey_event ( t, y )

%*****************************************************************************80
%
%% predator_prey_event() watches for repetition of the initial value.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 December 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real t: the current time.
%
%    real y(2): the current solution.
%
%  Output:
%
%    real value: defines an event when value = 0.
%
%    logical isterminal: is true if the event should cause
%    the calculation to halt.
%
%    logical direction: is 0 if all zeroes are to be located,
%    +1 or -1 if events only occur when value is increasing or decreasing.
%
  [ alpha, beta, gamma, delta, t0, y0, tstop ] = predator_prey_parameters ( );

  value = y(1) - y0(1);
  isterminal = false;
  direction = 0;

  return
end

