D01 Chapter Contents
D01 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentD01ARF

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

D01ARF computes definite and indefinite integrals over a finite range to a specified relative or absolute accuracy, using the method described in Patterson (1968).

## 2  Specification

 SUBROUTINE D01ARF ( A, B, FUN, RELACC, ABSACC, MAXRUL, IPARM, ACC, ANS, N, ALPHA, IFAIL)
 INTEGER MAXRUL, IPARM, N, IFAIL REAL (KIND=nag_wp) A, B, FUN, RELACC, ABSACC, ACC, ANS, ALPHA(390) EXTERNAL FUN

## 3  Description

D01ARF evaluates definite and indefinite integrals of the form:
 $∫abftdt$
using the method described in Patterson (1968).

### 3.1  Definite Integrals

In this case D01ARF must be called with ${\mathbf{IPARM}}=0$. By linear transformation the integral is changed to
 $I=∫-1 +1Fxdx$
where
 $Fx=b-a2 f b+a+b-ax2$
and is then approximated by an $n$-point quadrature rule
 $I=∑k=1nwkFxk$
where ${w}_{k}$ are the weights and ${x}_{k}$ are the abscissae.
The routine uses a family of nine interlacing rules based on the optimal extension of the three-point Gauss rule. These rules use $1$, $3$, $7$, $15$, $31$, $63$, $127$, $255$ and $511$ points and have respective polynomial integrating degrees $1$, $5$, $11$, $23$, $47$, $95$, $191$, $383$ and $767$. Each rule has the property that the next in sequence includes all the points of its predecessor and has the greatest possible increase in integrating degree.
The integration method is based on the successive application of these rules until the absolute value of the difference of two successive results differs by not more than ABSACC, or relatively by not more than RELACC. The result of the last rule used is taken as the value of the integral (ANS), and the absolute difference of the results of the last two rules used is taken as an estimate of the absolute error (ACC). Due to their interlacing form no integrand evaluations are wasted in passing from one rule to the next.

### 3.2  Indefinite Integrals

Suppose the value of the integral
 $∫cdftdt$
is required for a number of sub-intervals $\left[c,d\right]$, all of which lie in an interval $\left[a,b\right]$.
In this case D01ARF should first be called with the parameter ${\mathbf{IPARM}}=1$ and the interval set to $\left[a,b\right]$. The routine then calculates the integral over $\left[a,b\right]$ and the Legendre expansion of the integrand, using the same integrand values. If the routine is subsequently called with ${\mathbf{IPARM}}=2$ and the interval set to $\left[c,d\right]$, the integral over $\left[c,d\right]$ is calculated by analytical integration of the Legendre expansion, without further evaluations of the integrand.
For the interval $\left[-1,1\right]$ the expansion takes the form
 $Fx=∑i=0∞αiPix$
where ${P}_{i}\left(x\right)$ is the order $i$ Legendre polynomial. Assuming that the integral over the full range $\left[-1,1\right]$ was evaluated to the required accuracy using an $n$-point rule, then the coefficients
 $αi=122i-1∫-1 +1PixFxdx, i=0,1,…,m$
are evaluated by that same rule, up to
 $m=3n- 1/4.$
The accuracy for indefinite integration should be of the same order as that obtained for the definite integral over the full range. The indefinite integrals will be exact when $F\left(x\right)$ is a polynomial of degree $\text{}\le m$.
Patterson T N L (1968) The Optimum addition of points to quadrature formulae Math. Comput. 22 847–856

## 5  Parameters

