d04aaf calculates a set of derivatives (up to order $14$) of a function of one real variable at a point, together with a corresponding set of error estimates, using an extension of the Neville algorithm.
You must provide the value of ${x}_{0}$, a value of $n$ (which is reduced to $14$ should it exceed $14$), a subroutine which evaluates $f\left(x\right)$ for all real $x$, and a step length $h$. The results ${\mathbf{der}}\left(j\right)$ and ${\mathbf{erest}}\left(j\right)$ are based on $21$ function values:
Internally d04aaf calculates the odd order derivatives and the even order derivatives separately. There is an option you can use for restricting the calculation to only odd (or even) order derivatives. For each derivative the routine employs an extension of the Neville Algorithm (see Lyness and Moler (1969)) to obtain a selection of approximations.
For example, for odd derivatives, based on $20$ function values, d04aaf calculates a set of numbers:
each of which is an approximation to ${f}^{(2s+1)}\left({x}_{0}\right)/(2s+1)!$. A specific approximation ${T}_{\mathit{k},p,s}$ is of polynomial degree $2p+2$ and is based on polynomial interpolation using function values $f({x}_{0}\pm (2i-1)h)$, for $\mathit{k}=\mathit{k},\dots ,\mathit{k}+p$. In the absence of round-off error, the better approximations would be associated with the larger values of $p$ and of $k$. However, round-off error in function values has an increasingly contaminating effect for successively larger values of $p$. This routine proceeds to make a judicious choice between all the approximations in the following way.
where ${U}_{p}={\displaystyle \underset{\mathit{k}}{\mathrm{max}}}\phantom{\rule{0.25em}{0ex}}\left({T}_{\mathit{k},p,s}\right)$ and ${L}_{p}={\displaystyle \underset{\mathit{k}}{\mathrm{min}}}\phantom{\rule{0.25em}{0ex}}\left({T}_{\mathit{k},p,s}\right)$, for $\mathit{k}=0,1,\dots ,9-p$, and let $\overline{\mathit{p}}$ be such that ${R}_{\overline{\mathit{p}}}={\displaystyle \underset{\mathit{p}}{\mathrm{min}}}\phantom{\rule{0.25em}{0ex}}\left({R}_{\mathit{p}}\right)$, for $\mathit{p}=s,\dots ,6$.
Only odd order derivatives up to order $\mathrm{min}\phantom{\rule{0.125em}{0ex}}(-{\mathbf{nder}},13)$ are calculated.
3: $\mathbf{hbase}$ – Real (Kind=nag_wp)Input
On entry: the initial step length which may be positive or negative. For advice on the choice of hbase see Section 9.
Constraint:
${\mathbf{hbase}}\ne 0.0$.
4: $\mathbf{der}\left(14\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: ${\mathbf{der}}\left(j\right)$ contains an approximation to the $j$th derivative of $f\left(x\right)$ at $x={\mathbf{xval}}$, so long as the $j$th derivative is one of those requested by you when specifying nder. For other values of $j$, ${\mathbf{der}}\left(j\right)$ is unused.
5: $\mathbf{erest}\left(14\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: an estimate of the absolute error in the corresponding result ${\mathbf{der}}\left(j\right)$ so long as the $j$th derivative is one of those requested by you when specifying nder. The sign of ${\mathbf{erest}}\left(j\right)$ is positive unless the result ${\mathbf{der}}\left(j\right)$ is questionable. It is set negative when $\left|{\mathbf{der}}\left(j\right)\right|<\left|{\mathbf{erest}}\left(j\right)\right|$ or when for some other reason there is doubt about the validity of the result ${\mathbf{der}}\left(j\right)$ (see Section 6). For other values of $j$, ${\mathbf{erest}}\left(j\right)$ is unused.
6: $\mathbf{f}$ – real (Kind=nag_wp) Function, supplied by the user.External Procedure
f must evaluate the function $f\left(x\right)$ at a specified point.
If you have equally spaced tabular data, the following information may be useful:
(i)in any call of d04aaf the only values of $x$ for which $f\left(x\right)$ will be required are $x={\mathbf{xval}}$ and
$x={\mathbf{xval}}\pm (2\mathit{j}-1){\mathbf{hbase}}$, for $\mathit{j}=1,2,\dots ,10$; and
(ii)$f\left({x}_{0}\right)$ is always computed, but it is disregarded when only odd order derivatives are required.
f must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which d04aaf is called. Arguments denoted as Input must not be changed by this procedure.
Note:f should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d04aaf. If your code inadvertently does return any NaNs or infinities, d04aaf is likely to produce unexpected results.
7: $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $\mathrm{-1}$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $\mathrm{-1}$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $\mathrm{-1}$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).
6Error Indicators and Warnings
If on entry ${\mathbf{ifail}}=0$ or $\mathrm{-1}$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
Note: if ifail has a value zero on exit then d04aaf has terminated successfully, but before any use is made of a derivative ${\mathbf{der}}\left(j\right)$ the value of ${\mathbf{erest}}\left(j\right)$ must be checked.
${\mathbf{ifail}}=1$
On entry, ${\mathbf{hbase}}=0.0$.
Constraint: ${\mathbf{hbase}}\ne 0.0$.
On entry, ${\mathbf{nder}}=0$.
Constraint: ${\mathbf{nder}}\ne 0$.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please
contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.
7Accuracy
The accuracy of the results is problem dependent. An estimate of the accuracy of each result ${\mathbf{der}}\left(j\right)$ is returned in ${\mathbf{erest}}\left(j\right)$ (see Sections 3, 5 and 9).
A basic feature of any floating-point routine for numerical differentiation based on real function values on the real axis is that successively higher order derivative approximations are successively less accurate. It is expected that in most cases ${\mathbf{der}}\left(14\right)$ will be unusable. As an aid to this process, the sign of ${\mathbf{erest}}\left(j\right)$ is set negative when the estimated absolute error is greater than the approximate derivative itself, i.e., when the approximate derivative may be so inaccurate that it may even have the wrong sign. It is also set negative in some other cases when information available to the routine indicates that the corresponding value of ${\mathbf{der}}\left(j\right)$ is questionable.
The actual values in erest depend on the accuracy of the function values, the properties of the machine arithmetic, the analytic properties of the function being differentiated and the user-supplied step length hbase (see Section 9). The only hard and fast rule is that for a given ${\mathbf{f}}\left({\mathbf{xval}}\right)$ and hbase, the values of ${\mathbf{erest}}\left(j\right)$ increase with increasing $j$. The limit of $14$ is dictated by experience. Only very rarely can one obtain meaningful approximations for higher order derivatives on conventional machines.
8Parallelism and Performance
Background information to multithreading can be found in the Multithreading documentation.
d04aaf is not threaded in any implementation.
9Further Comments
The time taken by d04aaf depends on the time spent for function evaluations. Otherwise the time is roughly equivalent to that required to evaluate the function $21$ times and calculate a finite difference table having about $200$ entries in total.
The results depend very critically on the choice of the user-supplied step length hbase. The overall accuracy is diminished as hbase becomes small (because of the effect of round-off error) and as hbase becomes large (because the discretization error also becomes large). If the routine is used four or five times with different values of hbase one can find a reasonably good value. A process in which the value of hbase is successively halved (or doubled) is usually quite effective. Experience has shown that in cases in which the Taylor series for ${\mathbf{f}}\left({\mathbf{x}}\right)$ about xval has a finite radius of convergence $R$, the choices of ${\mathbf{hbase}}>R/19$ are not likely to lead to good results. In this case some function values lie outside the circle of convergence.
10Example
This example evaluates the odd-order derivatives of the function:
$$f\left(x\right)=\frac{1}{2}{e}^{2x-1}$$
up to order $7$ at the point $x=\frac{1}{2}$. Several different values of hbase are used, to illustrate that:
(i)extreme choices of hbase, either too large or too small, yield poor results;
(ii)the quality of these results is adequately indicated by the values of erest.