fdiff
compute a numerical approximation for the (partial) derivative of an expression evaluated at a point
Calling Sequence
Parameters
Description
Computational Approach
Examples
fdiff( f(x), x=a, prec )
fdiff( f(x), x, x=a, prec )
fdiff( f(x,y,...), [x=a, y=b, ...], prec )
fdiff( f(x,y,...), [x, y, ...], [x=a, y=b, ...], prec )
fdiff( g, [1, 2, ...], [a, b, ...], prec )
f(x)
-
algebraic expression depending on x
f(x, y, ...)
algebraic expression depending on x, y, ...
x, y, ...
names of the differentiation variables
a, b, ...
constants
g
procedure or operator
prec
(optional) equation of the form workprec = n, where n is a numerical constant > 1; working precision
The expression fdiff(sin(x), x=1) computes a numerical approximation to sin'(1) = cos(1) without using a formula for diff(sin(x),x). It does this by evaluating f at two points near x=1. More generally, the command fdiff(f(x,y,z),[x,x,y],[x=a,y=b,z=c] computes a numerical approximation for the partial derivative diff(f(x,y,z), x,x,y) evaluated at x=a,y=b,z=c. The differentiation is not performed symbolically, and the computation of this approximation assumes only that f can be evaluated numerically to high precision near the point x=a,y=b,z=c.
The fdiff command is useful when a closed form formula is not known for f'⁡x, thus for instance, permitting the plotting of expressions containing such derivatives. The accuracy of the approximation is determined by the global variable Digits and the optional argument workprec which is described in the Computational Approach section following. The algorithm used will normally, but not necessarily, output a result accurate to Digits precision.
The first argument passed to fdiff is the expression being differentiated. The second argument can be a set or a list of the differentiation variables. It specifies the order of the derivative. The third argument can be set or a list with equations containing the differentiation variables on the left-hand sides and the evaluation points on the right-hand sides. It specifies the point at which the derivative is computed. For example,
fdiff⁡f⁡x,y,x,x,y,x=a,y=b=∂3∂x2∂yf⁡x,yx=a,y=b|∂3∂x2∂yf⁡x,yx=a,y=b
Note: The order of the derivative is always equal to the number of elements in the second argument, and the equations in the third argument should assure that all the names in the expression being differentiated get a value, and the evaluation points (a,b in the above) should be numerical constants so that numerical differentiation can be performed. If the numerical derivative cannot be computed, fdiff returns unevaluated.
The right-hand side in the correspondence above can also be entered as an argument to evalf, for example,
evalf⁡∂3∂x2∂yf⁡x,yx=a,y=b|∂3∂x2∂yf⁡x,yx=a,y=b
in which case it is (internally) transformed into the corresponding call to fdiff, then numerically evaluated. The argument to evalf can also involve Eval instead of eval and Diff instead of diff.
Alternatively, the following shorter syntax can be used:
evalf⁡D1,1,2⁡f⁡a,b
For simplicity, an alternative syntax is allowed: the second argument, x,y in fdiff( f(x,y), [x,y], {x=a, y=b}) can be omitted, in which case the third argument, x=a,y=b indicates, simultaneously, the differentiations to be performed and the evaluation points. For example, these two calls result in the same output
fdiff⁡f⁡x,y,x,y,x=a,y=b=fdiff⁡f⁡x,y,x=a,y=b
Note: In the right-hand side, every equation entering the second argument implies a differentiation.
When the expression being differentiated depends on one variable only, or the computation is a first derivative, the second or third arguments can also be given without enclosing them as sets or lists, for example,
fdiff⁡f⁡x,x=a;
fdiff⁡f⁡x,x,x=a;
fdiff⁡f⁡x,y,z,x,x=a,y=b,z=c;
The expression being differentiated can also be a procedure or operator, which is a function of one or more inputs. This is useful when a function cannot be expressed as a formula and also when a program would be a more efficient representation of a function than a formula. In this case, the differentiation variables are specified in the second argument as a list with their numeric positions (see D), and the third argument to fdiff should be a list with the constant values corresponding to each (dummy) variable in the procedure. So, for instance, the correspondence is as in
fdiff⁡f,1,2,2,a,b=D1,2,2⁡f⁡a,b
The method used is a fixed point method. That is, for a given order of derivative, the function f is evaluated at a fixed number of points near the evaluation point x=a,y=b,… at sufficient precision so that all digits in the output are accurate. However, if the function is changing rapidly near the evaluation point, the result may be inaccurate. The optional argument workprec=n where n is a numerical constant > 1 means that the computation will be computed at a working precision of n*Digits precision and the result rounded to Digits precision. The default value for workprec is 1.
The following describes how the differentiation is performed numerically to compute D(f)(a). The Taylor series for f⁡x+h and f⁡x−h about h=0 are
f⁡x+h=f⁡x+h⁢f⁢' ⁢x+h2⁢f⁢''⁢x2+h3⁢f⁢'''⁢x6+h4⁢f⁢''''⁢x24+...⁢1
f⁡x−h=f⁡x−h⁢f⁢' ⁢x+h2⁢f⁢''⁢x2−h3⁢f⁢'''⁢x6+h4⁢f⁢''''⁢x24+...⁢2
To estimate f'⁡x, solve either of the above for f'⁡x, giving a one-sided difference formula, for example, f'x=fx+h−fxh−h2⋅f''x+… with error term h2⁢f''⁡x+...=O⁡h and requiring two function evaluations. Instead, fdiff uses a symmetric difference, taking (1) - (2) to obtain
f⁢' ⁢x=f⁡x+h−f⁡x−h2⁢h−h2⁢f⁢'''⁢x6+...
( * )
which also requires two function evaluations but has an error term h26⁢f'''⁡x+..., which is O⁡h2.
Let n=Digits and e=10−n, that is, e is a machine epsilon.
To compute f'⁡x accurate to n digits, take h=10−n2=e in ( * ). Thus the error=h26⁢f'''⁡x+...=Oh2=O10−n=O⁡e. Note that f⁡x+h2−f⁡x−h2h loses n2 digits of precision. Hence, to obtain the difference with n digits of precision, fdiff must compute f⁡x+h2 and f⁡x−h2 to at least 3⁢n2 digits of precision. The actual values for the precision used by fdiff to compute the linear combination of the f's allow for two guard digits.
For the second derivative, f''⁡x, solving (1) + (2) for f''⁡x one has
f⁢''⁢x=f⁡x+h−2⁢f⁡x+f⁡x−hh2−h2⁢f⁢''''⁢x12+...
with error term O⁡h2 but requiring three function calls. Setting h=10−n2, the error=h212⁢f''''⁡x+...=Oh2=10−n.
For higher order derivatives one may locate the evaluation points symmetrically and equally spaced about x, namely, for even order derivatives, at ...,x−2⁢h,⁢x−h,⁢x,⁢x+h,⁢x+2⁢h,..., and for odd order derivatives, at ...,x−3⁢h⁢2,x−h2,x+h2,x+3⁢h⁢2,... leading to an error term O⁡h2.
Compute cos'⁡1 numerically without using cos'⁡x=−sin⁡x.
fdiff⁡cos⁡x,x=1
−0.8414709848
Compare with the result obtained by first computing that formula:
diff⁡cos⁡x,x
−sin⁡x
evalf⁡eval⁡,x=1
An example where there is no closed formula for the derivative.
fdiff⁡BesselJ⁡a,12,a=13=eval⁡diff⁡BesselJ⁡a,12,a,a=13
−0.8196217810=ⅆⅆaBesselJ⁡a,12a=13|ⅆⅆaBesselJ⁡a,12a=13
Constructions like the right-hand side in the above can be evaluated numerically by passing them as arguments to evalf; they are internally translated into the corresponding call to fdiff (see the previous input line) and computed.
z≔eval⁡diff⁡BesselJ⁡a,12,a,a=13
z≔ⅆⅆaBesselJ⁡a,12a=13|ⅆⅆaBesselJ⁡a,12a=13
evalf⁡z
−0.8196217810
evalf⁡D1⁡BesselJ⁡13,12
You can also use Eval instead of eval and Diff instead of diff, with any combination of them resulting in the same output.
z≔Eval⁡Diff⁡BesselJ⁡a,12,a,a=13
An example with another special function, LegendreP, and a fourth order derivative with respect to two of its parameters (again, the derivative cannot be performed symbolically)
eval⁡diff⁡LegendreP⁡a,b,x,a,a,b,b,a=12,b=32,x=57=fdiff⁡LegendreP⁡a,b,x,a,a,b,b,a=12,b=32,x=75
∂4∂a2∂b2LegendreP⁡a,b,57a=12,b=32|∂4∂a2∂b2LegendreP⁡a,b,57a=12,b=32=−3.553866479
The operational syntax (see D) for this fourth derivative of LegendreP:
D1,1,2,2⁡LegendreP⁡12,32,75=fdiff⁡LegendreP,1,1,2,2,12,32,75
D1,1,2,2⁡LegendreP⁡12,32,75=−3.553866479
This is an example where the function is input as a (recursive) program. The following Maple procedure computes fn⁡x where f is the logistic function f⁡x=a⁢x⁢1−x with parameter a.
g := proc(x,a,n::integer) local y; if n=0 then x else y := g(x,a,n-1); a*y*(1-y) end if; end proc:
g⁡x,a,3
a3⁢x⁢1−x⁢1−a⁢x⁢1−x⁢1−a2⁢x⁢1−x⁢1−a⁢x⁢1−x
degree⁡g⁡x,a,3,x,degree⁡g⁡x,a,3,a
8,7
degree⁡g⁡x,a,4,x,degree⁡g⁡x,a,4,a
16,15
degree⁡g⁡x,a,5,x,degree⁡g⁡x,a,5,a
32,31
Compute the derivative diff(g(x,a,5),x,a) at x=13,a=12.
D2⁡g⁡13,12,5=fdiff⁡g,1,2,13,12,5
D2⁡g⁡13,12,5=0.05826998816
The following is an example where the result of fdiff is inaccurate.
f≔9000⁢x5+1
a≔5000000003
answer≔evalf20⁡eval⁡diff⁡f,x,x=a
answer≔3.4722222222222222222×1037
fdiff⁡f,x=a
3.47200000×1037
The workprec option can be used to increase the working precision used to obtain a more accurate result.
Increase Digits by 10% (1 digit).
fdiff⁡f,x=a,workprec=1.1
3.472220000×1037
Increase Digits by 30% (3 digits).
fdiff⁡f,x=a,workprec=1.3
3.472222200×1037
Increase Digits by 50% (5 digits).
fdiff⁡f,x=a,workprec=1.5
3.472222222×1037
The modified Bessel function of the second kind, BesselK, and a visual comparison between its derivative with respect to the first parameter and the series expansion of it. The visualization is obtained using the plotcompare command.
a≔a:
f≔diff⁡BesselK⁡a,35,a:
The series with respect to a.
f=convert⁡series⁡f,a,compose,diff,polynom
ⅆⅆaBesselK⁡a,35=ⅆⅆt1BesselK⁡t1,35t1=0|ⅆⅆt1BesselK⁡t1,35t1=0+ⅆ2ⅆt12BesselK⁡t1,35t1=0|ⅆ2ⅆt12BesselK⁡t1,35t1=0⁢a+ⅆ3ⅆt13BesselK⁡t1,35t1=0|ⅆ3ⅆt13BesselK⁡t1,35t1=0⁢a22+ⅆ4ⅆt14BesselK⁡t1,35t1=0|ⅆ4ⅆt14BesselK⁡t1,35t1=0⁢a36+ⅆ5ⅆt15BesselK⁡t1,35t1=0|ⅆ5ⅆt15BesselK⁡t1,35t1=0⁢a424+ⅆ6ⅆt16BesselK⁡t1,35t1=0|ⅆ6ⅆt16BesselK⁡t1,35t1=0⁢a5120
The default ranges are: x=−1..1, y=−1..1.
plotsplotcompare⁡
See Also
D
diff
eval
evalf
Download Help Document