function predator_rk4 ( n )

%*****************************************************************************80
%
%% predator_rk4 use RK4 on the predator problem.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    15 September 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N: the number of steps to take.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'predator_rk4' );
  fprintf ( 1, '  Solve predator-prey equation with the rk4 method\n' );
  fprintf ( 1, '  Plot the solution, phase plane, and conservation.\n' );

  if ( nargin < 1 )
    n = 300;
    fprintf ( 1, '\n' );
    fprintf ( 1, '  Input value N not supplied.  Using N = %d\n', n );
  end

  dydt = @predator_deriv;
  tspan = [ 0.0, 50.0 ];
  u0 = 4.0;
  v0 = 2.0;
  y0 = [ u0, v0 ];
  [ t, y ] = rk4 ( dydt, tspan, y0, n );

  figure ( 1 )
  plot ( t, y(:,1), 'g-', t, y(:,2), 'r-', 'linewidth', 3 );
  grid ( 'on' ); 
  xlabel ( '<--T-->' );
  ylabel ( '<--R(T), F(T)-->' );
  s = sprintf ( 'Predator RK4 ODE, n = %d.', n );
  title ( s );
  legend ( 'Rabbits', 'Foxes' );
  filename = sprintf ( 'predator_rk4_%d.png', n );
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  figure ( 2 )
  plot ( y(:,1), y(:,2), 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( '<-- R(T)-->' );
  ylabel ( '<-- F(T)-->' );
  s = sprintf ( 'Predator ODE RK4, n = %d.', n );
  title ( s );
  filename = sprintf ( 'predator_rk4_phase_%d.png', n );
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Conservation.
%
  alpha = 2.0;
  beta = 1.0;
  gamma = 1.0;
  delta = 1.0;

  u = y(:,1);
  v = y(:,2);
  c0 = delta * u0 - gamma * log ( u0 ) + beta * v0 - alpha * log ( v0 );
  c =  delta * u  - gamma * log ( u )  + beta * v  - alpha * log ( v );
  figure ( 3 )
  plot ( t, c, 'r-', ...
         [t(1),t(end)], [c0,c0], 'b--', ...
         [t(1),t(end)], [0,0], 'k-', 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( '<-- T -->' );
  ylabel ( '<-- C(T) -->' );
  s = sprintf ( 'Predator rk4: C(T) should be constant, N = %d.', n );
  title ( s );
  filename = sprintf ( 'predator_rk4_conservation_%d.png', n );
  print ( '-dpng', filename );

  return
end

