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_1d_gauss_vec (d01ua) computes an estimate of the definite integral of a function of known analytical form, using a Gaussian quadrature formula with a specified number of abscissae. Formulae are provided for a finite interval (Gauss–Legendre), a semi-infinite interval (Gauss–Laguerre, rational Gauss), and an infinite interval (Gauss–Hermite).

## Syntax

[dinest, user, ifail] = d01ua(key, a, b, n, f, 'user', user)
[dinest, user, ifail] = nag_quad_1d_gauss_vec(key, a, b, n, f, 'user', user)

## Description

### General

nag_quad_1d_gauss_vec (d01ua) evaluates an estimate of the definite integral of a function $f\left(x\right)$, over a finite or infinite range, by $n$-point Gaussian quadrature (see Davis and Rabinowitz (1975), Fröberg (1970), Ralston (1965) or Stroud and Secrest (1966)). The integral is approximated by a summation of the product of a set of weights and a set of function evaluations at a corresponding set of abscissae ${x}_{i}$. For adjusted weights, the function values correspond to the values of the integrand $f$, and hence the sum will be
 $∑i=1nwifxi$
where the ${w}_{i}$ are called the weights, and the ${x}_{i}$ the abscissae. A selection of values of $n$ is available. (See Arguments.)
Where applicable, normal weights may instead be used, in which case the corresponding weight function $\omega$ is factored out of the integrand as $f\left(x\right)=\omega \left(x\right)g\left(x\right)$ and hence the sum will be
 $∑ i=-1 n w-i gx ,$
where the normal weights ${\stackrel{-}{w}}_{i}={w}_{i}\omega \left({x}_{i}\right)$ are computed internally.
nag_quad_1d_gauss_vec (d01ua) uses a vectorized f to evaluate the integrand or normalized integrand at a set of abscissae, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{n}_{x}$. If adjusted weights are used, the integrand $f\left({x}_{i}\right)$ must be evaluated otherwise the normalized integrand $g\left({x}_{i}\right)$ must be evaluated.

### Both Limits Finite

 $∫abfxdx.$
The Gauss–Legendre weights and abscissae are used, and the formula is exact for any function of the form:
 $fx=∑i= 0 2n- 1 ci xi.$
The formula is appropriate for functions which can be well approximated by such a polynomial over $\left[a,b\right]$. It is inappropriate for functions with algebraic singularities at one or both ends of the interval, such as ${\left(1+x\right)}^{-1/2}$ on $\left[-1,1\right]$.

### One Limit Infinite

 $∫a∞fxdx or ∫-∞afxdx.$
Two quadrature formulae are available for these integrals.
(a) The Gauss–Laguerre formula is exact for any function of the form:
 $fx=e-bx ∑i= 0 2n- 1 ci xi.$
This formula is appropriate for functions decaying exponentially at infinity; the argument $b$ should be chosen if possible to match the decay rate of the function.
If the adjusted weights are selected, the complete integrand $f\left(x\right)$ should be provided through f.
If the normal form is selected, the contribution of ${e}^{-bx}$ is accounted for internally, and f should only return $g\left(x\right)$, where $f\left(x\right)={e}^{-bx}g\left(x\right)$.
If $b<0$ is supplied, the interval of integration will be $\left[a,\infty \right)$. Otherwise if $b>0$ is supplied, the interval of integration will be $\left(-\infty ,a\right]$.
(b) The rational Gauss formula is exact for any function of the form:
 $fx=∑i=2 2n+1cix+bi=∑i=0 2n-1c2n+1-ix+bix+b2n+1.$
This formula is likely to be more accurate for functions having only an inverse power rate of decay for large $x$. Here the choice of a suitable value of $b$ may be more difficult; unfortunately a poor choice of $b$ can make a large difference to the accuracy of the computed integral.
Only the adjusted form of the rational Gauss formula is available, and as such, the complete integrand $f\left(x\right)$ must be supplied in f.
If $a+b<0$, the interval of integration will be $\left[a,\infty \right)$. Otherwise if $a+b>0$, the interval of integration will be $\left(-\infty ,a\right]$.

### Both Limits Infinite

 $∫-∞ +∞fxdx.$
The Gauss–Hermite weights and abscissae are used, and the formula is exact for any function of the form:
 $fx = e - b x-a 2 ∑ i=0 2n- 1 ci xi ,$
where $b>0$. Again, for general functions not of this exact form, the argument $b$ should be chosen to match if possible the decay rate at $\text{}±\infty$.
If the adjusted weights are selected, the complete integrand $f\left(x\right)$ should be provided through f.
If the normal form is selected, the contribution of ${e}^{-b{\left(x-a\right)}^{2}}$ is accounted for internally, and f should only return $g\left(x\right)$, where $f\left(x\right)={e}^{-b{\left(x-a\right)}^{2}}g\left(x\right)$.

