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$1$ to 20$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_md_gauss(nptvec, weight, abscis, fun, 'ndim', ndim, 'lwa', lwa)

## Description

nag_quad_md_gauss (d01fb) approximates a multidimensional integral by evaluating the summation
 l1 l2 ln ∑ w1,i1 ∑ w2,i2 ⋯ ∑ wn,inf(x 1 , i1 ,x 2 , i2 , … ,x n , in) i1 = 1 i2 = 1 in = 1
$∑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 wj,ij${w}_{j,{i}_{j}}$ and abscissae xj,ij${x}_{j,{i}_{j}}$ for a multidimensional product integration rule (see Davis and Rabinowitz (1975)). The number of dimensions may be anything from 1$1$ to 20$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 xj${x}_{j}$ and to the functional dependence of f$f$ on xj${x}_{j}$.
If normal weights are used, the summation will approximate the integral
 ∫ w1(x1) ∫ w2(x2) ⋯ ∫ wn(xn)f (x1,x2, … ,xn) dxn ⋯ dx2dx1 $∫w1(x1)∫w2(x2)⋯∫wn(xn)f (x1,x2,…,xn) dxn⋯dx2dx1$
where wj(x)${w}_{j}\left(x\right)$ is the weight function associated with the quadrature formula chosen for the j$j$th dimension; while if adjusted weights are used, the summation will approximate the integral
 ∫ ∫ ⋯ ∫ f(x1,x2, … ,xn)dxn ⋯ dx2dx1. $∫∫⋯∫f(x1,x2,…,xn)dxn⋯dx2dx1.$
You must supply a function to evaluate
 f(x1,x2, … ,xn) $f(x1,x2,…,xn)$
at any values of x1,x2,,xn${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:     nptvec(ndim) – int64int32nag_int array
ndim, the dimension of the array, must satisfy the constraint 1ndim20$1\le {\mathbf{ndim}}\le 20$.
nptvec(j)${\mathbf{nptvec}}\left(\mathit{j}\right)$ must specify the number of points in the j$\mathit{j}$th dimension of the summation, for j = 1,2,,n$\mathit{j}=1,2,\dots ,n$.
2:     weight(lwa) – double array
lwa, the dimension of the array, must satisfy the constraint lwanptvec(1) + nptvec(2) + + ${\mathbf{lwa}}\ge {\mathbf{nptvec}}\left(1\right)+{\mathbf{nptvec}}\left(2\right)+\cdots +{\mathbf{nptvec}}\left({\mathbf{ndim}}\right)$.
Must contain in succession the weights for the various dimensions, i.e., weight(k)${\mathbf{weight}}\left(k\right)$ contains the i$i$th weight for the j$j$th dimension, with
 k = nptvec(1) + nptvec(2) + ⋯ + nptvec(j − 1) + i. $k=nptvec1+nptvec2+⋯+nptvecj-1+i.$
3:     abscis(lwa) – double array
lwa, the dimension of the array, must satisfy the constraint lwanptvec(1) + nptvec(2) + + ${\mathbf{lwa}}\ge {\mathbf{nptvec}}\left(1\right)+{\mathbf{nptvec}}\left(2\right)+\cdots +{\mathbf{nptvec}}\left({\mathbf{ndim}}\right)$.
Must contain in succession the abscissae for the various dimensions, i.e., abscis(k)${\mathbf{abscis}}\left(k\right)$ contains the i$i$th abscissa for the j$j$th dimension, with
 k = nptvec(1) + nptvec(2) + ⋯ + nptvec(j − 1) + i. $k=nptvec1+nptvec2+⋯+nptvecj-1+i.$
4:     fun – function handle or string containing name of m-file
fun must return the value of the integrand f$f$ at a specified point.
[result] = fun(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.

### Optional Input Parameters

1:     ndim – int64int32nag_int scalar
Default: The dimension of the array nptvec.
n$n$, the number of dimensions of the integral.
Constraint: 1ndim20$1\le {\mathbf{ndim}}\le 20$.
2:     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 as declared in the (sub)program from which nag_quad_md_gauss (d01fb) is called.
Constraint: lwanptvec(1) + nptvec(2) + + ${\mathbf{lwa}}\ge {\mathbf{nptvec}}\left(1\right)+{\mathbf{nptvec}}\left(2\right)+\cdots +{\mathbf{nptvec}}\left({\mathbf{ndim}}\right)$.

None.

### Output Parameters

1:     result – double scalar
The result of the function.
2:     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:
ifail = 1${\mathbf{ifail}}=1$
 On entry, ndim < 1${\mathbf{ndim}}<1$, or ndim > 20${\mathbf{ndim}}>20$, or lwa < nptvec(1) + nptvec(2) + ⋯ + ${\mathbf{lwa}}<{\mathbf{nptvec}}\left(1\right)+{\mathbf{nptvec}}\left(2\right)+\cdots +{\mathbf{nptvec}}\left({\mathbf{ndim}}\right)$.

## 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 × nptvec(1) × nptvec(2) × ⋯ × , $T×nptvec1×nptvec2×⋯×nptvecndim,$
where T$T$ is the time taken for one evaluation of fun.

## Example

```function nag_quad_md_gauss_example
nptvec = [int64(4);4;4;4];
weight = [0.1739274225687269;
0.326072577431273;
0.326072577431273;
0.1739274225687269;
0.4163695619189446;
1.024051219227148;
1.815573152910759;
3.243572542203831;
1.753944171790685;
1.49901614734385;
1.49901614734385;
1.753944171790685;
0.6025497939505366;
2.179205635439835;
8.982199653682851;
108.2360449169268];
abscis = [1.930568155797026;
1.669990521792428;
1.330009478207572;
1.069431844202974;
0.1612738448096961;
0.8728805505791732;
2.268310148460564;
4.697535456150566;
2.334414218338977;
0.7419637843027259;
-0.7419637843027259;
-2.334414218338977;
1.223836944463802;
2.477675284083257;
7.090647990761759;
41.20783978069118];
[result, ifail] = nag_quad_md_gauss(nptvec, weight, abscis, @fun)

function result = fun(ndim,x)
result = (x(1)*x(2)*x(3))^6/(x(4)+2.0)^8*exp(-2.0*x(2)-0.5*x(3)*x(3));
```
```

result =

0.2506

ifail =

0

```
```function d01fb_example
nptvec = [int64(4);4;4;4];
weight = [0.1739274225687269;
0.326072577431273;
0.326072577431273;
0.1739274225687269;
0.4163695619189446;
1.024051219227148;
1.815573152910759;
3.243572542203831;
1.753944171790685;
1.49901614734385;
1.49901614734385;
1.753944171790685;
0.6025497939505366;
2.179205635439835;
8.982199653682851;
108.2360449169268];
abscis = [1.930568155797026;
1.669990521792428;
1.330009478207572;
1.069431844202974;
0.1612738448096961;
0.8728805505791732;
2.268310148460564;
4.697535456150566;
2.334414218338977;
0.7419637843027259;
-0.7419637843027259;
-2.334414218338977;
1.223836944463802;
2.477675284083257;
7.090647990761759;
41.20783978069118];
[result, ifail] = d01fb(nptvec, weight, abscis, @fun)

function result = fun(ndim,x)
result = (x(1)*x(2)*x(3))^6/(x(4)+2.0)^8*exp(-2.0*x(2)-0.5*x(3)*x(3));
```
```

result =

0.2506

ifail =

0

```