Class FiniteDifference

All Implemented Interfaces:
Function<Vector,Double>, RealScalarFunction, UnivariateRealFunction

public class FiniteDifference extends AbstractUnivariateRealFunction
A finite difference (divided by a small increment) is an approximation of the derivative of a function. The accuracy depends on the function to take the derivative of. In general, the accuracy of central difference is the best while those of forward and backward differences are more or less the same. For finite difference, the higher an order of a derivative, the less accurate it gets. For example, approximating the 5-th derivative is much less accurate than approximating the 1st derivative.
See Also:
  • Constructor Details

    • FiniteDifference

      public FiniteDifference(UnivariateRealFunction f, int order, FiniteDifference.Type type)
      Construct an approximate derivative function for f using finite difference.
      Parameters:
      f - a univariate function
      order - the order of the derivative
      type - the type of finite difference to use, c.f., FiniteDifference.Type
  • Method Details

    • evaluate

      public double evaluate(double x)
      Description copied from interface: UnivariateRealFunction
      Evaluate y = f(x).
      Parameters:
      x - x
      Returns:
      f(x)
    • evaluate

      public double evaluate(double x, double h)
      Evaluate numerically the derivative of f at point x, f'(x), with step size h. It could be challenging to automatically determine the step size h, esp. when |x| is near 0. It may, for example, require an analysis that involves f' and f''. The user may want to experiment with different hs by calling this function.
      Parameters:
      x - the point to evaluate the derivative of f at
      h - step size
      Returns:
      f'(x), the numerical derivative of f at point x with step size h
    • df

      public double df(double x, double h)
      Compute the finite difference for f at x with an increment h for the n-th order using either forward, backward, or central difference.
      Parameters:
      x - the point to evaluate the function at
      h - the increment
      Returns:
      \(\Delta^n_hf(x)\)
      See Also: