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 (d01ba) 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).
Note: this function is scheduled to be withdrawn, please see d01ba in Advice on Replacement Calls for Withdrawn/Superseded Routines..

Syntax

[result, ifail] = d01ba(d01xxx, a, b, n, fun)
[result, ifail] = nag_quad_1d_gauss(d01xxx, a, b, n, fun)

Description

General

nag_quad_1d_gauss (d01ba) evaluates an estimate of the definite integral of a function f(x)$f\left(x\right)$, over a finite or infinite range, by n$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
 n ∑ wif(xi) i = 1
$∑i=1nwif(xi)$
where the wi${w}_{i}$ are called the weights, and the xi${x}_{i}$ the abscissae. A selection of values of n$n$ is available. (See Section [Parameters].)

Both Limits Finite

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

One Limit Infinite

 ∞ a ∫ f(x)dx  or ∫ f(x)dx. a − ∞
$∫a∞f(x)dx or ∫-∞af(x)dx.$
Two quadrature formulae are available for these integrals.
(a) The Gauss–Laguerre formula is exact for any function of the form:
 2n − 1 f(x) = e − bx ∑ cixi. i = 0
$f(x)=e-bx ∑i= 0 2n- 1 ci xi.$
This formula is appropriate for functions decaying exponentially at infinity; the parameter b$b$ should be chosen if possible to match the decay rate of the function.
(b) The rational Gauss formula is exact for any function of the form:
 2n + 1 f(x) = ∑ (ci)/((x + b)i) = ( ∑ i = 02n − 1c2n + 1 − i(x + b)i)/((x + b)2n + 1). i = 2
$f(x)=∑i=2 2n+1ci(x+b)i=∑i=0 2n-1c2n+1-i(x+b)i(x+b)2n+1.$
This formula is likely to be more accurate for functions having only an inverse power rate of decay for large x$x$. Here the choice of a suitable value of b$b$ may be more difficult; unfortunately a poor choice of b$b$ can make a large difference to the accuracy of the computed integral.

Both Limits Infinite

 + ∞ ∫ f(x)dx. − ∞
$∫-∞ +∞f(x)dx.$
The Gauss–Hermite weights and abscissae are used, and the formula is exact for any function of the form:
 2n − 1 f(x) = e − b(x − a)2 ∑ cixi. i = 0
$f(x)=e-b (x-a) 2 ∑i= 0 2n- 1 ci xi.$
Again, for general functions not of this exact form, the parameter b$b$ should be chosen to match if possible the decay rate at ± $\text{}±\infty$.

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:     d01xxx – string
String specifying the quadrature formula to be used:
• 'd01baz', for Gauss–Legendre quadrature on a finite interval;
• 'd01bay', for rational Gauss quadrature on a semi-infinite interval;
• 'd01bax', for Gauss–Laguerre quadrature on a semi-infinite interval;
• 'd01baw', for Gauss–Hermite quadrature on an infinite interval.
2:     a – double scalar
3:     b – double scalar
The parameters a$a$ and b$b$ which occur in the integration formulae:
Gauss–Legendre:
a$a$ is the lower limit and b$b$ is the upper limit of the integral. It is not necessary that a < b$a.
Rational Gauss:
b$b$ must be chosen so as to make the integrand match as closely as possible the exact form given in Section [One Limit Infinite](b). The range of integration is [a)$\left[a\infty \right)$ if a + b > 0$a+b>0$, and (a]$\left(-\infty a\right]$ if a + b < 0$a+b<0$.
Gauss–Laguerre:
b$b$ must be chosen so as to make the integrand match as closely as possible the exact form given in Section [One Limit Infinite](a). The range of integration is [a)$\left[a\infty \right)$ if b > 0$b>0$, and (a]$\left(-\infty a\right]$ is b < 0$b<0$.
Gauss–Hermite:
a$a$ and b$b$ must be chosen so as to make the integrand match as closely as possible the exact form given in Section [Both Limits Infinite].
Constraints:
• Rational Gauss: a + b0.0${\mathbf{a}}+{\mathbf{b}}\ne 0.0$;
• Gauss–Laguerre: b0.0${\mathbf{b}}\ne 0.0$;
• Gauss–Hermite: b > 0${\mathbf{b}}>0$.
4:     n – int64int32nag_int scalar
n$n$, the number of abscissae to be used.
Constraint: n = 1${\mathbf{n}}=1$, 2$2$, 3$3$, 4$4$, 5$5$, 6$6$, 8$8$, 10$10$, 12$12$, 14$14$, 16$16$, 20$20$, 24$24$, 32$32$, 48$48$ or 64$64$.
5:     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(x)

