function z = dirder ( x, w, f, f0 )

%*****************************************************************************80
%
%% dirder() estimates the directional derivative of a function F.
% 
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 April 2021
%
%  Author:
%
%    Tim Kelley
%
%  Reference:
%
%    Tim Kelley,
%    Iterative Methods for Linear and Nonlinear Equations,
%    SIAM, 2004,
%    ISBN: 0898713528,
%    LC: QA297.8.K45.
%
%  Input:
%
%    real X(N): the point at which the derivative is to be estimated.
%
%    real W(N): the direction.
%
%    function fx = F ( x ): MATLAB code that evaluates the function.
%
%    real F0(N): the value F(X).
%
%  Output:
%
%    real Z(N): the estimate for the directional derivative of F.
%
  x = x(:);
%
%  Hardwired difference increment.
%
  epsnew = 1.0E-07;

  n = length ( x );
%
%  Scale the step.
%
  if ( norm ( w ) == 0.0 )
    z = zeros ( n, 1 );
    return
  end

  epsnew = epsnew / norm ( w );

  if ( 0.0 < norm ( x ) )
    epsnew = epsnew * norm ( x );
  end

  del = x + epsnew * w;
  f1 = feval ( f, del );

  z = ( f1 - f0 ) / epsnew;

  return
end

