PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_quad_md_mcarlo (d01gb)
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:
Upon entry, unless
lenwrk has been set to the minimum value
, 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:
– double array
-
The lower limits of integration,
, for .
- 2:
– double array
-
The upper limits of integration,
, for .
- 3:
– int64int32nag_int scalar
-
Must be set
- either to the minimum number of integrand evaluations to be allowed, in which case ;
- 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:
– 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:
- ;
- .
- 5:
– function handle or string containing name of m-file
-
functn must return the value of the integrand
at a given point.
[result] = functn(ndim, x)
Input Parameters
- 1:
– int64int32nag_int scalar
-
, the number of dimensions of the integral.
- 2:
– double array
-
The coordinates of the point at which the integrand must be evaluated.
Output Parameters
- 1:
– double scalar
-
The value of the integrand at the given point.
- 6:
– double scalar
-
The relative accuracy required.
Constraint:
.
- 7:
– double array
-
If
,
wrkstr must be unchanged from the previous call of
nag_quad_md_mcarlo (d01gb) – except that for a new integrand
must be set to
. See also
mincls.
- 8:
– double scalar
-
Must be unchanged from a previous call to nag_quad_md_mcarlo (d01gb).
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the dimension of the arrays
a,
b. (An error is raised if these dimensions are not equal.)
, the number of dimensions of the integral.
Constraint:
.
- 2:
– 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
then the function uses only one iteration of a crude Monte–Carlo method with
maxcls sample points.
Constraint:
.
Output Parameters
- 1:
– int64int32nag_int scalar
-
Contains the number of integrand evaluations actually used by nag_quad_md_mcarlo (d01gb).
- 2:
– double scalar
-
The estimated relative accuracy of
finest.
- 3:
– 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, gives the number of sub-intervals used along the th coordinate axis.
- 4:
– double scalar
-
The best estimate obtained for the integral.
- 5:
– int64int32nag_int scalar
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, | , |
or | , |
or | , |
or | , |
or | . |
- W
-
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.
Accuracy
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
, 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.
Example
This example calculates the integral
Open in the MATLAB editor:
d01gb_example
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] = ...
d01gb(...
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)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015