NumericalCalculus`
NumericalCalculus`

ND

ND[expr,x,x0]

gives a numerical approximation to the derivative of expr with respect to x at the point x0.

ND[expr,{x,n},x0]

gives a numerical approximation to the n^(th) derivative of expr.

Details and Options

  • To use ND, you first need to load the Numerical Calculus Package using Needs["NumericalCalculus`"].
  • The expression expr must be numeric when its argument x is numeric.
  • ND[expr,x,x0] is equivalent to ND[expr,{x,1},x0].
  • ND is unable to recognize small numbers that should in fact be zero. Chop may be needed to eliminate these spurious residuals.
  • The following options can be given:
  • MethodEulerSummethod to use
    Scale1size at which variations are expected
    Terms7number of terms to be used
    WorkingPrecisionMachinePrecisionprecision to use in internal computations
  • Possible settings for Method include:
  • EulerSumuse Richardson's extrapolation to the limit
    NIntegrateuse Cauchy's integral formula
  • With Method->EulerSum, ND needs to evaluate expr at x0.
  • If expr is not analytic in the neighborhood of x0, then the default method EulerSum must be used.
  • The option Scale->s is used to capture the scale of variation when using Method->EulerSum.
  • When the value of the derivative depends on the direction, the default is to the right. Other directions can be chosen with the option Scale->s, where the direction is s.
  • The option Terms->n gives the number of terms to use for extrapolation when using Method->EulerSum.
  • With Method->NIntegrate, the expression expr must be analytic in a neighborhood of the point x0.
  • The option Scale->r specifies the radius of the contour of integration to use with Method->NIntegrate.

Examples

open allclose all

Basic Examples  (1)

Scope  (1)

The expression and evaluation point may be complex:

Generalizations & Extensions  (1)

ND is threaded element-wise:

Options  (7)

Method  (2)

Use the default Method->EulerSum when expr is not analytic in the neighborhood of x0:

Check:

An incorrect answer is obtained with Method->NIntegrate:

Here is a derivative where the default method works poorly:

The correct answer is:

In this case the expression is analytic, so Method->NIntegrate will work well:

Scale  (3)

Use Scale->s to capture the region of variation:

The scale of variation is around .01:

A value of Scale->s that is too large can be compensated by increasing the number of terms:

Use Scale to specify directional derivatives. The left and right derivatives of the nonanalytic function :

Check:

Complex directions may also be specified:

Check:

Use the option Scale to avoid regions of non-analyticity when the method used is NIntegrate:

Shrinking the radius avoids the essential singularity at x1:

Check:

Terms  (1)

Increasing the number of terms may improve accuracy. Here is a somewhat inaccurate approximation:

Check:

Increasing the number of terms produces a more accurate answer:

Increasing the number of terms further can produce nonsense due to numerical instability:

Combining an increase in the number of terms with a higher working precision often will reduce the error:

WorkingPrecision  (1)

High-order derivatives with Method->EulerSum experience significant subtractive cancellation:

Using a higher working precision and additional terms produces an accurate answer:

For this problem, Method->NIntegrate with default options produces a correct answer:

Higher-order derivatives will again experience numerical instability:

Increasing WorkingPrecision will improve the accuracy:

An alternative is to increase the radius of the contour of integration:

Applications  (1)

ND is useful for differentiating functions that are only defined numerically. Here is such a function:

Here is the derivative of f[a,b][t] with respect to b evaluated at {a,b,t}={1,2,1}:

ND can be used as an aid in developing and testing a more robust function for finding the derivative:

Check:

Properties & Relations  (3)

The option Method->NIntegrate uses Cauchy's integral formula to compute derivatives:

The equivalent computation can be performed using NResidue:

The Wolfram Language has built-in code to compute derivatives of numerical functions:

The built-in numerical derivative code can be used. However, it is unable to capture the rapid oscillations:

The correct answer:

Using ND with the appropriate options can compute an accurate derivative:

With Method->EulerSum, ND must be able to evaluate expr at the point x0:

Adding an additional definition for f allows ND to compute the derivative:

Check:

In this case, Method->NIntegrate produces a more accurate answer:

Neat Examples  (1)

Some fractional and complex derivatives can be computed: