function pendulum_rk2 ( n )

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

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

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

  [ t, y ] = rk2 ( 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: rk2' );
  legend ( 'rk2', 'exact' );
  filename = 'pendulum_rk2_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: rk2' );
  legend ( 'rk2', 'exact' );
  filename = 'pendulum_rk2_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: rk2' );
  filename = 'pendulum_rk2_energy.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved in "%s"\n', filename );

  return
end

