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_gauss (d01fb) computes an estimate of a multidimensional integral (from $1$ to $20$ dimensions), given the analytic form of the integrand and suitable Gaussian weights and abscissae.

## Syntax

[result, ifail] = d01fb(nptvec, weight, abscis, fun, 'ndim', ndim, 'lwa', lwa)
[result, ifail] = nag_quad_withdraw_md_gauss(nptvec, weight, abscis, fun, 'ndim', ndim, 'lwa', lwa)

## Description

nag_quad_md_gauss (d01fb) approximates a multidimensional integral by evaluating the summation
 $∑i1=1l1 w 1,i1 ∑i2=1l2 w2,i2 ⋯ ∑in=1ln wn,in f x 1 , i1 , x 2 , i2 ,…, x n , in$
given the weights ${w}_{j,{i}_{j}}$ and abscissae ${x}_{j,{i}_{j}}$ for a multidimensional product integration rule (see Davis and Rabinowitz (1975)). The number of dimensions may be anything from $1$ to $20$.
The weights and abscissae for each dimension must have been placed in successive segments of the arrays weight and abscis; for example, by calling nag_quad_1d_gauss_wgen (d01bc) or nag_quad_1d_gauss_wres (d01tb) once for each dimension using a quadrature formula and number of abscissae appropriate to the range of each ${x}_{j}$ and to the functional dependence of $f$ on ${x}_{j}$.
If normal weights are used, the summation will approximate the integral
 $∫w1x1∫w2x2⋯∫wnxnf x1,x2,…,xn dxn⋯dx2dx1$
where ${w}_{j}\left(x\right)$ is the weight function associated with the quadrature formula chosen for the $j$th dimension; while if adjusted weights are used, the summation will approximate the integral
 $∫∫⋯∫fx1,x2,…,xndxn⋯dx2dx1.$
You must supply a function to evaluate
 $fx1,x2,…,xn$
at any values of ${x}_{1},{x}_{2},\dots ,{x}_{n}$ within the range of integration.

## References

Davis P J and Rabinowitz P (1975) Methods of Numerical Integration Academic Press

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{nptvec}\left({\mathbf{ndim}}\right)$int64int32nag_int array
${\mathbf{nptvec}}\left(\mathit{j}\right)$ must specify the number of points in the $\mathit{j}$th dimension of the summation, for $\mathit{j}=1,2,\dots ,n$.
2:     $\mathrm{weight}\left({\mathbf{lwa}}\right)$ – double array
Must contain in succession the weights for the various dimensions, i.e., ${\mathbf{weight}}\left(k\right)$ contains the $i$th weight for the $j$th dimension, with
 $k=nptvec1+nptvec2+⋯+nptvecj-1+i.$
3:     $\mathrm{abscis}\left({\mathbf{lwa}}\right)$ – double array
Must contain in succession the abscissae for the various dimensions, i.e., ${\mathbf{abscis}}\left(k\right)$ contains the $i$th abscissa for the $j$th dimension, with
 $k=nptvec1+nptvec2+⋯+nptvecj-1+i.$
4:     $\mathrm{fun}$ – function handle or string containing name of m-file
fun must return the value of the integrand $f$ at a specified point.
[result] = fun(ndim, x)

Input Parameters

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

Output Parameters

1:     $\mathrm{result}$ – double scalar
The value of $f\left(x\right)$ evaluated at x.

### Optional Input Parameters

1:     $\mathrm{ndim}$int64int32nag_int scalar
Default: the dimension of the array nptvec.
$n$, the number of dimensions of the integral.
Constraint: $1\le {\mathbf{ndim}}\le 20$.
2:     $\mathrm{lwa}$int64int32nag_int scalar
Default: the dimension of the arrays weight, abscis. (An error is raised if these dimensions are not equal.)
The dimension of the arrays weight and abscis.
Constraint: ${\mathbf{lwa}}\ge {\mathbf{nptvec}}\left(1\right)+{\mathbf{nptvec}}\left(2\right)+\cdots +{\mathbf{nptvec}}\left({\mathbf{ndim}}\right)$.

### Output Parameters

1:     $\mathrm{result}$ – double scalar
The result of the function.
2:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{ndim}}<1$, or ${\mathbf{ndim}}>20$, or ${\mathbf{lwa}}<{\mathbf{nptvec}}\left(1\right)+{\mathbf{nptvec}}\left(2\right)+\cdots +{\mathbf{nptvec}}\left({\mathbf{ndim}}\right)$.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

The accuracy of the computed multidimensional sum depends on the weights and the integrand values at the abscissae. If these numbers vary significantly in size and sign then considerable accuracy could be lost. If these numbers are all positive, then little accuracy will be lost in computing the sum.

The total time taken by nag_quad_md_gauss (d01fb) will be proportional to
 $T×nptvec1×nptvec2×⋯×nptvecndim,$
where $T$ is the time taken for one evaluation of fun.

## Example

This example evaluates the integral
 $∫12∫0∞∫-∞∞∫1∞ x1x2x3 6 x4+2 8e-2x2e-0.5x32dx4dx3dx2dx1$
• ${x}_{1}$: Gauss–Legendre, $a=1.0$, $b=2.0$,
• ${x}_{2}$: Gauss–Laguerre, $a=0.0$, $b=2.0$,
• ${x}_{3}$: Gauss–Hermite, $a=0.0$, $b=0.5$,
• ${x}_{4}$: rational Gauss, $a=1.0$, $b=2.0$.
Four points are sufficient in each dimension, as this integral is in fact a product of four one-dimensional integrals, for each of which the chosen four-point formula is exact.
function d01fb_example

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

nptvec = [int64(4), 4, 4, 4];
a      = [1, 0, 0  , 1];
b      = [2, 2, 0.5, 2];
key    = [int64(0), -3, -4, -5];
j      = 1;
for i = 1:4
[wgt, absc, ifail] = d01tb(key(i), a(i), b(i), nptvec(i));
weight(j:j+nptvec(i)-1) = wgt;
abscis(j:j+nptvec(i)-1) = absc;
j = j + nptvec(i);
end

[result, ifail] = d01fb( ...
nptvec, weight, abscis, @fun);

fprintf('Result = %13.5f\n', result);

function result = fun(ndim,x)
result = ((x(1)*x(2)*x(3))^6/(x(4)+2)^8)*exp(-2*x(2)-x(3)^2/2);
d01fb example results

Result =       0.25065