Input Parameters

1:     x – double scalar
The point at which the integrand f$f$ must be evaluated.

Output Parameters

1:     result – double scalar
The result of the function.
Some points to bear in mind when coding fun are mentioned in Section [Accuracy].

None.

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

Note: nag_quad_1d_gauss (d01ba) 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 ifail = 1${\mathbf{ifail}}=1$
The N-point rule is not among those stored. If the soft fail option is used, the answer is evaluated for the largest valid value of n less than the requested value.
ifail = 2${\mathbf{ifail}}=2$
The value of a and/or b is invalid.
Rational Gauss: a + b = 0.0${\mathbf{a}}+{\mathbf{b}}=0.0$.
Gauss–Laguerre: b = 0.0${\mathbf{b}}=0.0$.
Gauss–Hermite: b0.0${\mathbf{b}}\le 0.0$.
If the soft fail option is used, the answer is returned as zero.

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 (d01ba) 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 fun 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
 f(x) = e − bxg(x) $f(x)=e-bxg(x)$
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 as:
 f(x) = exp( − bx + lng(x)) $f(x)=exp(-bx+ln⁡g(x))$
Another situation requiring care is exemplified by
 + ∞ ∫ e − x2xmdx = 0,  m​ odd. − ∞
$∫-∞ +∞e-x2xmdx=0, m​ odd.$
The integrand here assumes very large values; for example, for m = 63$m=63$, the peak value exceeds 3 × 1033$3×{10}^{33}$. Now, if the machine holds floating point numbers to an accuracy of k$k$ significant decimal digits, we could not expect such terms to cancel in the summation leaving an answer of much less than 1033k${10}^{33-k}$ (the weights being of order unity); that is instead of zero, we obtain a rather large answer through rounding error. Fortunately, such situations are characterised by great variability in the answers returned by formulae with different values of n$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 (d01ba) depends on the complexity of the expression for the integrand and on the number of abscissae required.

Example

This example evaluates the integrals
 1 ∫ 4/(1 + x2)dx = π 0
$∫0141+x2 dx=π$
 ∞ ∫ 1/(x2 lnx)dx = 0.378671 2
$∫2∞ 1x2 ln⁡x dx =0.378671$
by rational Gauss quadrature with b = 0$b=0$;
 ∞ ∫ (e − x)/xdx = 0.048901 2
$∫2∞e-xxdx=0.048901$
by Gauss–Laguerre quadrature with b = 1$b=1$; and
 + ∞ + ∞ ∫ e − 3x2 − 4x − 1dx = ∫ e − 3(x + 1)2e2x + 2dx = 1.428167 − ∞ − ∞
$∫-∞ +∞e-3⁢x2-4x-1dx=∫-∞ +∞e-3⁢ (x+1) 2e2x+2dx=1.428167$
by Gauss–Hermite quadrature with a = 1$a=-1$ and b = 3$b=3$.
The formulae with n = 4,8,16$n=4,8,16$ are used in each case.
```function nag_quad_1d_gauss_example
a = 0;
b = 1;
n = int64(4);
fun = @(x) 4.0/(1.0+x^2);
```
```

result =

3.1416

ifail =

0

```
```function d01ba_example
a = 0;
b = 1;
n = int64(4);
fun = @(x) 4.0/(1.0+x^2);
[result, ifail] = d01ba('d01baz', a, b, n, fun)
```
```

result =

3.1416

ifail =

0

```