function pendulum_midpoint ( n )

%*****************************************************************************80
%
%% pendulum_midpoint tests pendulum_ode using the midpoint method.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    31 October 2020
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'pendulum_midpoint:\n' );
  fprintf ( 1, '  Test pendulum_ode using midpoint().\n' );

  [ g, l, m, t0, y0 ] = pendulum_parameters ( );

  f = @ pendulum_deriv;
  tspan = [ t0, 20.0 ];
  if ( nargin < 1 )
    n = 100;
  end

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

  ye = pendulum_solution ( t );
%
%  Plot u = theta.
%
  figure ( 1 );
  clf ( );
  plot ( t, y(:,1), 'ro', ...
         t, ye(:,1), 'b-', 'linewidth', 2 );
  grid ( 'on' );
  xlabel ( '<-- Time -->' );
  ylabel ( '<-- Angular deflection (radians) -->' );
  title ( 'Linear pendulum, theta: midpoint' );
  legend ( 'midpoint', 'exact' );
  filename = 'pendulum_midpoint_theta.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved in "%s"\n', filename );
%
%  Plot v = thetadot.
%
  figure ( 2 );
  clf ( );
  plot ( t, y(:,2), 'ro', ...
         t, ye(:,2), 'b-', 'linewidth', 2 );
  grid ( 'on' );
  xlabel ( '<-- Time -->' );
  ylabel ( '<-- Angular velocity (radians/sec) -->' );
  title ( 'Linear pendulum, thetadot: midpoint' );
  legend ( 'midpoint', 'exact' );
  filename = 'pendulum_midpoint_thetadot.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved in "%s"\n', filename );
%
%  Plot energy.
%
  h = pendulum_energy ( y );

  figure ( 3 );
  clf ( );
  hold ( 'on' );
    plot ( t, h, 'b-', 'linewidth', 2 );
    plot ( [t(1), t(end) ], [ 0.0, 0.0 ], 'k-', 'linewidth', 2 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<-- Time -->' );
  ylabel ( '<-- Energy -->' );
  title ( 'Energy conservation: midpoint' );
  filename = 'pendulum_midpoint_energy.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved in "%s"\n', filename );

  return
end

