org.apache.commons.math3.analysis.differentiation

## Class FiniteDifferencesDifferentiator

• java.lang.Object
• org.apache.commons.math3.analysis.differentiation.FiniteDifferencesDifferentiator
• All Implemented Interfaces:
Serializable, UnivariateFunctionDifferentiator, UnivariateMatrixFunctionDifferentiator, UnivariateVectorFunctionDifferentiator

public class FiniteDifferencesDifferentiator
extends Object
implements UnivariateFunctionDifferentiator, UnivariateVectorFunctionDifferentiator, UnivariateMatrixFunctionDifferentiator, Serializable
Univariate functions differentiator using finite differences.

This class creates some wrapper objects around regular univariate functions (or univariate vector functions or univariate matrix functions). These wrapper objects compute derivatives in addition to function value.

The wrapper objects work by calling the underlying function on a sampling grid around the current point and performing polynomial interpolation. A finite differences scheme with n points is theoretically able to compute derivatives up to order n-1, but it is generally better to have a slight margin. The step size must also be small enough in order for the polynomial approximation to be good in the current point neighborhood, but it should not be too small because numerical instability appears quickly (there are several differences of close points). Choosing the number of points and the step size is highly problem dependent.

As an example of good and bad settings, lets consider the quintic polynomial function f(x) = (x-1)*(x-0.5)*x*(x+0.5)*(x+1). Since it is a polynomial, finite differences with at least 6 points should theoretically recover the exact same polynomial and hence compute accurate derivatives for any order. However, due to numerical errors, we get the following results for a 7 points finite differences for abscissae in the [-10, 10] range:

• step size = 0.25, second order derivative error about 9.97e-10
• step size = 0.25, fourth order derivative error about 5.43e-8
• step size = 1.0e-6, second order derivative error about 148
• step size = 1.0e-6, fourth order derivative error about 6.35e+14
This example shows that the small step size is really bad, even simply for second order derivative!

Since:
3.1
Version:
$Id: FiniteDifferencesDifferentiator.java 1420666 2012-12-12 13:33:20Z erans$