hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_quad_md_mcarlo (d01gb)


    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example


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.


[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)


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:
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, 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.


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


Compulsory Input Parameters

1:     andim – double array
The lower limits of integration, ai, for i=1,2,,n.
2:     bndim – double array
The upper limits of integration, bi, for i=1,2,,n.
3:     mincls int64int32nag_int scalar
Must be set
  • either to the minimum number of integrand evaluations to be allowed, in which case mincls0;
  • or to a negative value. In this case, the function assumes that a previous call had been made with the same arguments 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.
  • maxcls>mincls;
  • maxcls4×ndim+1.
5:     functn – function handle or string containing name of m-file
functn must return the value of the integrand f at a given point.
[result] = functn(ndim, x)

Input Parameters

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

Output Parameters

1:     result – double scalar
The value of the integrand f at the given point.
6:     eps – double scalar
The relative accuracy required.
Constraint: eps0.0.
7:     wrkstrlenwrk – double array
If mincls<0, wrkstr must be unchanged from the previous call of nag_quad_md_mcarlo (d01gb) – except that for a new integrand wrkstrlenwrk must be set to 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, the number of dimensions of the integral.
Constraint: ndim1.
2:     lenwrk int64int32nag_int scalar
Default: the dimension of the array wrkstr.
The dimension of the array wrkstr.
For maximum efficiency, lenwrk should be about
If lenwrk is given the value 10×ndim then the function uses only one iteration of a crude Monte–Carlo method with maxcls sample points.
Constraint: lenwrk10×ndim.

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:     wrkstrlenwrk – 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, wrkstrj gives the number of sub-intervals used along the jth coordinate axis.
4:     finest – double scalar
The best estimate obtained for the integral.
5:     ifail int64int32nag_int scalar
ifail=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.

On entry,ndim<1,
W  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.
An unexpected error has been triggered by this routine. Please contact NAG.
Your licence key may have expired or may not have been installed correctly.
Dynamic memory allocation failed.


A relative error estimate is output through the argument 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.

Further Comments

The running time for nag_quad_md_mcarlo (d01gb) will usually be dominated by the time used to evaluate the integrand f, 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.

Additional Information

The exact values of finest and acc on return will depend (within statistical limits) on the sequence of random numbers generated within this function.


This example calculates the integral
010101014x1x3exp2x1x3 1+x2+x4 2dx1dx2dx3dx4=0.575364.  
function d01gb_example

fprintf('d01gb example results\n\n');

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;

[mincls, acc, wrkstr, finest, ifail] = ...
          a, b, mincls, maxcls, @functn, epsilon, wrkstr, finest);

fprintf('Requested accuracy    =  %9.2e\n', epsilon);
fprintf('Estimated value       =  %8.5f\n', finest);
fprintf('Estimated accuracy    =  %9.2e\n', acc);
fprintf('Number of evaluations =  %5d\n',mincls);

function result = functn(ndim,x)
  result = 4*x(1)*x(3)^2*exp(2*x(1)*x(3))/(1+x(2)+x(4))^2;
d01gb example results

Requested accuracy    =   1.00e-02
Estimated value       =   0.57572
Estimated accuracy    =   9.21e-03
Number of evaluations =   1728

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

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