function kepler_midpoint ( n )

%*****************************************************************************80
%
%% kepler_midpoint() uses midpoint() to solve kepler_ode().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    23 April 2021
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'kepler_midpoint_test():\n' );
  fprintf ( 1, '  Use midpoint() to solve kepler_ode().\n' );

  [ e, t0, y0, tstop ] = kepler_parameters ( );

  f = @kepler_deriv;
  tspan = [ t0, tstop ];

  [ t, y ] = midpoint ( f, tspan, y0, n );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Number of fixed steps = %d\n',n );

  figure ( 1 );
  clf ( );
  hold ( 'on' );
  plot ( t, y(:,1), 'r-', 'linewidth', 2 );
  plot ( t, y(:,2), 'g-', 'linewidth', 2 );
  plot ( t, y(:,3), 'b-', 'linewidth', 2 );
  plot ( t, y(:,4), 'm-', 'linewidth', 2 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<---  t  --->' );
  ylabel ( '<---  p,q  --->' );
  title ( 'kepler_ode(): midpoint, Time Plot', 'fontsize', 24 )
  legend ( 'P', 'P''', 'Q', 'Q''' );
  filename = 'kepler_midpoint_plot.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  figure ( 2 );
  plot ( y(:,1), y(:,2) );
  grid ( 'on' );
  xlabel ( '<---  q1  --->' );
  ylabel ( '<---  q2  --->' );
  title ( 'kepler_ode(): midpoint, Phase Plot', 'fontsize', 24 )
  filename = 'kepler_midpoint_phase.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  [ h1, h2 ] = kepler_conserved ( y );

  figure ( 3 );
  clf ( );
  hold ( 'on' );
    plot ( t, h1, 'r-', 'linewidth', 3 );
    plot ( tspan, [h1(1), h1(1) ], 'k:', 'linewidth', 3 );
    plot ( tspan, [0.0, 0.0 ], 'k--', 'linewidth', 3 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<-- T -->', 'Fontsize', 16 );
  ylabel ( '<-- H1(T) -->', 'Fontsize', 16 );
  title ( 'kepler_ode(): midpoint: H1(T)', 'fontsize', 24 );
  filename = 'kepler_midpoint_h1.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  figure ( 4 );
  clf ( );
  hold ( 'on' );
    plot ( t, h2, 'r-', 'linewidth', 3 );
    plot ( tspan, [h2(1), h2(1) ], 'k:', 'linewidth', 3 );
    plot ( tspan, [0.0, 0.0 ], 'k--', 'linewidth', 3 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<-- T -->', 'Fontsize', 16 );
  ylabel ( '<-- H2(T) -->', 'Fontsize', 16 );
  title ( 'kepler_ode(): midpoint: H2(T)', 'fontsize', 24 );
  filename = 'kepler_midpoint_h2.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end
