function polar_midpoint ( n )

%*****************************************************************************80
%
%% polar_midpoint() uses midpoint() on the polar ODE.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    24 April 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N, the number of time steps.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'polar_midpoint_test()\n' );
  fprintf ( 1, '  Solve the polar ODE using midpoint().\n' );

  [ t0, z0, tstop ] = polar_parameters ( );

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

  [ t1, z1 ] = midpoint ( f, tspan, z0, n );

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

  t2 = linspace ( t0, tstop, 201 );
  z2 = polar_exact ( t2 );
%
%  Polar plot.
%
  plot ( real ( z1 ), imag ( z1 ), 'ro', ...
         real ( z2 ), imag ( z2 ), 'b-', ...
         'linewidth', 3 );
  grid ( 'on' );
  xlabel ( '<-- R(z) -->' );
  ylabel ( '<-- I(z) -->' );
  title ( ' polar ODE, midpoint' );
  legend ( 'Computed', 'Exact' );
  filename = 'polar_midpoint.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Time plot.
%
  plot ( t1, real ( z1 ), 'ro', t1, imag ( z1 ), 'mo', ...
         t2, real ( z2 ), 'b-', t2, imag ( z2 ), 'c-', ...
         'linewidth', 3 );
  grid ( 'on' );
  xlabel ( '<-- t -->' );
  ylabel ( '<-- z(t) -->' );
  title ( ' polar ODE, midpoint' );
  legend ( 'R(approx)', 'I(approx)', 'R(exact)', 'I(exact)' );
  filename = 'polar_midpoint_time.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Compare solution values at midpoint's nodes.
%
  z2 = polar_exact ( t1 );
  e = rms ( z1 - z2 );
  fprintf ( 1, '  RMS error norm = %g\n', e );
  fprintf ( 1, '  L2 error norm = %g\n', norm ( z1 - z2 ) );

  return
end

