%% fd_plot1 plots the error in a forward difference estimate of a first derivative.
%
%  Discussion:
%
%    Use the forward difference estimate for the first derivative of tan(x) at
%    x = pi/4, for which the exact value is 2.
%
%    Do this for 50 values of h, starting with h = 0.25, and decreasting by a
%    factor of 1/2.
%
%    Compute the error, and plot it against h.
%
%    Note that a great deal of error occurs for very small h, but does not
%    show up in the plot!
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    31 August 2019
%
%  Author:
%
%    John Burkardt
%
  h = 0.25; 
  alpha = 0.5;
  n = 50;
  p = 0 : n - 1;
  h_list = h * alpha .^ p;
  fd_list = fd ( @(x)tan(x), pi/4, h_list );
  e_list = abs ( 2.0 - fd_list );
  plot ( h_list, e_list );
  print ( '-dpng', 'fd_plot1.png' );
  title ( 'Error versus step h' )
  grid ( 'on' )
  [ e_min, i_min ] = min ( e_list );

  fprintf ( 1, '  Minimum error is at index %d\n', i_min );
  fprintf ( 1, '  Minimum error is %g\n', e_min );
  fprintf ( 1, '  Best h is %g\n', h_list(i_min) );
  fprintf ( 1, '  Best estimate is %20.16g\n', fd_list(i_min) );

