fdiff - compute a numerical approximation for the (partial) derivative of an expression evaluated at a point
|
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 = , where 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 . 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 . 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 .
|
|
The fdiff command is useful when a closed form formula is not known for , 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,
|
|
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 ( 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,
|
|
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:
|
•
|
For simplicity, an alternative syntax is allowed: the second argument, in fdiff( f(x,y), [x,y], {x=a, y=b}) can be omitted, in which case the third argument, indicates, simultaneously, the differentiations to be performed and the evaluation points. For example, these two calls result in the same output
|
|
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,
|
;
;
;
•
|
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
|
|
|
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 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 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.
|
|
( * )
|
|
|
|
which also requires two function evaluations but has an error term , which is .
|
|
For the second derivative, , solving (1) + (2) for one has
|
|
|
Examples
|
|
Compute numerically without using .
>
|
|
| (1) |
Compare with the result obtained by first computing that formula:
>
|
|
| (2) |
>
|
|
| (3) |
An example where there is no closed formula for the derivative.
>
|
|
| (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.
>
|
|
| (5) |
>
|
|
| (6) |
>
|
|
| (7) |
You can also use Eval instead of eval and Diff instead of diff, with any combination of them resulting in the same output.
>
|
|
| (8) |
>
|
|
| (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)
>
|
|
| (10) |
The operational syntax (see D) for this fourth derivative of LegendreP:
>
|
|
| (11) |
This is an example where the function is input as a (recursive) program. The following Maple procedure computes where is the logistic function with parameter .
>
|
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:
|
>
|
|
| (12) |
>
|
|
| (13) |
>
|
|
| (14) |
>
|
|
| (15) |
Compute the derivative diff(g(x,a,5),x,a) at .
>
|
|
| (16) |
The following is an example where the result of fdiff is inaccurate.
>
|
|
| (17) |
>
|
|
| (18) |
>
|
|
| (19) |
>
|
|
| (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).
>
|
|
| (21) |
Increase Digits by 30% (3 digits).
>
|
|
| (22) |
Increase Digits by 50% (5 digits).
>
|
|
| (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.
>
|
|
>
|
|
The series with respect to a.
>
|
|
| (24) |
The default ranges are: , .
>
|
|
|
|