function unstable_midpoint ( n )

%*****************************************************************************80
%
%% unstable_midpoint() uses midpoint() to solve unstable_ode().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    28 October 2021
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'unstable_midpoint():\n' );
  fprintf ( 1, '  Use midpoint() to solve unstable_ode().\n' );

  [ mu, t0, y0, tstop ] = unstable_parameters ( );

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

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

  dt = ( tstop - t0 ) / n;
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Number of time steps = %d\n', n );
  fprintf ( 1, '  Timestep size =        %g seconds\n', dt );
%
%  Y(t), Y'(t)
%
  figure ( 1 );
  clf ( );
  plot ( t, y, 'linewidth', 1 );
  grid ( 'on' );
  xlabel ( '<---  t  --->' );
  ylabel ( '<---  y(t)  --->' );
  title ( 'unstable_ode(): midpoint' );
  legend ( 'Y(t)', 'Y''(t)' );
  filename = 'unstable_midpoint_plot.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Y(t), Yexact(t)
%
  yexact = unstable_exact ( t );
  figure ( 2 );
  clf ( );
  hold ( 'on' );
  plot ( t, y(:,1), 'linewidth', 1 );
  plot ( t, yexact(:,1), 'linewidth', 1 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<---  t  --->' );
  ylabel ( '<---  y(t), yexact(t)  --->' );
  title ( 'unstable_ode(): midpoint' );
  legend ( 'Y(t)', 'Yexact(t)' );
  filename = 'unstable_midpoint_comparison.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'unstable_midpoint():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end