## References

Davis P J and Rabinowitz P (1975) Methods of Numerical Integration Academic Press
Fröberg C E (1970) Introduction to Numerical Analysis Addison–Wesley
Ralston A (1965) A First Course in Numerical Analysis pp. 87–90 McGraw–Hill
Stroud A H and Secrest D (1966) Gaussian Quadrature Formulas Prentice–Hall

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{key}$int64int32nag_int scalar
${\mathbf{key}}=0$
Gauss–Legendre quadrature on a finite interval, using normal weights.
${\mathbf{key}}=3$
Gauss–Laguerre quadrature on a semi-infinite interval, using normal weights.
${\mathbf{key}}=-3$
${\mathbf{key}}=4$
Gauss–Hermite quadrature on an infinite interval, using normal weights.
${\mathbf{key}}=-4$
${\mathbf{key}}=-5$
Constraint: ${\mathbf{key}}=0$, $3$, $-3$, $4$, $-4$ or $-5$.
2:     $\mathrm{a}$ – double scalar
3:     $\mathrm{b}$ – double scalar
The quantities $a$ and $b$ as described in the appropriate subsection of Description.
Constraints:
• Rational Gauss: ${\mathbf{a}}+{\mathbf{b}}\ne 0.0$;
• Gauss–Laguerre: ${\mathbf{b}}\ne 0.0$;
• Gauss–Hermite: ${\mathbf{b}}>0$.
4:     $\mathrm{n}$int64int32nag_int scalar
$n$, the number of abscissae to be used.
Constraint: ${\mathbf{n}}=1$, $2$, $3$, $4$, $5$, $6$, $8$, $10$, $12$, $14$, $16$, $20$, $24$, $32$, $48$ or $64$.
If the soft fail option is used, the answer is evaluated for the largest valid value of n less than the requested value.
5:     $\mathrm{f}$ – function handle or string containing name of m-file
f must return the value of the integrand $f$, or the normalized integrand $g$, at a specified point.
[fv, iflag, user] = f(x, nx, iflag, user)

Input Parameters

1:     $\mathrm{x}\left({\mathbf{nx}}\right)$ – double array
The abscissae, ${x}_{i}$, for $\mathit{i}=1,2,\dots ,{n}_{x}$ at which function values are required.
2:     $\mathrm{nx}$int64int32nag_int scalar
${n}_{x}$, the number of abscissae.
3:     $\mathrm{iflag}$int64int32nag_int scalar
${\mathbf{iflag}}=0$.
4:     $\mathrm{user}$ – Any MATLAB object
f is called from nag_quad_1d_gauss_vec (d01ua) with the object supplied to nag_quad_1d_gauss_vec (d01ua).

Output Parameters

1:     $\mathrm{fv}\left({\mathbf{nx}}\right)$ – double array
If adjusted weights are used, the values of the integrand $f$. ${\mathbf{fv}}\left(\mathit{i}\right)=f\left({x}_{\mathit{i}}\right)$, for $\mathit{i}=1,2,\dots ,{n}_{x}$.
Otherwise the values of the normalized integrand $g$. ${\mathbf{fv}}\left(\mathit{i}\right)=g\left({x}_{\mathit{i}}\right)$, for $\mathit{i}=1,2,\dots ,{n}_{x}$.
2:     $\mathrm{iflag}$int64int32nag_int scalar
Set ${\mathbf{iflag}}<0$ if you wish to force an immediate exit from nag_quad_1d_gauss_vec (d01ua) with ${\mathbf{ifail}}=-{\mathbf{1}}$.
3:     $\mathrm{user}$ – Any MATLAB object
Some points to bear in mind when coding f are mentioned in Accuracy.

### Optional Input Parameters

1:     $\mathrm{user}$ – Any MATLAB object
user is not used by nag_quad_1d_gauss_vec (d01ua), but is passed to f. Note that for large objects it may be more efficient to use a global variable which is accessible from the m-files than to use user.

### Output Parameters

1:     $\mathrm{dinest}$ – double scalar
The estimate of the definite integral.
2:     $\mathrm{user}$ – Any MATLAB object
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Note: nag_quad_1d_gauss_vec (d01ua) may return useful information for one or more of the following detected errors or 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.

