D04AAF (PDF version)
D04 Chapter Contents
D04 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

D04AAF

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.

 Contents

    1  Purpose
    7  Accuracy

1  Purpose

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.

2  Specification

SUBROUTINE D04AAF ( XVAL, NDER, HBASE, DER, EREST, FUN, IFAIL)
INTEGER  NDER, IFAIL
REAL (KIND=nag_wp)  XVAL, HBASE, DER(14), EREST(14), FUN
EXTERNAL  FUN

3  Description

D04AAF provides a set of approximations:
DERj,  j=1,2,,n  
to the derivatives:
f j x0,   j= 1,2,,n  
of a real valued function fx at a real abscissa x0, together with a set of error estimates:
ERESTj,  j=1,2,,n  
which hopefully satisfy:
DERj-f j x0<ERESTj,   j= 1,2,,n.  
You must provide the value of x0, a value of n (which is reduced to 14 should it exceed 14), a subroutine which evaluates fx for all real x, and a step length h. The results DERj and ERESTj are based on 21 function values:
fx0,fx0±2i-1h,  i=1,2,,10.  
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:
Tk,p,s,   p=s,s+ 1,,6,   k= 0,1,,9-p  
each of which is an approximation to f 2s+1 x0/2s+1!. A specific approximation Tk,p,s is of polynomial degree 2p+2 and is based on polynomial interpolation using function values fx0±2i-1h, for k=k,,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 Up = maxk Tk,p,s  and Lp = mink Tk,p,s , for k=0,1,,9-p, and let p- be such that Rp- = minp Rp , for p=s,,6.
The 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 Kj is a safety factor which has been assigned the values:
Kj=1, j9
Kj=1.5, j=10,11
Kj=2 j12,
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 (1966) van der Monde systems and numerical differentiation Numer. Math. 8 458–464
Lyness J N and Moler C B (1969) Generalised Romberg methods for integrals of derivatives Numer. Math. 14 1–14

5  Parameters

1:     XVAL – REAL (KIND=nag_wp)Input
On entry: the point at which the derivatives are required, x0.
2:     NDER – INTEGERInput
On entry: must be set so that its absolute value is the highest order derivative required.
NDER>0
All derivatives up to order minNDER,14 are calculated.
NDER<0 and NDER is even
Only even order derivatives up to order min-NDER,14 are calculated.
NDER<0 and NDER is odd
Only odd order derivatives up to order min-NDER,13 are calculated.
3:     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: HBASE0.0.
4:     DER14 – REAL (KIND=nag_wp) arrayOutput
On exit: DERj contains an approximation to the jth derivative of fx at x=XVAL, so long as the jth derivative is one of those requested by you when specifying NDER. For other values of j, DERj is unused.
5:     EREST14 – REAL (KIND=nag_wp) arrayOutput
On exit: an estimate of the absolute error in the corresponding result DERj so long as the jth derivative is one of those requested by you when specifying NDER. The sign of ERESTj is positive unless the result DERj is questionable. It is set negative when DERj<ERESTj or when for some other reason there is doubt about the validity of the result DERj (see Section 6). For other values of j, ERESTj is unused.
6:     FUN – REAL (KIND=nag_wp) FUNCTION, supplied by the user.External Procedure
FUN must evaluate the function fx at a specified point.
The specification of FUN is:
FUNCTION FUN ( X)
REAL (KIND=nag_wp) FUN
REAL (KIND=nag_wp)  X
1:     X – REAL (KIND=nag_wp)Input
On entry: the value of the argument x.
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 fx will be required are x=XVAL and x=XVAL±2j-1HBASE, for j=1,2,,10; and
(ii) fx0 is always computed, but it is disregarded when only odd order derivatives are required.
FUN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D04AAF is called. Parameters denoted as Input must not be changed by this procedure.
7:     IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ 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​ 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 -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
IFAIL=1
On entry,NDER=0,
orHBASE=0.0.
IFAIL=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.
If IFAIL has a value zero on exit then D04AAF has terminated successfully, but before any use is made of a derivative DERj the value of ERESTj must be checked.

7  Accuracy

The accuracy of the results is problem dependent. An estimate of the accuracy of each result DERj is returned in ERESTj (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 DER14 will be unusable. As an aid to this process, the sign of ERESTj 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 DERj 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 FUNXVAL and HBASE, the values of ERESTj 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.

8  Parallelism and Performance

Not applicable.

9  Further 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 FUNX about XVAL has a finite radius of convergence R, the choices of HBASE>R/19 are not likely to lead to good results. In this case some function values lie outside the circle of convergence.

10  Example

This example evaluates the odd-order derivatives of the function:
fx = 12 e 2x-1  
up to order 7 at the point x=12 . 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.

10.1  Program Text

Program Text (d04aafe.f90)

10.2  Program Data

None.

10.3  Program Results

Program Results (d04aafe.r)


D04AAF (PDF version)
D04 Chapter Contents
D04 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015