hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_quad_1d_gauss (d01ba)

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(x), over a finite or infinite range, by nn-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 wiwi are called the weights, and the xixi the abscissae. A selection of values of nn 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][a,b]. It is inappropriate for functions with algebraic singularities at one or both ends of the interval, such as (1 + x)1 / 2(1+x)-1/2 on [1,1][-1,1].

One Limit Infinite

a
f(x)dx  or  f(x)dx.
a
af(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) = ebxcixi.
i = 0
f(x)=e-bx i= 0 2n- 1 ci xi.
This formula is appropriate for functions decaying exponentially at infinity; the parameter bb 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 = 02n1c2n + 1i(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 xx. Here the choice of a suitable value of bb may be more difficult; unfortunately a poor choice of bb 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) = eb(xa)2cixi.
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 bb should be chosen to match if possible the decay rate at ± ±.

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 aa and bb which occur in the integration formulae:
Gauss–Legendre:
aa is the lower limit and bb is the upper limit of the integral. It is not necessary that a < ba<b.
Rational Gauss:
bb 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)[a) if a + b > 0a+b>0, and (a](-a] if a + b < 0a+b<0.
Gauss–Laguerre:
bb 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)[a) if b > 0b>0, and (a](-a] is b < 0b<0.
Gauss–Hermite:
aa and bb 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.0a+b0.0;
  • Gauss–Laguerre: b0.0b0.0;
  • Gauss–Hermite: b > 0b>0.
4:     n – int64int32nag_int scalar
nn, the number of abscissae to be used.
Constraint: n = 1n=1, 22, 33, 44, 55, 66, 88, 1010, 1212, 1414, 1616, 2020, 2424, 3232, 4848 or 6464.
5:     fun – function handle or string containing name of m-file
fun must return the value of the integrand ff at a specified point.
[result] = fun(x)

Input Parameters

1:     x – double scalar
The point at which the integrand ff 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].

Optional Input Parameters

None.

Input Parameters Omitted from the MATLAB Interface

None.

Output Parameters

1:     result – double scalar
The result of the function.
2:     ifail – int64int32nag_int scalar
ifail = 0ifail=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 = 1ifail=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 = 2ifail=2
The value of a and/or b is invalid.
Rational Gauss: a + b = 0.0a+b=0.0.
Gauss–Laguerre: b = 0.0b=0.0.
Gauss–Hermite: b0.0b0.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) = ebxg(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+lng(x))
Another situation requiring care is exemplified by
+
ex2xmdx = 0,  m​ odd.
- +e-x2xmdx=0,  m​ odd.
The integrand here assumes very large values; for example, for m = 63m=63, the peak value exceeds 3 × 10333×1033. Now, if the machine holds floating point numbers to an accuracy of kk significant decimal digits, we could not expect such terms to cancel in the summation leaving an answer of much less than 1033k1033-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 nn. In general, you should be aware of the order of magnitude of the integrand, and should judge the answer in that light.

Further Comments

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=π
by Gauss–Legendre quadrature;
1/(x2 lnx)dx = 0.378671
2
2 1x2 lnx dx =0.378671
by rational Gauss quadrature with b = 0b=0;
(ex)/xdx = 0.048901
2
2e-xxdx=0.048901
by Gauss–Laguerre quadrature with b = 1b=1; and
+ +
e3x24x1dx = e3(x + 1)2e2x + 2dx = 1.428167
- +e-3x2-4x-1dx=- +e-3 (x+1) 2e2x+2dx=1.428167
by Gauss–Hermite quadrature with a = 1a=-1 and b = 3b=3.
The formulae with n = 4,8,16n=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, ifail] = nag_quad_1d_gauss('nag_quad_1d_gauss_legendre', a, b, n, fun)
 

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



PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013