Class FiniteDifferencesDifferentiator
 All Implemented Interfaces:
Serializable
,UnivariateFunctionDifferentiator
,UnivariateMatrixFunctionDifferentiator
,UnivariateVectorFunctionDifferentiator
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
values.
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 n1, 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) = (x1)*(x0.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.97e10
 step size = 0.25, fourth order derivative error about 5.43e8
 step size = 1.0e6, second order derivative error about 148
 step size = 1.0e6, 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!
 See Also:

Constructor Summary
ConstructorDescriptionFiniteDifferencesDifferentiator
(int nbPoints, double stepSize) Build a differentiator with number of points and step size when independent variable is unbounded.FiniteDifferencesDifferentiator
(int nbPoints, double stepSize, double tLower, double tUpper) Build a differentiator with number of points and step size when independent variable is bounded. 
Method Summary
Modifier and TypeMethodDescriptiondifferentiate
(UnivariateFunction function) Create an implementation of adifferential
from a regularfunction
.differentiate
(UnivariateMatrixFunction function) Create an implementation of adifferential
from a regularmatrix function
.differentiate
(UnivariateVectorFunction function) Create an implementation of adifferential
from a regularvector function
.int
Get the number of points to use.double
Get the step size.

Constructor Details

FiniteDifferencesDifferentiator
public FiniteDifferencesDifferentiator(int nbPoints, double stepSize) throws MathIllegalArgumentException Build a differentiator with number of points and step size when independent variable is unbounded.Beware that wrong settings for the finite differences differentiator can lead to highly unstable and inaccurate results, especially for high derivation orders. Using very small step sizes is often a bad idea.
 Parameters:
nbPoints
 number of points to usestepSize
 step size (gap between each point) Throws:
MathIllegalArgumentException
 ifstepsize <= 0
(note thatMathIllegalArgumentException
extendsMathIllegalArgumentException
)MathIllegalArgumentException
nbPoint <= 1

FiniteDifferencesDifferentiator
public FiniteDifferencesDifferentiator(int nbPoints, double stepSize, double tLower, double tUpper) throws MathIllegalArgumentException Build a differentiator with number of points and step size when independent variable is bounded.When the independent variable is bounded (tLower < t < tUpper), the sampling points used for differentiation will be adapted to ensure the constraint holds even near the boundaries. This means the sample will not be centered anymore in these cases. At an extreme case, computing derivatives exactly at the lower bound will lead the sample to be entirely on the right side of the derivation point.
Note that the boundaries are considered to be excluded for function evaluation.
Beware that wrong settings for the finite differences differentiator can lead to highly unstable and inaccurate results, especially for high derivation orders. Using very small step sizes is often a bad idea.
 Parameters:
nbPoints
 number of points to usestepSize
 step size (gap between each point)tLower
 lower bound for independent variable (may beDouble.NEGATIVE_INFINITY
if there are no lower bounds)tUpper
 upper bound for independent variable (may beDouble.POSITIVE_INFINITY
if there are no upper bounds) Throws:
MathIllegalArgumentException
 ifstepsize <= 0
(note thatMathIllegalArgumentException
extendsMathIllegalArgumentException
)MathIllegalArgumentException
nbPoint <= 1
MathIllegalArgumentException
stepSize * (nbPoints  1) >= tUpper  tLower


Method Details

getNbPoints
public int getNbPoints()Get the number of points to use. Returns:
 number of points to use

getStepSize
public double getStepSize()Get the step size. Returns:
 step size

differentiate
Create an implementation of adifferential
from a regularfunction
.The returned object cannot compute derivatives to arbitrary orders. The value function will throw a
MathIllegalArgumentException
if the requested derivation order is larger or equal to the number of points. Specified by:
differentiate
in interfaceUnivariateFunctionDifferentiator
 Parameters:
function
 function to differentiate Returns:
 differential function

differentiate
Create an implementation of adifferential
from a regularvector function
.The returned object cannot compute derivatives to arbitrary orders. The value function will throw a
MathIllegalArgumentException
if the requested derivation order is larger or equal to the number of points. Specified by:
differentiate
in interfaceUnivariateVectorFunctionDifferentiator
 Parameters:
function
 function to differentiate Returns:
 differential function

differentiate
Create an implementation of adifferential
from a regularmatrix function
.The returned object cannot compute derivatives to arbitrary orders. The value function will throw a
MathIllegalArgumentException
if the requested derivation order is larger or equal to the number of points. Specified by:
differentiate
in interfaceUnivariateMatrixFunctionDifferentiator
 Parameters:
function
 function to differentiate Returns:
 differential function
