# NAG CL Interfaced04bac (rcomm)

Settings help

CL Name Style:

## 1Purpose

d04bac 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 function differs from d04aac, in that the abscissae and corresponding function values must be calculated before this function is called. The abscissae may be generated using d04bbc.

## 2Specification

 #include
 void d04bac (const double xval[], const double fval[], double der[], double erest[], NagError *fail)
The function may be called by the names: d04bac, nag_numdiff_rcomm or nag_numdiff_1d_real_eval.

## 3Description

d04bac provides a set of approximations:
 $der[j-1] , j=1,2,…,14$
to the derivatives:
 $f(j) (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:
 $erest[j-1] , j=1,2,…,14$
which hopefully satisfy:
 $|der[j-1]- f (j) (x0)| < erest[j-1] , j= 1,2,…,14 .$
The results ${\mathbf{der}}\left[j-1\right]$ and ${\mathbf{erest}}\left[j-1\right]$ are based on $21$ function values:
 $f(x0) , 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 d04bac as the vectors xval and fval respectively. The step size $h$ is derived from the abscissae in xval. See Section 9 for a discussion of how the derived value of $h$ may affect the results of d04bac. 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 d04bbc if desired. For each derivative d04bac employs an extension of the Neville Algorithm (see Lyness and Moler (1969)) to obtain a selection of approximations.
For example, for odd derivatives, this function 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 function 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 $\overline{p}$ be such that ${R}_{\overline{\mathit{p}}}=\underset{\mathit{p}}{\mathrm{min}}\phantom{\rule{0.25em}{0ex}}\left({R}_{\mathit{p}}\right)$, for $\mathit{p}=s,\dots ,6$.
This function returns:
 $der[2s] = 1 8-p¯ × { ∑ k=0 9-p¯ T k, p¯, s -Up¯-Lp¯} ⁢ (2s+1) !$
and
 $erest[2s] = 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.
Lyness J N and Moler C B (1969) Generalised Romberg methods for integrals of derivatives Numer. Math. 14 1–14

## 5Arguments

1: $\mathbf{xval}\left[21\right]$const double Input
On entry: the abscissae at which the function has been evaluated, as described in Section 3. These can be generated by calling d04bbc. 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: $\mathbf{fval}\left[21\right]$const double Input
On entry: ${\mathbf{fval}}\left[\mathit{j}-1\right]$ must contain the function value at ${\mathbf{xval}}\left[\mathit{j}-1\right]$, for $\mathit{j}=1,2,\dots ,21$.
3: $\mathbf{der}\left[14\right]$double Output
On exit: the $14$ derivative estimates.
4: $\mathbf{erest}\left[14\right]$double Output
On exit: the $14$ error estimates for the derivatives.
5: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
On entry, argument $⟨\mathit{\text{value}}⟩$ had an illegal value.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_SPACING_INCORRECT
On entry, the values of xval are not correctly spaced.
Derived $h=⟨\mathit{\text{value}}⟩$.
NE_TOO_SMALL
The derived $h$ is below tolerance.
Derived $h>⟨\mathit{\text{value}}⟩$ is required. Derived $h=⟨\mathit{\text{value}}⟩$.

## 7Accuracy

The accuracy of the results is problem dependent. An estimate of the accuracy of each result ${\mathbf{der}}\left[j-1\right]$ is returned in ${\mathbf{erest}}\left[j-1\right]$ (see Sections 3, 5 and 9).
A basic feature of any floating-point function 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[13\right]$ will be unusable. As an aid to this process, the sign of ${\mathbf{erest}}\left[j-1\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 d04bac indicates that the corresponding value of ${\mathbf{der}}\left[j-1\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 9). The only hard and fast rule is that for a given objective function and $h$, the values of ${\mathbf{erest}}\left[j-1\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

d04bac is not threaded in any implementation.

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 function 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 d04bbc, then they will be in ascending order. In particular, the target abscissa ${x}_{0}$ will be stored in ${\mathbf{xval}}\left[10\right]$.

## 10Example

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

### 10.1Program Text

Program Text (d04bace.c)

None.

### 10.3Program Results

Program Results (d04bace.r)