Blame doc/diff.rst

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