function stiff_backward_euler_explicit ( n )

%*****************************************************************************80
%
%% stiff_backward_euler_explicit uses the backward Euler method on the stiff 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 stiff_deriv function for this calculation.
%  Instead, we need to work with the formula, and solve for ynew.
%
  t0 = 0.0;
  tstop = 1.0;
  tspan = [ t0, tstop ];
  y0 = 0.0;
%
%  Backward Euler method for the stiff equation:
%    dydt = 50.0 * ( cos ( t ) - y );
%  is approximated by evaluating the right hand side at the NEW time:
%    ( ynew - yold ) / dt = 50 * ( cos ( tnew ) - ynew )
%  and then solving for ynew:
%    ynew                  =   yold + dt * 50 * cos ( tnew ) - dt * 50 * ynew 
%    ynew + dt * 50 * ynew =   yold + dt * 50 * cos ( tnew )
%    ynew                  = ( yold + dt * 50 * cos ( tnew ) ) / ( 1 + dt * 50 )
%
  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 * 50.0 * cos ( t1(i+1) ) ) / ( 1.0 + 50.0 * dt );
  end
%
%  Get exact solution, make plots.
%
  t2 = linspace ( t0, tstop, 101 );
  y2 = stiff_solution ( t2 );

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

  return
end

