ND
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:
-
Method EulerSum method to use Scale 1 size at which variations are expected Terms 7 number of terms to be used WorkingPrecision MachinePrecision precision to use in internal computations - Possible settings for Method include:
-
EulerSum use Richardson's extrapolation to the limit NIntegrate use 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 all close allBasic Examples (1)
Needs["NumericalCalculus`"]ND[Exp[x], x, 1]ND[Cos[x]^3, {x, 2}, 0]Scope (1)
Generalizations & Extensions (1)
Needs["NumericalCalculus`"]ND is threaded element-wise:
ND[{Exp[x], Sin[x]}, x, 1]Options (7)
Method (2)
Needs["NumericalCalculus`"]Use the default Method->EulerSum when expr is not analytic in the neighborhood of x0:
ND[Re[Cos[I y]], y, 1]D[ComplexExpand[Re[Cos[I y]]], y] /. y -> 1//NAn incorrect answer is obtained with Method->NIntegrate:
ND[Re[Cos[I y]], y, 1, Method -> NIntegrate]Needs["NumericalCalculus`"]Here is a derivative where the default method works poorly:
ND[Exp[x^2], {x, 3}, 1]D[Exp[x^2], {x, 3}] /. x -> 1.In this case the expression is analytic, so Method->NIntegrate will work well:
ND[Exp[x^2], {x, 3}, 1, Method -> NIntegrate]Scale (3)
Needs["NumericalCalculus`"]Use Scale->s to capture the region of variation:
ND[Sin[100x], x, 0]The scale of variation is around .01:
ND[Sin[100x], x, 0, Scale -> .01]A value of Scale->s that is too large can be compensated by increasing the number of terms:
ND[Sin[100x], x, 0, Terms -> 11]Needs["NumericalCalculus`"]Use Scale to specify directional derivatives. The left and right derivatives of the nonanalytic function
:
ND[Abs[x], {x, 1}, 0]ND[Abs[x], {x, 1}, 0, Scale -> -1]Plot[Abs[x], {x, -1, 1}]Complex directions may also be specified:
ND[Abs[x], {x, 1}, 0, Scale -> 1 + I]Limit[(Abs[(1 + I)h] - Abs[0]/(1 + I)h), h -> 0]//NNeeds["NumericalCalculus`"]Use the option Scale to avoid regions of non-analyticity when the method used is NIntegrate:
ND[Exp[(1/1 - x)], x, .5, Method -> NIntegrate]Shrinking the radius avoids the essential singularity at x1:
ND[Exp[(1/1 - x)], x, .5, Method -> NIntegrate, Scale -> .1]D[Exp[(1/1 - x)], x] /. x -> .5Terms (1)
Needs["NumericalCalculus`"]Increasing the number of terms may improve accuracy. Here is a somewhat inaccurate approximation:
ND[Sin[x^2], {x, 3}, 1]D[Sin[x^2], {x, 3}] /. x -> 1`30Increasing the number of terms produces a more accurate answer:
ND[Sin[x^2], {x, 3}, 1, Terms -> 10]//FullFormIncreasing the number of terms further can produce nonsense due to numerical instability:
ND[Sin[x^2], {x, 3}, 1, Terms -> 20]Combining an increase in the number of terms with a higher working precision often will reduce the error:
ND[Sin[x^2], {x, 3}, 1, Terms -> 20, WorkingPrecision -> 40]WorkingPrecision (1)
Needs["NumericalCalculus`"]High-order derivatives with Method->EulerSum experience significant subtractive cancellation:
ND[Exp[x], {x, 10}, 0]Using a higher working precision and additional terms produces an accurate answer:
ND[Exp[x], {x, 10}, 0, WorkingPrecision -> 40, Terms -> 10]For this problem, Method->NIntegrate with default options produces a correct answer:
ND[Exp[x], {x, 10}, 0, Method -> NIntegrate]Higher-order derivatives will again experience numerical instability:
ND[Exp[x], {x, 20}, 0, Method -> NIntegrate]Increasing WorkingPrecision will improve the accuracy:
ND[Exp[x], {x, 20}, 0, Method -> NIntegrate, WorkingPrecision -> 30]An alternative is to increase the radius of the contour of integration:
ND[Exp[x], {x, 20}, 0, Method -> NIntegrate, Scale -> 5]Applications (1)
Needs["NumericalCalculus`"]ND is useful for differentiating functions that are only defined numerically. Here is such a function:
f[a_ ? NumericQ, b_ ? NumericQ] := f[a, b] = Module[{t}, y /. NDSolve[{y'[t] == a - b y[t]^3, y[0] == 1}, y, {t, 0, 1}][[1]]]Here is the derivative of f[a,b][t] with respect to b evaluated at {a,b,t}={1,2,1}:
ND[f[1, b][1], {b, 1}, 2]ND can be used as an aid in developing and testing a more robust function for finding the derivative:
fb[a_ ? NumericQ, b_ ? NumericQ] := fb[a, b] = Module[{t}, yb /. NDSolve[{y'[t] == a - b y[t]^3, yb'[t] == -y[t]^3 - 3b y[t]^2yb[t], y[0] == 1, yb[0] == 0}, {y, yb}, {t, 0, 1}][[1]]]fb[1, 2][1]Properties & Relations (3)
Needs["NumericalCalculus`"]The option Method->NIntegrate uses Cauchy's integral formula to compute derivatives:
ND[Exp[x^2], {x, 4}, 0, Method -> NIntegrate]The equivalent computation can be performed using NResidue:
4!NResidue[(Exp[x^2]/x^4 + 1), {x, 0}]Needs["NumericalCalculus`"]The Wolfram Language has built-in code to compute derivatives of numerical functions:
f[t_ ? NumericQ] := 0.001 + 0.002Sin[50t]The built-in numerical derivative code can be used. However, it is unable to capture the rapid oscillations:
f'[.1]D[.001 + .002Sin[50t], t] /. t -> .1Using ND with the appropriate options can compute an accurate derivative:
ND[f[t], t, .1, Scale -> .1]ND[f[t], t, .1, Method -> NIntegrate, Scale -> (1/2)]Needs["NumericalCalculus`"]With Method->EulerSum, ND must be able to evaluate expr at the point x0:
f[x_ ? NumericQ] := (Tanh[π x]/1 + x^2)ND[f[x], x, I]Adding an additional definition for f allows ND to compute the derivative:
f[I//N] = Limit[(Tanh[π x]/1 + x^2), x -> I]ND[f[x], x, I]Limit[D[(Tanh[π x]/1 + x^2), x], x -> I]//NIn this case, Method->NIntegrate produces a more accurate answer:
ND[f[x], x, I, Method -> NIntegrate, Scale -> (1/10)]