blob: ba11ebc2ac7386deb732b5a79ee5f18839614a5f [file] [log] [blame]
@cindex differentiation of functions, numeric
@cindex functions, numerical differentiation
@cindex derivatives, calculating numerically
@cindex numerical derivatives
@cindex slope, see numerical derivative
The functions described in this chapter compute numerical derivatives by
finite differencing. An adaptive algorithm is used to find the best
choice of finite difference and to estimate the error in the derivative.
These functions are declared in the header file @file{gsl_deriv.h}.
@menu
* Numerical Differentiation functions::
* Numerical Differentiation Examples::
* Numerical Differentiation References::
@end menu
@node Numerical Differentiation functions
@section Functions
@deftypefun int gsl_deriv_central (const gsl_function * @var{f}, double @var{x}, double @var{h}, double * @var{result}, double * @var{abserr})
This function computes the numerical derivative of the function @var{f}
at the point @var{x} using an adaptive central difference algorithm with
a step-size of @var{h}. The derivative is returned in @var{result} and an
estimate of its absolute error is returned in @var{abserr}.
The initial value of @var{h} is used to estimate an optimal step-size,
based on the scaling of the truncation error and round-off error in the
derivative calculation. The derivative is computed using a 5-point rule
for equally spaced abscissae at @math{x-h}, @math{x-h/2}, @math{x},
@math{x+h/2}, @math{x+h}, with an error estimate taken from the difference
between the 5-point rule and the corresponding 3-point rule @math{x-h},
@math{x}, @math{x+h}. Note that the value of the function at @math{x}
does not contribute to the derivative calculation, so only 4-points are
actually used.
@end deftypefun
@deftypefun int gsl_deriv_forward (const gsl_function * @var{f}, double @var{x}, double @var{h}, double * @var{result}, double * @var{abserr})
This function computes the numerical derivative of the function @var{f}
at the point @var{x} using an adaptive forward difference algorithm with
a step-size of @var{h}. The function is evaluated only at points greater
than @var{x}, and never at @var{x} itself. The derivative is returned in
@var{result} and an estimate of its absolute error is returned in
@var{abserr}. This function should be used if @math{f(x)} has a
discontinuity at @var{x}, or is undefined for values less than @var{x}.
The initial value of @var{h} is used to estimate an optimal step-size,
based on the scaling of the truncation error and round-off error in the
derivative calculation. The derivative at @math{x} is computed using an
``open'' 4-point rule for equally spaced abscissae at @math{x+h/4},
@math{x+h/2}, @math{x+3h/4}, @math{x+h}, with an error estimate taken
from the difference between the 4-point rule and the corresponding
2-point rule @math{x+h/2}, @math{x+h}.
@end deftypefun
@deftypefun int gsl_deriv_backward (const gsl_function * @var{f}, double @var{x}, double @var{h}, double * @var{result}, double * @var{abserr})
This function computes the numerical derivative of the function @var{f}
at the point @var{x} using an adaptive backward difference algorithm
with a step-size of @var{h}. The function is evaluated only at points
less than @var{x}, and never at @var{x} itself. The derivative is
returned in @var{result} and an estimate of its absolute error is
returned in @var{abserr}. This function should be used if @math{f(x)}
has a discontinuity at @var{x}, or is undefined for values greater than
@var{x}.
This function is equivalent to calling @code{gsl_deriv_forward} with a
negative step-size.
@end deftypefun
@node Numerical Differentiation Examples
@section Examples
The following code estimates the derivative of the function
@c{$f(x) = x^{3/2}$}
@math{f(x) = x^@{3/2@}}
at @math{x=2} and at @math{x=0}. The function @math{f(x)} is
undefined for @math{x<0} so the derivative at @math{x=0} is computed
using @code{gsl_deriv_forward}.
@example
@verbatiminclude examples/diff.c
@end example
@noindent
Here is the output of the program,
@example
$ ./a.out
@verbatiminclude examples/diff.out
@end example
@node Numerical Differentiation References
@section References and Further Reading
The algorithms used by these functions are described in the following sources:
@itemize @asis
@item
Abramowitz and Stegun, @cite{Handbook of Mathematical Functions},
Section 25.3.4, and Table 25.5 (Coefficients for Differentiation).
@item
S.D. Conte and Carl de Boor, @cite{Elementary Numerical Analysis: An
Algorithmic Approach}, McGraw-Hill, 1972.
@end itemize