function pendulum_control_euler ( n, alpha_value )

%*****************************************************************************80
%
%% pendulum_control_euler calls euler() with specific values of n, a, b, and y0.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    03 November 2019
%
%  Author:
%
%    John Burkardt
%
  global alpha

  if ( nargin < 1 )
    n = input ( 'Enter number of time steps, n: ' );
  end

  if ( nargin < 2 )
    alpha_value = input ( 'Enter friction coefficient alpha: ' );
  end

  alpha = alpha_value;

  a = 0.0;
  b = 6.0 * pi;
  u = 1.0;
  v = 0.0;
  y0 = [ u, v ];
  E0 = u^2 + v^2;
  
  t = linspace ( a, b, n + 1 );
  t = t';
  dt = ( b - a ) / n;

  m = length ( y0 );
  y = zeros ( n + 1, m );
  y(1,:) = y0(:);

  for i = 1 : n
    y(i+1,:) = y(i,:) + dt * ( pendulum_friction_prime ( t(i), y(i,:) ) )';
    E = y(i+1,1).^2 + y(i+1,2).^2;
    if ( E < 0.70 * E0 )
      y(i+1,1:2) = sqrt ( E0 / E ) * y(i+1,1:2);
      fprintf ( 'Battery jolt at time %g\n', t(i+1) );
    end
  end

  figure ( 1 )
  plot ( t, y, 'm-', 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( '<-- T -->' );
  ylabel ( '<-- Y(T) -->' );
  dt = ( b - a ) / n;
  label = sprintf ( 'controlled pendulum, friction = %g, dt = %g\n', alpha, dt );
  title ( label );
  filename = 'pendulum_control_plot.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  figure ( 2 )
  clf ( );
  hold ( 'on' );
  plot ( y(:,1), y(:,2), 'm-', 'linewidth', 3 );
  plot ( y(1,1), y(1,2), 'g.', 'markersize', 40 );
  plot ( y(end,1), y(end,2), 'r.', 'markersize', 30 );
  hold ( 'off' );
  grid ( 'on' );
  axis ( 'equal' );
  xlabel ( '<-- Theta -->' );
  ylabel ( '<-- dThetadT -->' );
  dt = ( b - a ) / n;
  label = sprintf ( 'controlled pendulum, friction = %g, dt = %g\n', alpha, dt );
  title ( label );
  filename = 'pendulum_control_phase.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end

  
