function predator_midpoint ( n )

%*****************************************************************************80
%
%% predator_midpoint computes and plots a solution to the predator problem.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    02 November 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N: the number of steps to take.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'predator_midpoint' );
  fprintf ( 1, '  Solve predator-prey equation with midpoint method\n' );
  fprintf ( 1, '  Plot the solution.\n' );

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

  dydt = @predator_deriv;
  t0 = 0.0;
  tstop = 50.0;
  tspan = [ t0, tstop ];
  y0 = [ 4.0, 2.0 ];
  [ t, y ] = midpoint ( dydt, tspan, y0, n );

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

  figure ( 2 );
  clf ( );
  plot ( y(:,1), y(:,2), 'linewidth', 2 );
  grid ( 'on' );
  xlabel ( 'Rabbits' );
  ylabel ( 'Foxes' );
  s = sprintf ( 'predator midpoint, phase plot, n = %d.', n );
  title ( s );
  filename = 'predator_midpoint_phase.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  h = predator_conserved ( y );
  figure ( 3 );
  clf ( );
  hold ( 'on' );
  plot ( t, h, 'r-', 'linewidth', 3 );
  plot ( [ t0, tstop ], [0.0, 0.0], 'k-', 'linewidth', 3 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<-- T -->', 'Fontsize', 16 );
  ylabel ( '<-- H(T) -->', 'Fontsize', 16 );
  s = sprintf ( 'predator midpoint, conservation plot, n = %d.', n );
  title ( s );
  filename = 'predator_midpoint_conserved.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  fprintf ( 1, '  h(1) = %g, h(n+1) = %g\n', h(1), h(n+1) );

  return
end

