function yp = diff_forward ( f_string, x, h )

%*****************************************************************************80
%
%% diff_forward interactively uses forward differences to estimate f'(x).
%
%  Discussion:
%
%    This function manages an interactive computation which uses
%    forward differences to estimate the derivative of f(x) 
%    at the point x, using a stepsize of h.
%
%    The user enters a formula for f(x), and the values of x and h.
%
%    If a call is made of the form 
%      yp = diff_forward ( f_string, x, h )
%    then x can be a vector.  Otherwise, x should be a single value.
%
%    The program returns yp, the forward difference estimate for f'(x).
%
%    Using a negative value for h will result in the backward difference estimate.
%
%    The program can be invoked by a function call, in which case the
%    string specifying f(x) must be quoted:
%      yp = diff_forward ( 'x^2', 1.0, 0.01 )
%    or as an interactive command with arguments:
%      yp = diff_forward x^2 1.0 0.01
%    or, if called with no arguments, it will request them:
%      yp = diff_forward ( );
%      Enter function formula, like x^2: x^2
%      Enter evaluation point, x: 1.0
%      Enter stepsize, h: 0.01
%
%    The function is specified as a string which is either:
%    * a MATLAB expression using the argument 'x', 
%    * the name of an M-file followed by the argument '(x)'.
%
%    The string should not contain any spaces between symbols, except
%    when it is passed as a function argument in quotes.
%
%    It is not necessary to use the "dot" notation for expressions involving
%    '*', '/', or '^', but it doesn't hurt either.
%
%    Examples of function specifications:
%
%      x^2
%      x.^2
%      3/(x^4+5*x-6)
%      sin(7*x)*sqrt(x)/8
%      wiggle(x)     <-- where "wiggle.m" is a user-provided M file.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    26 July 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string f_string: the function to be interpolated.
%
%    real x: the evaluation point.
%
%    real h: the stepsize to use.
%
%  Output:
%
%    real yp: the forward difference estimate for the derivative.
%

%
%  Get F, X, H
%
  if ( nargin < 1 )
    f_string = input ( 'Enter function formula, like x^2: ', 's' );
  end
  f_element = vector_to_element ( f_string );
  f = str2func ( [ '@(x)', f_element ] );

  if ( nargin < 2 )
    x = input ( 'Enter evaluation point x: ' );
  elseif ( ischar ( x ) )
    x = str2num ( x );
  end

  if ( nargin < 3 )
    h = input ( 'Enter stepsize h: ' );
  elseif ( ischar ( h ) )
    h = str2num ( h );
  end

  n = length ( x );
  yp = zeros ( n, 1 );
  for i = 1 : n
    yp(i) = ( f ( x(i) + h ) - f ( x(i) ) ) / h; 
  end

  return
end
function f_element = vector_to_element ( f_vector )

%*****************************************************************************80
%
%% vector_to_element rewrites a MATLAB vector function elementwise.
%
%  Discussion:
%
%    In MATLAB, by default, the operators '*', '/', and '^' are interpreted
%    as linear algebraic operations.  In many cases, a user simply desired
%    the elementwise operation instead, which MATLAB indicates using a 
%    dotted notation: '.*', './', and '.^'.
%
%    This function converts a string to a form that uses dotted operators.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    29 June 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string f_vector, a string presumably representing a MATLAB expression.
%
%  Output:
%
%    string f_element, a copy of f_vector in which all operators have been
%    replaced by their dotted (elementwise) form.
%
  n_vector = length ( f_vector );
  f_element = [''];

  c_prev = '';
  for iv = 1 : n_vector
    c = f_vector(iv);
    if ( ( c == '*' || c == '/' || c == '^' ) && ( c_prev ~= '.' ) )
      f_element = [ f_element, ' .', c, ' ' ];
    else
      f_element = [ f_element, c ];
    end
    c_prev = c;
  end

  return
end

