D01BAF (PDF version)
D01 Chapter Contents
D01 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

D01BAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

D01BAF 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).

2  Specification

FUNCTION D01BAF ( D01XXX, A, B, N, FUN, IFAIL)
REAL (KIND=nag_wp) D01BAF
INTEGER  N, IFAIL
REAL (KIND=nag_wp)  A, B, FUN
EXTERNAL  D01XXX, FUN

3  Description

3.1  General

D01BAF evaluates an estimate of the definite integral of a function fx, 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
i=1nwifxi
where the wi are called the weights, and the xi the abscissae. A selection of values of n is available. (See Section 5.)

3.2  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 a,b. It is inappropriate for functions with algebraic singularities at one or both ends of the interval, such as 1+x-1/2 on -1,1.

3.3  One Limit Infinite

afxdx  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 parameter 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:
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.

3.4  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.
Again, for general functions not of this exact form, the parameter b should be chosen to match if possible the decay rate at ±.

4  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

5  Parameters

1:     D01XXX – SUBROUTINE, supplied by the NAG Library.External Procedure
The name of the routine indicates the quadrature formula:
  • 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.
The name used must be declared as EXTERNAL in the subroutine from which D01BAF is called.
2:     A – REAL (KIND=nag_wp)Input
3:     B – REAL (KIND=nag_wp)Input
On entry: the parameters a and b which occur in the integration formulae:
Gauss–Legendre:
a is the lower limit and b is the upper limit of the integral. It is not necessary that a<b.
Rational Gauss:
b must be chosen so as to make the integrand match as closely as possible the exact form given in Section 3.3(b). The range of integration is a if a+b>0, and -a if a+b<0.
Gauss–Laguerre:
b must be chosen so as to make the integrand match as closely as possible the exact form given in Section 3.3(a). The range of integration is a if b>0, and -a is b<0.
Gauss–Hermite:
a and b must be chosen so as to make the integrand match as closely as possible the exact form given in Section 3.4.
Constraints:
  • Rational Gauss: A+B0.0;
  • Gauss–Laguerre: B0.0;
  • Gauss–Hermite: B>0.
4:     N – INTEGERInput
On entry: n, the number of abscissae to be used.
Constraint: N=1, 2, 3, 4, 5, 6, 8, 10, 12, 14, 16, 20, 24, 32, 48 or 64.
5:     FUN – REAL (KIND=nag_wp) FUNCTION, supplied by the user.External Procedure
FUN must return the value of the integrand f at a specified point.
The specification of FUN is:
FUNCTION FUN ( X)
REAL (KIND=nag_wp) FUN
REAL (KIND=nag_wp)  X
1:     X – REAL (KIND=nag_wp)Input
On entry: the point at which the integrand f must be evaluated.
FUN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which D01BAF is called. Parameters denoted as Input must not be changed by this procedure.
Some points to bear in mind when coding FUN are mentioned in Section 7.
6:     IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, because for this routine the values of the output parameters may be useful even if IFAIL0 on exit, the recommended value is -1. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Note: D01BAF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
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
The value of A and/or B is invalid.
Rational Gauss: A+B=0.0.
Gauss–Laguerre: B=0.0.
Gauss–Hermite: B0.0.
If the soft fail option is used, the answer is returned as zero.

7  Accuracy

The accuracy depends on the behaviour of the integrand, and on the number of abscissae used. No tests are carried out in D01BAF to estimate the accuracy of the result. If such an estimate is required, the routine 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
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 as:
fx=exp-bx+lngx
Another situation requiring care is exemplified by
- +e-x2xmdx=0,  m​ odd.
The integrand here assumes very large values; for example, for m=63, the peak value exceeds 3×1033. 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 1033-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. In general, you should be aware of the order of magnitude of the integrand, and should judge the answer in that light.

8  Further Comments

The time taken by D01BAF depends on the complexity of the expression for the integrand and on the number of abscissae required.

9  Example

This example evaluates the integrals
0141+x2 dx=π
by Gauss–Legendre quadrature;
2 1x2 lnx dx =0.378671
by rational Gauss quadrature with b=0;
2e-xxdx=0.048901
by Gauss–Laguerre quadrature with b=1; and
- +e-3x2-4x-1dx=- +e-3 x+1 2e2x+2dx=1.428167
by Gauss–Hermite quadrature with a=-1 and b=3.
The formulae with n=4,8,16 are used in each case.

9.1  Program Text

Program Text (d01bafe.f90)

9.2  Program Data

None.

9.3  Program Results

Program Results (d01bafe.r)


D01BAF (PDF version)
D01 Chapter Contents
D01 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012