D04 Chapter Contents
D04 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentD04BAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

D04BAF calculates a set of derivatives (up to order $14$) of a function at a point with respect to a single variable. A corresponding set of error estimates is also returned. Derivatives are calculated using an extension of the Neville algorithm. This routine differs from D04AAF, in that the abscissae and corresponding function values must be calculated before this routine is called. The abscissae may be generated using D04BBF.

## 2  Specification

 SUBROUTINE D04BAF ( XVAL, FVAL, DER, EREST, IFAIL)
 INTEGER IFAIL REAL (KIND=nag_wp) XVAL(21), FVAL(21), DER(14), EREST(14)

## 3  Description

D04BAF provides a set of approximations:
 $DERj , j=1,2,…,14$
to the derivatives:
 $fj x0 , j=1,2,…,14$
of a real valued function $f\left(x\right)$ at a real abscissa ${x}_{0}$, together with a set of error estimates:
 $ERESTj , j=1,2,…,14$
which hopefully satisfy:
 $DERj - f j x0 < ERESTj , j= 1,2,…,14 .$
The results ${\mathbf{DER}}\left(j\right)$ and ${\mathbf{EREST}}\left(j\right)$ are based on $21$ function values:
 $fx0 , f x0 ± 2i-1 h , i=1,2,…,10 .$
The abscissae $x$ and the corresponding function values $f\left(x\right)$ should be passed into D04BAF as the vectors XVAL and FVAL respectively. The step size $h$ is derived from the abscissae in XVAL. See Section 8 for a discussion of how the derived value of $h$ may affect the results of D04BAF. The order in which the abscissae and function values are stored in XVAL and FVAL is irrelevant, provided that the function value at any given index corresponds to the value of the abscissa at the same index. Abscissae may be automatically generated using D04BBF if desired. For each derivative D04BAF employs an extension of the Neville Algorithm (see Lyness and Moler (1969)) to obtain a selection of approximations.
For example, for odd derivatives, this routine calculates a set of numbers:
 $T k,p,s , p=s,s+1,…,6 , k=0,1,…,9-p$
each of which is an approximation to ${f}^{\left(2s+1\right)}\left({x}_{0}\right)/\left(2s+1\right)!$. A specific approximation ${T}_{\mathit{k},p,s}$ is of polynomial degree $2p+2$ and is based on polynomial interpolation using function values $f\left({x}_{0}±\left(2i-1\right)h\right)$, 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.
For a specified value of $s$, let:
 $Rp = Up - Lp , p=s,s+1,…,6$
where ${U}_{p}=\underset{\mathit{k}}{\mathrm{max}}\phantom{\rule{0.25em}{0ex}}\left({T}_{\mathit{k},p,s}\right)$ and ${L}_{p}=\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 $\stackrel{-}{p}$ be such that ${R}_{\stackrel{-}{\mathit{p}}}=\underset{\mathit{p}}{\mathrm{min}}\phantom{\rule{0.25em}{0ex}}\left({R}_{\mathit{p}}\right)$, for $\mathit{p}=s,\dots ,6$.
This routine returns:
 $DER2s+1 = 1 8-p- × ∑ k=0 9-p- T k, p-, s - Up- - Lp- ⁢ 2s+1 !$
and
 $EREST2s+1 = Rp- × 2s+1 ! × K 2s+1$
where ${K}_{j}$ is a safety factor which has been assigned the values:
 ${K}_{j}=1$, $j\le 9$ ${K}_{j}=1.5$, $j=10,11$ ${K}_{j}=2$ $j\ge 12$,
on the basis of performance statistics.
The even order derivatives are calculated in a precisely analogous manner.

## 4  References

Lyness J N and Moler C B (1969) Generalised Romberg methods for integrals of derivatives Numer. Math. 14 1–14

## 5  Parameters

1:     XVAL($21$) – REAL (KIND=nag_wp) arrayInput
On entry: the abscissae at which the function has been evaluated, as described in Section 3. These can be generated by calling D04BBF. The order of the abscissae is irrelevant.
Constraint: the values in XVAL must span the set $\left\{{x}_{0},{x}_{0}±\left(2\mathit{j}-1\right)h\right\}$, for $\mathit{j}=1,2,\dots ,10$.
2:     FVAL($21$) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{FVAL}}\left(\mathit{j}\right)$ must contain the function value at ${\mathbf{XVAL}}\left(\mathit{j}\right)$, for $\mathit{j}=1,2,\dots ,21$.
3:     DER($14$) – REAL (KIND=nag_wp) arrayOutput
On exit: the 14 derivative estimates.
4:     EREST($14$) – REAL (KIND=nag_wp) arrayOutput
On exit: the 14 error estimates for the derivatives.
5:     IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
Either the derived $h$ is below the acceptable tolerance, or the abscissae in XVAL are not correctly spaced. You must ensure that the values of XVAL cover the set $\left\{{x}_{0},{x}_{0}±\left(2\mathit{j}-1\right)h\right\}$, for $\mathit{j}=1,2,\dots ,10$. The abscissae may be generated, in ascending order, using D04BBF.

## 7  Accuracy

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 8).
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 D04BAF 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 step length $h$ (see Section 8). The only hard and fast rule is that for a given objective function and $h$, 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.

The results depend very critically on the choice of the step length $h$. The overall accuracy is diminished as $h$ becomes small (because of the effect of round-off error) and as $h$ becomes large (because the discretization error also becomes large). If this routine is used four or five times with different values of $h$ one can find a reasonably good value. A process in which the value of $h$ is successively halved (or doubled) is usually quite effective. Experience has shown that in cases in which the Taylor series for the objective function about ${x}_{0}$ has a finite radius of convergence $R$, the choices of $h>R/19$ are not likely to lead to good results. In this case some function values lie outside the circle of convergence.
As mentioned, the order of the abscissae in XVAL does not matter, provided the corresponding values of FVAL are ordered identically. If the abscissae are generated by D04BBF, then they will be in ascending order. In particular, the target abscissa ${x}_{0}$ will be stored in ${\mathbf{XVAL}}\left(11\right)$.

## 9  Example

This example evaluates the derivatives of the polygamma function, calculated using S14AEF, and compares the first $3$ derivatives calculated to those found using S14AEF.

### 9.1  Program Text

Program Text (d04bafe.f90)

None.

### 9.3  Program Results

Program Results (d04bafe.r)