Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

## Purpose

nag_quad_md_mcarlo (d01gb) 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 function is suitable for low accuracy work.

## Syntax

[mincls, acc, wrkstr, finest, ifail] = d01gb(a, b, mincls, maxcls, functn, eps, wrkstr, finest, 'ndim', ndim, 'lenwrk', lenwrk)
[mincls, acc, wrkstr, finest, ifail] = nag_quad_md_mcarlo(a, b, mincls, maxcls, functn, eps, wrkstr, finest, 'ndim', ndim, 'lenwrk', lenwrk)

## Description

nag_quad_md_mcarlo (d01gb) uses an adaptive Monte–Carlo method based on the algorithm described in Lautrup (1971). It is implemented for integrals of the form:
 b1 b2 bn ∫ ∫ ⋯ ∫ f(x1,x2, … ,xn)dxn ⋯ dx2dx1. a1 a2 an
$∫ 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 × ndim$10×{\mathbf{ndim}}$, the function 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 function 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.

## References

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

## Parameters

### Compulsory Input Parameters

1:     a(ndim) – double array
ndim, the dimension of the array, must satisfy the constraint ndim1${\mathbf{ndim}}\ge 1$.
The lower limits of integration, ai${a}_{i}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
2:     b(ndim) – double array
ndim, the dimension of the array, must satisfy the constraint ndim1${\mathbf{ndim}}\ge 1$.
The upper limits of integration, bi${b}_{i}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
3:     mincls – int64int32nag_int scalar
Must be set
• either to the minimum number of integrand evaluations to be allowed, in which case mincls0${\mathbf{mincls}}\ge 0$;
• or to a negative value. In this case, the function 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 nag_quad_md_mcarlo (d01gb) continues calculation) or a similar integrand (in which case nag_quad_md_mcarlo (d01gb) begins the calculation with the subdivision used in the last iteration of the previous call). See also wrkstr.
4:     maxcls – int64int32nag_int scalar
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}}$;
• maxcls4 × (ndim + 1)${\mathbf{maxcls}}\ge 4×\left({\mathbf{ndim}}+1\right)$.
5:     functn – function handle or string containing name of m-file
functn must return the value of the integrand f$f$ at a given point.
[result] = functn(ndim, x)

Input Parameters

1:     ndim – int64int32nag_int scalar
n$n$, the number of dimensions of the integral.
2:     x(ndim) – double array
The coordinates of the point at which the integrand f$f$ must be evaluated.

Output Parameters

1:     result – double scalar
The result of the function.
6:     eps – double scalar
The relative accuracy required.
Constraint: eps0.0${\mathbf{eps}}\ge 0.0$.
7:     wrkstr(lenwrk) – double array
lenwrk, the dimension of the array, must satisfy the constraint lenwrk10 × ndim${\mathbf{lenwrk}}\ge 10×{\mathbf{ndim}}$.
If mincls < 0${\mathbf{mincls}}<0$, wrkstr must be unchanged from the previous call of nag_quad_md_mcarlo (d01gb) – except that for a new integrand ${\mathbf{wrkstr}}\left({\mathbf{lenwrk}}\right)$ must be set to 0.0$0.0$. See also mincls.
8:     finest – double scalar
Must be unchanged from a previous call to nag_quad_md_mcarlo (d01gb).

### Optional Input Parameters

1:     ndim – int64int32nag_int scalar
Default: The dimension of the arrays a, b. (An error is raised if these dimensions are not equal.)
n$n$, the number of dimensions of the integral.
Constraint: ndim1${\mathbf{ndim}}\ge 1$.
2:     lenwrk – int64int32nag_int scalar
Default: The dimension of the array wrkstr.
The dimension of the array wrkstr as declared in the (sub)program from which nag_quad_md_mcarlo (d01gb) is called.
For maximum efficiency, lenwrk should be about
 3 × ndim × (maxcls / 4)1 / ndim + 7 × ndim. $3×ndim×(maxcls/4)1/ndim+7×ndim.$
If lenwrk is given the value 10 × ndim$10×{\mathbf{ndim}}$ then the function uses only one iteration of a crude Monte–Carlo method with maxcls sample points.
Constraint: lenwrk10 × ndim${\mathbf{lenwrk}}\ge 10×{\mathbf{ndim}}$.

None.

### Output Parameters

1:     mincls – int64int32nag_int scalar
Contains the number of integrand evaluations actually used by nag_quad_md_mcarlo (d01gb).
2:     acc – double scalar
The estimated relative accuracy of finest.
3:     wrkstr(lenwrk) – double array
Contains information about the current sub-interval structure which could be used in later calls of nag_quad_md_mcarlo (d01gb). In particular, wrkstr(j)${\mathbf{wrkstr}}\left(j\right)$ gives the number of sub-intervals used along the j$j$th coordinate axis.
4:     finest – double scalar
The best estimate obtained for the integral.
5:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

ifail = 1${\mathbf{ifail}}=1$
 On entry, ndim < 1${\mathbf{ndim}}<1$, or ${\mathbf{mincls}}\ge {\mathbf{maxcls}}$, or lenwrk < 10 × ndim${\mathbf{lenwrk}}<10×{\mathbf{ndim}}$, or maxcls < 4 × (ndim + 1)${\mathbf{maxcls}}<4×\left({\mathbf{ndim}}+1\right)$, or eps < 0.0${\mathbf{eps}}<0.0$.
W ifail = 2${\mathbf{ifail}}=2$
maxcls was too small for nag_quad_md_mcarlo (d01gb) to obtain the required relative accuracy eps. In this case nag_quad_md_mcarlo (d01gb) 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 function calculates that the current estimates could not be improved before maxcls was exceeded.

## 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 nag_quad_md_mcarlo (d01gb) 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, nag_quad_md_mcarlo (d01gb) 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, nag_quad_md_mcarlo (d01gb) 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 nag_quad_md_mcarlo (d01gb) by calls to nag_rand_dist_uniform01 (g05sa). Separate runs will produce identical answers.

## Example

```function nag_quad_md_mcarlo_example
a = [0;
0;
0;
0];
b = [1;
1;
1;
1];
mincls = int64(1000);
maxcls = int64(20000);
epsilon = 0.01;
wrkstr =zeros(500,1);
finest = 0;
[minclsOut, acc, wrkstrOut, finestOut, ifail] = ...
nag_quad_md_mcarlo(a, b, mincls, maxcls, @functn, epsilon, wrkstr, finest);
minclsOut, acc, finestOut, ifail

function result = functn(ndim,x)
result = 4.0*x(1)*x(3)^2*exp(2.0*x(1)*x(3))/(1.0+x(2)+x(4))^2;
```
```

minclsOut =

1728

acc =

0.0092

finestOut =

0.5757

ifail =

0

```
```function d01gb_example
a = [0;
0;
0;
0];
b = [1;
1;
1;
1];
mincls = int64(1000);
maxcls = int64(20000);
epsilon = 0.01;
wrkstr =zeros(500,1);
finest = 0;
[minclsOut, acc, wrkstrOut, finestOut, ifail] = ...
d01gb(a, b, mincls, maxcls, @functn, epsilon, wrkstr, finest);
minclsOut, acc, finestOut, ifail

function result = functn(ndim,x)
result = 4.0*x(1)*x(3)^2*exp(2.0*x(1)*x(3))/(1.0+x(2)+x(4))^2;
```
```

minclsOut =

1728

acc =

0.0092

finestOut =

0.5757

ifail =

0

```