fdiff - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


fdiff

compute a numerical approximation for the (partial) derivative of an expression evaluated at a point

 

Calling Sequence

Parameters

Description

Computational Approach

Examples

Calling Sequence

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 )

Parameters

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

Description

• 

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,

fdifffx,y,x,x,y,x=a,y=b=3x2yfx,yx=a,y=b|3x2yfx,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,

evalf3x2yfx,yx=a,y=b|3x2yfx,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:

evalfD1,1,2fa,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

fdifffx,y,x,y,x=a,y=b=fdifffx,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,

fdifffx,x=a;

fdifffx,x,x=a;

fdifffx,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

fdifff,1,2,2,a,b=D1,2,2fa,b

Computational Approach

  

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 fx+h and fxh about h=0 are

fx+h=fx+hf' x+h2f''x2+h3f'''x6+h4f''''x24+...1

fxh=fxhf' x+h2f''x2h3f'''x6+h4f''''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+hfxhh2f''x+ with error term h2f''x+...=Oh and requiring two function evaluations. Instead, fdiff uses a symmetric difference, taking (1) - (2) to obtain

f' x=fx+hfxh2hh2f'''x6+...

( * )

  

which also requires two function evaluations but has an error term h26f'''x+..., which is Oh2.

  

Let n=Digits and e=10n, that is, e is a machine epsilon.

  

To compute f'x accurate to n digits, take h=10n2=e in ( * ). Thus the error=h26f'''x+...=Oh2=O10n=Oe. Note that fx+h2fxh2h loses n2 digits of precision. Hence, to obtain the difference with n digits of precision, fdiff must compute fx+h2 and fxh2 to at least 3n2 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=fx+h2fx+fxhh2h2f''''x12+...

  

with error term Oh2 but requiring three function calls. Setting h=10n2, the error=h212f''''x+...=Oh2=10n.

  

For higher order derivatives one may locate the evaluation points symmetrically and equally spaced about x, namely, for even order derivatives, at ...,x2h,xh,x,x+h,x+2h,..., and for odd order derivatives, at ...,x3h2,xh2,x+h2,x+3h2,... leading to an error term Oh2.

Examples

Compute cos'1 numerically without using cos'x=sinx.

fdiffcosx,x=1

−0.8414709848

(1)

Compare with the result obtained by first computing that formula:

diffcosx,x

sinx

(2)

evalfeval,x=1

−0.8414709848

(3)

An example where there is no closed formula for the derivative.

fdiffBesselJa,12,a=13=evaldiffBesselJa,12,a,a=13

−0.8196217810=ⅆⅆaBesselJa,12a=13|ⅆⅆaBesselJa,12a=13

(4)

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.

zevaldiffBesselJa,12,a,a=13

zⅆⅆaBesselJa,12a=13|ⅆⅆaBesselJa,12a=13

(5)

evalfz

−0.8196217810

(6)

evalfD1BesselJ13,12

−0.8196217810

(7)

You can also use Eval instead of eval and Diff instead of diff, with any combination of them resulting in the same output.

zEvalDiffBesselJa,12,a,a=13

zⅆⅆaBesselJa,12a=13|ⅆⅆaBesselJa,12a=13

(8)

evalfz

−0.8196217810

(9)

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)

evaldiffLegendrePa,b,x,a,a,b,b,a=12,b=32,x=57=fdiffLegendrePa,b,x,a,a,b,b,a=12,b=32,x=75

4a2b2LegendrePa,b,57a=12,b=32|4a2b2LegendrePa,b,57a=12,b=32=−3.553866479

(10)

The operational syntax (see D) for this fourth derivative of LegendreP:

D1,1,2,2LegendreP12,32,75=fdiffLegendreP,1,1,2,2,12,32,75

D1,1,2,2LegendreP12,32,75=−3.553866479

(11)

This is an example where the function is input as a (recursive) program. The following Maple procedure computes fnx where f is the logistic function fx=ax1x 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:

gx,a,3

a3x1x1ax1x1a2x1x1ax1x

(12)

degreegx,a,3,x,degreegx,a,3,a

8,7

(13)

degreegx,a,4,x,degreegx,a,4,a

16,15

(14)

degreegx,a,5,x,degreegx,a,5,a

32,31

(15)

Compute the derivative diff(g(x,a,5),x,a) at x=13,a=12.

D2g13,12,5=fdiffg,1,2,13,12,5

D2g13,12,5=0.05826998816

(16)

The following is an example where the result of fdiff is inaccurate.

f9000x5+1

f9000x5+1

(17)

a5000000003

a5000000003

(18)

answerevalf20evaldifff,x,x=a

answer3.4722222222222222222×1037

(19)

fdifff,x=a

3.47200000×1037

(20)

The workprec option can be used to increase the working precision used to obtain a more accurate result.

Increase Digits by 10% (1 digit).

fdifff,x=a,workprec=1.1

3.472220000×1037

(21)

Increase Digits by 30% (3 digits).

fdifff,x=a,workprec=1.3

3.472222200×1037

(22)

Increase Digits by 50% (5 digits).

fdifff,x=a,workprec=1.5

3.472222222×1037

(23)

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.

aa:

fdiffBesselKa,35,a:

The series with respect to a.

f=convertseriesf,a,compose,diff,polynom

ⅆⅆaBesselKa,35=ⅆⅆt1BesselKt1,35t1=0|ⅆⅆt1BesselKt1,35t1=0+ⅆ2ⅆt12BesselKt1,35t1=0|ⅆ2ⅆt12BesselKt1,35t1=0a+ⅆ3ⅆt13BesselKt1,35t1=0|ⅆ3ⅆt13BesselKt1,35t1=0a22+ⅆ4ⅆt14BesselKt1,35t1=0|ⅆ4ⅆt14BesselKt1,35t1=0a36+ⅆ5ⅆt15BesselKt1,35t1=0|ⅆ5ⅆt15BesselKt1,35t1=0a424+ⅆ6ⅆt16BesselKt1,35t1=0|ⅆ6ⅆt16BesselKt1,35t1=0a5120

(24)

The default ranges are: x=−1..1, y=−1..1.

plotsplotcompare

See Also

D

diff

eval

evalf