# SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.analysis.differentiation.univariate

## 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.
• ### Nested Class Summary

Nested Classes
Modifier and Type Class and Description
static class  FiniteDifference.Type
the available types of finite difference
• ### Nested classes/interfaces inherited from interface com.numericalmethod.suanshu.analysis.function.Function

Function.EvaluationException
• ### Constructor Summary

Constructors
Constructor and Description
FiniteDifference(UnivariateRealFunction f, int order, FiniteDifference.Type type)
Construct an approximate derivative function for f using finite difference.
• ### Method Summary

All Methods
Modifier and Type Method and Description
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)
Evaluate y = f(x).
double evaluate(double x, double h)
Evaluate numerically the derivative of f at point x, f'(x), with step size h.
• ### Methods inherited from class com.numericalmethod.suanshu.analysis.function.rn2r1.univariate.AbstractUnivariateRealFunction

evaluate
• ### Methods inherited from class com.numericalmethod.suanshu.analysis.function.rn2r1.AbstractRealScalarFunction

dimensionOfDomain, dimensionOfRange
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
• ### Methods inherited from interface com.numericalmethod.suanshu.analysis.function.Function

dimensionOfDomain, dimensionOfRange
• ### Constructor Detail

• #### 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 Detail

• #### 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)$$