function squircle_midpoint ( n )

%*****************************************************************************80
%
%% squircle_midpoint() tests squircle_ode() using midpoint().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    21 December 2020
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'squircle_midpoint():\n' );
  fprintf ( 1, '  Test squircle_ode() using midpoint().\n' );

  [ s, t0, y0, tstop ] = squircle_parameters ( );

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

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

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Number of steps taken was %d\n', n );
%
%  Plot u, v.
%
  figure ( 1 );
  clf ( );
  hold ( 'on' );
    plot ( t, y(:,1), 'r-', 'linewidth', 2 );
    plot ( t, y(:,2), 'b-', 'linewidth', 2 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<-- Time -->' );
  ylabel ( '<-- Solution components -->' );
  title ( 'squircle_ode(): midpoint: solution components' );
  legend ( 'y_1', 'y_2' );
  filename = 'squircle_midpoint_plot.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved in "%s"\n', filename );
%
%  Phase plot.
%
  figure ( 2 );
  clf ( );
  plot ( y(:,1), y(:,2), 'b-', 'linewidth', 2 );
  grid ( 'on' );
  xlabel ( '<-- y_1 -->' );
  ylabel ( '<-- y_2 -->' );
  title ( 'squircle_ode(): midpoint: phase plot' );
  axis ( 'equal' );
  filename = 'squircle_midpoint_phase.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved in "%s"\n', filename );
%
%  Plot energy.
%
  h = squircle_conserved ( y );

  figure ( 3 );
  clf ( );
  hold ( 'on' );
    plot ( t, h, 'r-', 'linewidth', 2 );
    plot ( [ t0, tstop ], [ h(1), h(1) ], 'b--', 'linewidth', 2 );
    plot ( [ t0, tstop ], [ 0.0, 0.0 ], 'k-', 'linewidth', 2 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<-- Time -->' );
  ylabel ( '<-- Energy -->' );
  title ( 'squircle_ode(): midpoint: energy' );
  legend ( 'Computed energy', 'Exact energy', 'Zero energy' );
  filename = 'squircle_midpoint_conservation.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved in "%s"\n', filename );

  return
end

