D01 Chapter Contents
D01 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentD01GBF

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

D01GBF returns an approximation to the integral of a function over a hyper-rectangular region, using a Monte–Carlo method. An approximate relative error estimate is also returned. This routine is suitable for low accuracy work.

## 2  Specification

 SUBROUTINE D01GBF ( NDIM, A, B, MINCLS, MAXCLS, FUNCTN, EPS, ACC, LENWRK, WRKSTR, FINEST, IFAIL)
 INTEGER NDIM, MINCLS, MAXCLS, LENWRK, IFAIL REAL (KIND=nag_wp) A(NDIM), B(NDIM), FUNCTN, EPS, ACC, WRKSTR(LENWRK), FINEST EXTERNAL FUNCTN

## 3  Description

D01GBF uses an adaptive Monte–Carlo method based on the algorithm described in Lautrup (1971). It is implemented for integrals of the form:
 $∫ a1 b1 ∫ a2 b2 ⋯ ∫ an bn f x1,x2,…,xn dxn ⋯ dx2 dx1 .$
Upon entry, unless LENWRK has been set to the minimum value $10×{\mathbf{NDIM}}$, the routine subdivides the integration region into a number of equal volume subregions. Inside each subregion the integral and the variance are estimated by means of pseudorandom sampling. All contributions are added together to produce an estimate for the whole integral and total variance. The variance along each coordinate axis is determined and the routine uses this information to increase the density and change the widths of the sub-intervals along each axis, so as to reduce the total variance. The total number of subregions is then increased by a factor of two and the program recycles for another iteration. The program stops when a desired accuracy has been reached or too many integral evaluations are needed for the next cycle.

## 4  References

Lautrup B (1971) An adaptive multi-dimensional integration procedure Proc. 2nd Coll. Advanced Methods in Theoretical Physics, Marseille

## 5  Parameters

1:     NDIM – INTEGERInput
On entry: $n$, the number of dimensions of the integral.
Constraint: ${\mathbf{NDIM}}\ge 1$.
2:     A(NDIM) – REAL (KIND=nag_wp) arrayInput
On entry: the lower limits of integration, ${a}_{i}$, for $\mathit{i}=1,2,\dots ,n$.
3:     B(NDIM) – REAL (KIND=nag_wp) arrayInput
On entry: the upper limits of integration, ${b}_{i}$, for $\mathit{i}=1,2,\dots ,n$.
4:     MINCLS – INTEGERInput/Output
On entry: must be set
• either to the minimum number of integrand evaluations to be allowed, in which case ${\mathbf{MINCLS}}\ge 0$;
• or to a negative value. In this case, the routine assumes that a previous call had been made with the same parameters NDIM, A and B and with either the same integrand (in which case D01GBF continues calculation) or a similar integrand (in which case D01GBF begins the calculation with the subdivision used in the last iteration of the previous call). See also WRKSTR.
On exit: contains the number of integrand evaluations actually used by D01GBF.
5:     MAXCLS – INTEGERInput
On entry: the maximum number of integrand evaluations to be allowed. In the continuation case this is the number of new integrand evaluations to be allowed. These counts do not include zero integrand values.
Constraints:
• ${\mathbf{MAXCLS}}>{\mathbf{MINCLS}}$;
• ${\mathbf{MAXCLS}}\ge 4×\left({\mathbf{NDIM}}+1\right)$.
6:     FUNCTN – REAL (KIND=nag_wp) FUNCTION, supplied by the user.External Procedure
FUNCTN must return the value of the integrand $f$ at a given point.
The specification of FUNCTN is:
 FUNCTION FUNCTN ( NDIM, X)
 REAL (KIND=nag_wp) FUNCTN
 INTEGER NDIM REAL (KIND=nag_wp) X(NDIM)
1:     NDIM – INTEGERInput
On entry: $n$, the number of dimensions of the integral.
2:     X(NDIM) – REAL (KIND=nag_wp) arrayInput
On entry: the coordinates of the point at which the integrand $f$ must be evaluated.
FUNCTN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D01GBF is called. Parameters denoted as Input must not be changed by this procedure.
7:     EPS – REAL (KIND=nag_wp)Input
On entry: the relative accuracy required.
Constraint: ${\mathbf{EPS}}\ge 0.0$.
8:     ACC – REAL (KIND=nag_wp)Output
On exit: the estimated relative accuracy of FINEST.
9:     LENWRK – INTEGERInput
On entry: the dimension of the array WRKSTR as declared in the (sub)program from which D01GBF is called.
For maximum efficiency, LENWRK should be about
 $3×NDIM×MAXCLS/41/NDIM+7×NDIM.$
If LENWRK is given the value $10×{\mathbf{NDIM}}$ then the subroutine uses only one iteration of a crude Monte–Carlo method with MAXCLS sample points.
Constraint: ${\mathbf{LENWRK}}\ge 10×{\mathbf{NDIM}}$.
10:   WRKSTR(LENWRK) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if ${\mathbf{MINCLS}}<0$, WRKSTR must be unchanged from the previous call of D01GBF – except that for a new integrand ${\mathbf{WRKSTR}}\left({\mathbf{LENWRK}}\right)$ must be set to $0.0$. See also MINCLS.
On exit: contains information about the current sub-interval structure which could be used in later calls of D01GBF. In particular, ${\mathbf{WRKSTR}}\left(j\right)$ gives the number of sub-intervals used along the $j$th coordinate axis.
11:   FINEST – REAL (KIND=nag_wp)Input/Output
On entry: must be unchanged from a previous call to D01GBF.
On exit: the best estimate obtained for the integral.
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).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{NDIM}}<1$, or ${\mathbf{MINCLS}}\ge {\mathbf{MAXCLS}}$, or ${\mathbf{LENWRK}}<10×{\mathbf{NDIM}}$, or ${\mathbf{MAXCLS}}<4×\left({\mathbf{NDIM}}+1\right)$, or ${\mathbf{EPS}}<0.0$.
${\mathbf{IFAIL}}=2$
MAXCLS was too small for D01GBF to obtain the required relative accuracy EPS. In this case D01GBF returns a value of FINEST with estimated relative error ACC, but ACC will be greater than EPS. This error exit may be taken before MAXCLS nonzero integrand evaluations have actually occurred, if the routine calculates that the current estimates could not be improved before MAXCLS was exceeded.

## 7  Accuracy

A relative error estimate is output through the parameter ACC. The confidence factor is set so that the actual error should be less than ACC 90% of the time. If you want a higher confidence level then a smaller value of EPS should be used.

The running time for D01GBF will usually be dominated by the time used to evaluate FUNCTN, so the maximum time that could be used is approximately proportional to MAXCLS.
For some integrands, particularly those that are poorly behaved in a small part of the integration region, D01GBF may terminate with a value of ACC which is significantly smaller than the actual relative error. This should be suspected if the returned value of MINCLS is small relative to the expected difficulty of the integral. Where this occurs, D01GBF should be called again, but with a higher entry value of MINCLS (e.g., twice the returned value) and the results compared with those from the previous call.
The exact values of FINEST and ACC on return will depend (within statistical limits) on the sequence of random numbers generated within D01GBF by calls to G05SAF. Separate runs will produce identical answers.

## 9  Example

This example calculates the integral
 $∫01∫01∫01∫014x1x3exp2x1x3 1+x2+x4 2dx1dx2dx3dx4=0.575364.$

### 9.1  Program Text

Program Text (d01gbfe.f90)

None.

### 9.3  Program Results

Program Results (d01gbfe.r)