1:     A – REAL (KIND=nag_wp)Input
On entry: $a$, the lower limit of integration.
2:     B – REAL (KIND=nag_wp)Input
On entry: $b$, the upper limit of integration. It is not necessary that $a.
3:     FUN – REAL (KIND=nag_wp) FUNCTION, supplied by the user.External Procedure
FUN must return the value of the integrand $f$ 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 point in $\left[a,b\right]$ at which the integrand $f$ must be evaluated.
FUN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D01ARF is called. Parameters denoted as Input must not be changed by this procedure.
If ${\mathbf{IPARM}}=2$, FUN is not called.
4:     RELACC – REAL (KIND=nag_wp)Input
On entry: the relative accuracy required. If convergence according to absolute accuracy is required, RELACC should be set to zero (but see also Section 7). If ${\mathbf{RELACC}}<0.0$, its absolute value is used.
If ${\mathbf{IPARM}}=2$, RELACC is not used.
5:     ABSACC – REAL (KIND=nag_wp)Input
On entry: the absolute accuracy required. If convergence according to relative accuracy is required, ABSACC should be set to zero (but see also Section 7). If ${\mathbf{ABSACC}}<0.0$, its absolute value is used.
If ${\mathbf{IPARM}}=2$, ABSACC is not used.
6:     MAXRUL – INTEGERInput
On entry: the maximum number of successive rules that may be used.
Constraint: $1\le {\mathbf{MAXRUL}}\le 9$. If MAXRUL is outside these limits, the value $9$ is assumed.
If ${\mathbf{IPARM}}=2$, MAXRUL is not used.
7:     IPARM – INTEGERInput
On entry: indicates the task to be performed by the routine.
${\mathbf{IPARM}}=0$
Only the definite integral over $\left[a,b\right]$ is evaluated.
${\mathbf{IPARM}}=1$
As well as the definite integral, the expansion of the integrand in Legendre polynomials over $\left[a,b\right]$ is calculated, using the same values of the integrand as used to compute the integral. The expansion coefficients, and some other quantities, are returned in ALPHA for later use in computing indefinite integrals.
${\mathbf{IPARM}}=2$
$f\left(t\right)$ is integrated analytically over $\left[a,b\right]$ using the previously computed expansion, stored in ALPHA. No further evaluations of the integrand are required. The routine must previously have been called with ${\mathbf{IPARM}}=1$ and the interval $\left[a,b\right]$ must lie within that specified for the previous call. In this case only the arguments A, B, IPARM, ANS, ALPHA and IFAIL are used.
Constraint: ${\mathbf{IPARM}}=0$, $1$ or $2$.
8:     ACC – REAL (KIND=nag_wp)Output
On exit: if ${\mathbf{IPARM}}=0$ or $1$, ACC contains the absolute value of the difference between the last two successive estimates of the integral. This may be used as a measure of the accuracy actually achieved.
If ${\mathbf{IPARM}}=2$, ACC is not used.
9:     ANS – REAL (KIND=nag_wp)Output
On exit: the estimated value of the integral.
10:   N – INTEGEROutput
On exit: when ${\mathbf{IPARM}}=0$ or $1$, N contains the number of integrand evaluations used in the calculation of the integral.
If ${\mathbf{IPARM}}=2$, N is not used.
11:   ALPHA($390$) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if ${\mathbf{IPARM}}=2$, ALPHA must contain the coefficients of the Legendre expansions of the integrand, as returned by a previous call of D01ARF with ${\mathbf{IPARM}}=1$ and a range containing the present range.
If ${\mathbf{IPARM}}=0$ or $1$, ALPHA need not be set on entry.
On exit: if ${\mathbf{IPARM}}=1$, the first $m$ elements of ALPHA hold the coefficients of the Legendre expansion of the integrand, and the value of $m$ is stored in ${\mathbf{ALPHA}}\left(390\right)$. ALPHA must not be changed between a call with ${\mathbf{IPARM}}=1$ and subsequent calls with ${\mathbf{IPARM}}=2$.
If ${\mathbf{IPARM}}=2$, the first $m$ elements of ALPHA are unchanged on exit.
12:   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: D01ARF 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$
If ${\mathbf{IPARM}}=0$ or $1$, this indicates that all MAXRUL rules have been used and the integral has not converged to the accuracy requested. In this case ANS contains the last approximation to the integral, and ACC contains the difference between the last two approximations. To check this estimate of the integral, D01ARF could be called again to evaluate
 $∫abftdt as ∫ac ftdt+∫cb ftdt for some ​a
If ${\mathbf{IPARM}}=2$, this indicates failure of convergence during the run with ${\mathbf{IPARM}}=1$ in which the Legendre expansion was created.
${\mathbf{IFAIL}}=2$
On entry, ${\mathbf{IPARM}}\ne 0$, $1$ or $2$
${\mathbf{IFAIL}}=3$
The routine is called with ${\mathbf{IPARM}}=2$ but a previous call with ${\mathbf{IPARM}}=1$ has been omitted or was invoked with an integration interval of length zero.
${\mathbf{IFAIL}}=4$
On entry, with ${\mathbf{IPARM}}=2$, the interval for indefinite integration is not contained within the interval specified when D01ARF was previously called with ${\mathbf{IPARM}}=1$.

## 7  Accuracy

The relative or absolute accuracy required is specified by you in the variables RELACC or ABSACC. D01ARF will terminate whenever either the relative accuracy specified by RELACC or the absolute accuracy specified by ABSACC is reached. One or other of these criteria may be ‘forced’ by setting the parameter for the other to zero. If both RELACC and ABSACC are specified as zero, then the routine uses the value  for RELACC.
If on exit ${\mathbf{IFAIL}}={\mathbf{0}}$, then it is likely that the result is correct to one or other of these accuracies. If on exit ${\mathbf{IFAIL}}={\mathbf{1}}$, then it is likely that neither of the requested accuracies has been reached.
When you have no prior idea of the magnitude of the integral, it is possible that an unreasonable accuracy may be requested, e.g., a relative accuracy for an integral which turns out to be zero, or a small absolute accuracy for an integral which turns out to be very large. Even if failure is reported in such a case, the value of the integral may still be satisfactory. The device of setting the other ‘unused’ accuracy parameter to a small positive value (e.g., ${10}^{-9}$ for an implementation of $11$-digit precision) rather than zero, may prevent excessive calculation in such a situation.
To avoid spurious convergence, it is recommended that relative accuracies larger than about ${10}^{-3}$ be avoided.

The time taken by D01ARF depends on the complexity of the integrand and the accuracy required.
This routine uses the Patterson method over the whole integration interval and should therefore be suitable for well behaved functions. However, for very irregular functions it would be more efficient to submit the differently behaved regions separately for integration.

## 9  Example

This example evaluates the following integrals
(i) Definite integral only $\left({\mathbf{IPARM}}=0\right)$ for
 $∫0141+x2 dx ABSACC=10-5.$
(ii) Definite integral together with expansion coefficients $\left({\mathbf{IPARM}}=1\right)$ for
 $∫12x8dx ABSACC=10-5.$
(iii) Indefinite integral using previous expansion $\left({\mathbf{IPARM}}=2\right)$ for
 $∫1.21.8x8dx ABSACC=10-5.$

### 9.1  Program Text

Program Text (d01arfe.f90)

None.

### 9.3  Program Results

Program Results (d01arfe.r)