function predator_euler_conservation ( n )

%*****************************************************************************80
%
%% predator_euler_conservation checks conservation for the predator-prey ODE.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    02 September 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N: the number of steps to take.
%
  if ( nargin < 1 )
    n = 10000;
  end

  fprintf ( 1, '\n' );
  fprintf ( 1, 'predator_euler_conservation:\n' );
  fprintf ( 1, '  Evaluate conservation for predator-prey ODE\n' );

  alpha = 2.0;
  beta = 1.0;
  gamma = 1.0;
  delta = 1.0;

  dydt = @ predator_deriv;
  tspan = [ 0.0, 50.0 ];
  u0 = 4.0;
  v0 = 2.0;
  y = [ u0, v0 ];

  [ t, y ] = euler ( dydt, tspan, y, n );
%
%  Conservation.
%
  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 );
%
%  Plot the value of C(T).  For comparison, draw horizontal lines for C(0)
%  and the x axis.
%
  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 euler: C(T) should be constant, N = %d.', n );
  title ( s );
  filename = sprintf ( 'predator_euler_conservation_%d.png', n );
  print ( '-dpng', filename );

  return
end