W  ${\mathbf{ifail}}=1$
The $n$-point rule is not among those stored.
W  ${\mathbf{ifail}}=2$
Underflow occurred in calculation of normal weights.
W  ${\mathbf{ifail}}=3$
No nonzero weights were generated for the provided parameters.
${\mathbf{ifail}}=11$
Constraint: ${\mathbf{key}}=0$, $3$, $-3$, $4$, $-4$ or $-5$.
${\mathbf{ifail}}=12$
The value of a and/or b is invalid for the chosen key. Either:
• Constraint: $\left|{\mathbf{a}}+{\mathbf{b}}\right|>0.0$.
• Constraint: $\left|{\mathbf{b}}\right|>0.0$.
• Constraint: ${\mathbf{b}}>0.0$.
${\mathbf{ifail}}=14$
Constraint: ${\mathbf{n}}>0$.
W  ${\mathbf{ifail}}=-1$
Exit requested from f with ${\mathbf{iflag}}=_$.
${\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 depends on the behaviour of the integrand, and on the number of abscissae used. No tests are carried out in nag_quad_1d_gauss_vec (d01ua) to estimate the accuracy of the result. If such an estimate is required, the function may be called more than once, with a different number of abscissae each time, and the answers compared. It is to be expected that for sufficiently smooth functions a larger number of abscissae will give improved accuracy.
Alternatively, the range of integration may be subdivided, the integral estimated separately for each sub-interval, and the sum of these estimates compared with the estimate over the whole range.
The coding of f may also have a bearing on the accuracy. For example, if a high-order Gauss–Laguerre formula is used, and the integrand is of the form
 $fx=e-bxgx$
it is possible that the exponential term may underflow for some large abscissae. Depending on the machine, this may produce an error, or simply be assumed to be zero. In any case, it would be better to evaluate the expression with
 $fx = sgn gx × exp -bx+ lngx$
Another situation requiring care is exemplified by
 $∫-∞ +∞e-x2xmdx=0, m​ odd.$
The integrand here assumes very large values; for example, when $m=63$, the peak value exceeds $3×{10}^{33}$. Now, if the machine holds floating-point numbers to an accuracy of $k$ significant decimal digits, we could not expect such terms to cancel in the summation leaving an answer of much less than ${10}^{33-k}$ (the weights being of order unity); that is, instead of zero we obtain a rather large answer through rounding error. Such situations are characterised by great variability in the answers returned by formulae with different values of $n$.
In general, you should be aware of the order of magnitude of the integrand, and should judge the answer in that light.

The time taken by nag_quad_1d_gauss_vec (d01ua) depends on the complexity of the expression for the integrand and on the number of abscissae required.

## Example

This example evaluates the integrals
 $∫0141+x2 dx=π$
 $∫2∞ 1x2 ln⁡x dx =0.378671$
by rational Gauss quadrature with $b=0$;
 $∫2∞e-xxdx=0.048901$
by Gauss–Laguerre quadrature with $b=1$; and
 $∫-∞ +∞e-3⁢x2-4x-1dx=∫-∞ +∞e-3⁢ x+1 2e2x+2dx=1.428167$
by Gauss–Hermite quadrature with $a=-1$ and $b=3$.
The formulae with $n=2,4,8,16,32$ and $64$ are used in each case. Both adjusted and normal weights are used for Gauss–Laguerre and Gauss–Hermite quadrature.
function d01ua_example

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

for funid=1:6
switch funid
case 1
fprintf('\nGauss-Legendre example\n');
a = 0.0;
b = 1.0;
key = int64(0);
case 2
fprintf('\nRational Gauss example\n');
a = 2.0;
b = 0.0;
key = int64(-5);
case 3
a = 2.0;
b = 1.0;
key = int64(-3);
case 4
fprintf('\nGauss-Laguerre example (normal weights)\n');
a = 2.0;
b = 1.0;
key = int64(3);
case 5
a = -1.0;
b = 3.0;
key = int64(-4);
case 6
fprintf('\nGauss-Hermite example (normal weights)\n');
a = -1.0;
b = 3.0;
key = int64(4);
end

for i=1:6
n = int64(2^i);
[dinest, user, ifail] = d01ua(key, a, b, n, @f, 'user', funid);

if ifail == 0 || ifail == 1
fprintf('%2d Points   Answer = %10.5f\n', n, dinest);
end
end
end

function [fv, iflag, user] = f(x, nx, iflag, user)
switch user
case 1
fv = 4./(1+x.*x);
case 2
fv = 1./(x.*x.*log(x));
case 3
fv = exp(-x)./x;
case 4
fv = 1./x;
case 5
fv = exp(-3.*x.*x-4.*x-1);
case 6
fv = exp(2*x+2);
otherwise
fv = zeros(nx, 1);
iflag = -1;
end
d01ua example results

Gauss-Legendre example

Rational Gauss example

Gauss-Laguerre example (normal weights)

Gauss-Hermite example (normal weights)