function trigger_backward_euler_explicit ( n )

%*****************************************************************************80
%
%% trigger_backward_euler_explicit uses the backward Euler method on the trigger ODE.
%
%  Discussion:
%
%    For this particular ODE, we can rewrite the implicit backward Euler
%    step as an explicit formula.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    19 October 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N: the number of steps to take.
%
  if ( nargin < 1 )
    n = 25;
  end
%
%  We DON'T use the trigger_deriv function for this calculation.
%  Instead, we need to work with the formula, and solve for ynew.
%
  t0 = 0.0;
  tstop = 2.0 * pi;
  tspan = [ t0, tstop ];
  y0 = 0.0;
%
%  Backward Euler method for the trigger equation:
%    dydt = c * ( - y + sin ( t ) )
%  is approximated by evaluating the right hand side at the NEW time:
%    ( ynew - yold ) / dt = c * ( - ynew + sin ( tnew ) )
%  and then solving for ynew:
%    ynew                 =   yold - dt * c * ynew + dt * c * sin ( tnew) 
%    ynew + dt * c * ynew =   yold + dt * c * sin ( tnew )
%    ynew                 = ( yold + dt * c * sin ( tnew ) ) / ( 1 + dt * c )
%
  c = 10.0;

  dt = ( tstop - t0 ) / n;
  t1(1) = t0;
  y1(1) = y0;

  for i = 1 : n
    t1(i+1) = t1(i) + dt;
    y1(i+1) = ( y1(i) + dt * c * sin ( t1(i+1) ) ) / ( 1.0 + c * dt );
  end
%
%  Get exact solution, make plots.
%
  t2 = linspace ( t0, tstop, 101 );
  y2 = trigger_solution ( t2 );

  plot ( t1, y1, 'r-o', ...
         t2, y2, 'b-', ...
         'linewidth', 3 );
  grid ( 'on' );
  xlabel ( '<-- t -->' );
  ylabel ( '<-- y(t) -->' );
  s = sprintf ( 'trigger ODE, backward Euler explicit, n = %d', n );
  title ( s );
  legend ( 'Computed', 'Exact' );
  filename = sprintf ( 'trigger_backward_euler_explicit_%d.png', n );
  print ( '-dpng', filename );

  return
end

