SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.analysis.differentiation.univariate
Class FiniteDifference

java.lang.Object
  extended by com.numericalmethod.suanshu.analysis.function.rn2r1.UnivariateRealFunction
      extended by com.numericalmethod.suanshu.analysis.differentiation.univariate.FiniteDifference
All Implemented Interfaces:
Function, RealScalarFunction

public class FiniteDifference
extends UnivariateRealFunction

A finite difference (divided by a small increment) is an approximation of the derivative of a function.

The accuracy of central difference is the best while those of forward and backward differences are more or less the same.

In general, 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. Also, the accuracy depends on the function to take the derivative of.

See Also:

Nested Class Summary
static class FiniteDifference.Type
          the types of finite difference available
 
Nested classes/interfaces inherited from interface com.numericalmethod.suanshu.analysis.function.Function
Function.EvaluationException
 
Field Summary
 UnivariateRealFunction f
          the univariate function to take the derivative of
 int order
          the order of the derivative
 FiniteDifference.Type type
          the type of finite difference chosen
 
Constructor Summary
FiniteDifference(UnivariateRealFunction f, int order, FiniteDifference.Type type)
          Construct an approximation of the derivative function for f using finite difference.
 
Method Summary
 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.
 double evaluate(double x)
          Numerically evaluate the derivative of f at point x.
 double evaluate(double[] x, double h)
          Numerically evaluate the derivative of f at point x with step size h.
 
Methods inherited from class com.numericalmethod.suanshu.analysis.function.rn2r1.UnivariateRealFunction
dimension4Domain, dimension4Range, evaluate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

f

public final UnivariateRealFunction f
the univariate function to take the derivative of


order

public final int order
the order of the derivative


type

public final FiniteDifference.Type type
the type of finite difference chosen

Constructor Detail

FiniteDifference

public FiniteDifference(UnivariateRealFunction f,
                        int order,
                        FiniteDifference.Type type)
Construct an approximation of the derivative function for f using finite difference.

Parameters:
f - a univariate function
order - the order of the derivative
type - the type of finite difference type to use, c.f., FiniteDifference.Type
Method Detail

evaluate

public double evaluate(double x)
Numerically evaluate the derivative of f at point x.

Specified by:
evaluate in class UnivariateRealFunction
Parameters:
x - the point to evaluate the derivative at
Returns:
the numerical derivative of f at point x
See Also:
Wikipedia: Practical considerations

evaluate

public double evaluate(double[] x,
                       double h)
Numerically evaluate the derivative of f at point x with step size h.

Automatically determining the step size h could be challenging, esp. when |x| is near 0. An analysis that involves f' and f'' may be needed to determine the proper step size. User may want to experiment with different h's by calling this function.

Parameters:
x - the point to evaluate the derivative of f at. Although it is a double[] array, only x[0] is used (f is univariate).
h - step size
Returns:
the numerical derivative of f at point x using 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 - evaluate the function at x
h - the increment
Returns:
dnh[f](x)
See Also:
Wikipedia: Higher-order differences

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.