function rigid_body_backward_euler ( n )

%*****************************************************************************80
%
%% rigid_body_backward_euler() uses backward_euler() to solve rigid_body_ode().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    30 April 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N: the number of steps to take.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'rigid_body_backward_euler():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  Solve rigid_body_ode() using backward_euler().\n' );

  [ i1, i2, i3, t0, y0, tstop ] = rigid_body_parameters ( );

  tspan = [ t0, tstop ];

  f = @rigid_body_deriv;

  [ t, xyz ] = backward_euler ( f, tspan, y0, n );

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

  if ( 2000 <= n )
    ilo = n - 125;
  else
    ilo = 1;
  end

  figure ( 1 );
  plot ( t(ilo:n), xyz(ilo:n,:), 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( '<-- T -->', 'Fontsize', 16 );
  ylabel ( '<-- XYZ(T) -->', 'Fontsize', 16 );
  title ( 'rigid_body_ode(): backward euler: time plot' );
  legend ( 'X(t)', 'Y(t)', 'Z(t)' );
  filename = 'rigid_body_backward_backward euler_plot.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  figure ( 2 );
  plot3 ( xyz(ilo:n,1), xyz(ilo:n,2), xyz(ilo:n,3), 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( '<-- X -->', 'Fontsize', 16 );
  ylabel ( '<-- Y -->', 'Fontsize', 16 );
  zlabel ( '<-- Z -->', 'Fontsize', 16 );
  title ( 'rigid_body_ode(): backward euler: phase' );
  view ( 3 );
  filename = 'rigid_body_backward_backward_euler_phase.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  [ h1, h2 ] = rigid_body_conserved ( xyz );

  figure ( 3 );
  clf ( );
  hold ( 'on' );
    plot ( t, h1, 'r-', 'linewidth', 3 );
    plot ( [ tspan ], [ h1(1), h1(1) ], 'b--', 'linewidth', 3 );
    plot ( [ tspan ], [ 0.0, 0.0 ], 'k--', 'linewidth', 3 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<-- T -->', 'Fontsize', 16 );
  ylabel ( '<-- H1(T) -->', 'Fontsize', 16 );
  title ( 'rigid_body_ode(): backward euler: conservation h1(t)' );
  filename = 'rigid_body_backward_euler_conservation_h1.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  figure ( 4 );
  clf ( );
  hold ( 'on' );
    plot ( t, h2, 'r-', 'linewidth', 3 );
    plot ( [ tspan ], [ h2(1), h2(1) ], 'b--', 'linewidth', 3 );
    plot ( [ tspan ], [ 0.0, 0.0 ], 'k--', 'linewidth', 3 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<-- T -->', 'Fontsize', 16 );
  ylabel ( '<-- H2(T) -->', 'Fontsize', 16 );
  title ( 'rigid_body_ode(): backward euler: conservation h2(t)' );
  filename = 'rigid_body_backward_euler_conservation_h2.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end
