NAG Library Routine Document
D01AHF
1 Purpose
D01AHF computes a definite integral over a finite range to a specified relative accuracy using a method described by Patterson.
2 Specification
REAL (KIND=nag_wp) D01AHF 
INTEGER 
NPTS, NLIMIT, IFAIL 
REAL (KIND=nag_wp) 
A, B, EPSR, RELERR, F 
EXTERNAL 
F 

3 Description
D01AHF computes a definite integral of the form
The method uses as its basis a family of interlacing high precision rules (see
Patterson (1968)) using
$1$,
$3$,
$7$,
$15$,
$31$,
$63$,
$127$ and
$255$ nodes. Initially the family is applied in sequence to the integrand. When two successive rules differ relatively by less than the required relative accuracy, the last rule used is taken as the value of the integral and the operation is regarded as successful. If all rules in the family have been applied unsuccessfully, subdivision is invoked. The subdivision strategy is as follows. The interval under scrutiny is divided into two subintervals (not always equal). The basic family is then applied to the first subinterval. If the required accuracy is not obtained, the interval is stored for future examination (see
${\mathbf{IFAIL}}={\mathbf{2}}$) and the second subinterval is examined. Should the basic family again be unsuccessful, then the subinterval is further subdivided and the whole process repeated. Successful integrations are accumulated as the partial value of the integral. When all possible successful integrations have been completed, those previously unsuccessful subintervals placed in store are examined.
A large number of refinements are incorporated to improve the performance. Some of these are:
(a) 
The rate of convergence of the basic family is monitored and used to make a decision to abort and subdivide before the full sequence has been applied. 
(b) 
The $\epsilon $algorithm is applied to the basic results in an attempt to increase the convergence rate. See Wynn (1956). 
(c) 
An attempt is made to detect sharp end point peaks and singularities in each subinterval and to apply appropriate transformations to smooth the integrand. This consideration is also used to select interval sizes in the subdivision process. 
(d) 
The relative accuracy sought in each subinterval is adjusted in accordance with its likely contribution to the total integral. 
(e) 
Random transformations of the integrand are applied to improve reliability in some instances. 
4 References
Patterson T N L (1968) The Optimum addition of points to quadrature formulae Math. Comput. 22 847–856
Wynn P (1956) On a device for computing the ${e}_{m}\left({S}_{n}\right)$ transformation Math. Tables Aids Comput. 10 91–96
5 Parameters
 1: $\mathrm{A}$ – REAL (KIND=nag_wp)Input

On entry: $a$, the lower limit of integration.
 2: $\mathrm{B}$ – REAL (KIND=nag_wp)Input

On entry: $b$, the upper limit of integration. It is not necessary that $a<b$.
 3: $\mathrm{EPSR}$ – REAL (KIND=nag_wp)Input

On entry: the relative accuracy required.
Constraint:
${\mathbf{EPSR}}>0.0$.
 4: $\mathrm{NPTS}$ – INTEGEROutput

On exit: the number of function evaluations used in the calculation of the integral.
 5: $\mathrm{RELERR}$ – REAL (KIND=nag_wp)Output

On exit: a rough estimate of the relative error achieved.
 6: $\mathrm{F}$ – REAL (KIND=nag_wp) FUNCTION, supplied by the user.External Procedure

F must return the value of the integrand
$f$ at a given point.
The specification of
F is:
 1: $\mathrm{X}$ – REAL (KIND=nag_wp)Input

On entry: the point at which the integrand $f$ must be evaluated.
F must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D01AHF is called. Parameters denoted as
Input must
not be changed by this procedure.
 7: $\mathrm{NLIMIT}$ – INTEGERInput

On entry: a limit to the number of function evaluations. If ${\mathbf{NLIMIT}}\le 0$, the routine uses a default limit of $10000$.
 8: $\mathrm{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, because for this routine the values of the output parameters may be useful even if
${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is
$1$.
When the value $\mathbf{1}\text{ or}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).
Note: D01AHF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
 ${\mathbf{IFAIL}}=1$

The integral has not converged to the accuracy requested. It may be worthwhile to try increasing
NLIMIT.
 ${\mathbf{IFAIL}}=2$

Too many unsuccessful levels of subdivision have been invoked.
 ${\mathbf{IFAIL}}=3$

On entry,  ${\mathbf{EPSR}}\le 0.0$. 
 ${\mathbf{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.
 ${\mathbf{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.
 ${\mathbf{IFAIL}}=999$
Dynamic memory allocation failed.
See
Section 3.6 in the Essential Introduction for further information.
When ${\mathbf{IFAIL}}={\mathbf{1}}$ or ${\mathbf{2}}$ a result may be obtained by continuing without further subdivision, but this is likely to be inaccurate.
7 Accuracy
The relative accuracy required is specified by you in the variable
EPSR. The routine will terminate whenever the relative accuracy specified by
EPSR is judged to have been reached.
If on exit, ${\mathbf{IFAIL}}={\mathbf{0}}$, then it is most likely that the result is correct to the specified accuracy. If, on exit, ${\mathbf{IFAIL}}={\mathbf{1}}$ or ${\mathbf{2}}$, then it is likely that the specified accuracy has not been reached.
RELERR is a rough estimate of the relative error achieved. It is a byproduct of the computation and is not used to effect the termination of the routine. The outcome of the integration must be judged by the value of
IFAIL.
8 Parallelism and Performance
Not applicable.
The time taken by D01AHF depends on the complexity of the integrand and the accuracy required.
10 Example
This example evaluates the integral to a requested relative accuracy of
${10}^{5}$
10.1 Program Text
Program Text (d01ahfe.f90)
10.2 Program Data
Program Data (d01ahfe.d)
10.3 Program Results
Program Results (d01ahfe.r)