NAG Library Function Document
nag_1d_quad_gauss_1 (d01tac)
1 Purpose
nag_1d_quad_gauss_1 (d01tac) 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
#include <nag.h> |
#include <nagd01.h> |
double |
nag_1d_quad_gauss_1 (Nag_GaussFormulae quadrule,
double |
(*f)(double x,
Nag_User *comm),
|
|
double a,
double b,
Integer npts,
Nag_User *comm,
NagError *fail) |
|
3 Description
3.1 General
nag_1d_quad_gauss_1 (d01tac) evaluates an estimate of the definite integral of a function
, over a finite or infinite interval, by
-point Gaussian quadrature (see
Davis and Rabinowitz (1967),
Fröberg (1970),
Ward (1975) or
Stroud and Secrest (1966)). The integral is approximated by a summation
where
are called the weights, and the
the abscissae. A selection of values of
is available. (See
Section 5.)
3.2 Both Limits Finite
The Gauss–Legendre weights and abscissae are used, and the formula is exact for any function of the form:
The formula is appropriate for functions which can be well approximated by such a polynomial over
. It is inappropriate for functions with algebraic singularities at one or both ends of the interval, such as
on
.
3.3 One Limit Infinite
Two quadrature formulae are available for these integrals:
(a) |
The Gauss–Laguerre formula is exact for any function of the form:
|
(b) |
This formula is appropriate for functions decaying exponentially at infinity; the argument should be chosen if possible to match the decay rate of the function. |
(c) |
The rational Gauss formula is exact for any function of the form:
|
(d) |
This formula is likely to be more accurate for functions having only an inverse power rate of decay for large . Here the choice of a suitable value of may be more difficult; unfortunately a poor choice of can make a large difference to the accuracy of the computed integral. |
3.4 Both Limits Infinite
The Gauss–Hermite weights and abscissae are used, and the formula is exact for any function of the form:
Again, for general functions not of this exact form, the argument
should be chosen to match if possible the decay rate at
.
4 References
Davis P J and Rabinowitz P (1967) Numerical Integration 33–52 Blaisdell Publishing Company
Fröberg C E (1970) Introduction to Numerical Analysis Addison–Wesley
Stroud A H and Secrest D (1966) Gaussian Quadrature Formulas Prentice–Hall
Ward R C (1975) The combination shift algorithm SIAM J. Numer. Anal. 12 835–853
5 Arguments
- 1:
quadrule – Nag_GaussFormulaeInput
On entry: indicates the quadrature formula:
- , for Gauss–Legendre quadrature on a finite interval;
- , for rational Gauss quadrature on a semi-infinite interval;
- , for Gauss–Laguerre quadrature on a semi-infinite interval;
- , for Gauss–Hermite quadrature on an infinite interval.
Constraint:
, , or .
- 2:
f – function, supplied by the userExternal Function
-
f must return the value of the integrand
at a given point.
The specification of
f is:
double |
f (double x,
Nag_User *comm)
|
|
- 1:
x – doubleInput
-
On entry: the point at which the integrand must be evaluated.
- 2:
comm – Nag_User *
-
Pointer to a structure of type Nag_User with the following member:
- p – Pointer
-
On entry/exit: the pointer
should be cast to the required type, e.g.,
struct user *s = (struct user *)comm → p, to obtain the original object's address with appropriate type. (See the argument
comm below.)
Some points to bear in mind when coding
f are mentioned in
Section 9.
- 3:
a – doubleInput
- 4:
b – doubleInput
-
On entry: the arguments
and
which occur in the integration formulae:
Gauss–Legendre: is the lower limit and is the upper limit of the integral. It is not necessary that .
Rational Gauss:
must be chosen so as to make the integrand match as closely as possible the exact form given in
Section 3. The interval of integration is
if
, and
if
.
Gauss–Laguerre:
must be chosen so as to make the integrand match as closely as possible the exact form given in
Section 3. The interval of integration is
if
, and
if
.
Gauss–Hermite:
and
must be chosen so as to make the integrand match as closely as possible the exact form given in
Section 3.
Constraints:
- Rational Gauss: ;
- Gauss–Laguerre: ;
- Gauss–Hermite: .
- 5:
npts – IntegerInput
-
On entry: the number of abscissae to be used, .
Constraint:
, , , , , , , , , , , , , , or .
- 6:
comm – Nag_User *
-
Pointer to a structure of type Nag_User with the following member:
- p – Pointer
-
On entry/exit: the pointer
, of type Pointer, allows you to communicate information to and from
f(). An object of the required type should be declared, e.g., a structure, and its address assigned to the pointer
by means of a cast to Pointer in the calling program, e.g.,
comm.p = (Pointer)&s. The type pointer will be
void * with a C compiler that defines
void * and
char * otherwise.
- 7:
fail – NagError *Input/Output
-
The NAG error argument (see
Section 3.6 in the Essential Introduction).
6 Error Indicators and Warnings
- NE_BAD_PARAM
-
On entry, argument
quadrule had an illegal value.
- NE_QUAD_GAUSS_CONS
-
Gauss–Hermite input is invalid with .
Constraint: .
Gauss–Laguerre input is invalid with .
Constraint: .
Rational Gauss input is invalid with .
Constraint: .
The answer is returned as zero.
- NE_QUAD_GAUSS_NPTS_RULE
-
The
-point rule is not among those stored.
The answer is evaluated for
, the largest possible value of
npts less than the requested value,
.
7 Accuracy
The accuracy depends on the behaviour of the integrand, and on the number of abscissae used. No tests are carried out in nag_1d_quad_gauss_1 (d01tac) 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 interval 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 interval.
The coding of the function
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
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:
Another situation requiring care is exemplified by
The integrand here assumes very large values; for example, for
, the peak value exceeds
. Now, if the machine holds floating-point numbers to an accuracy of
significant decimal digits, we could not expect such terms to cancel in the summation leaving an answer of much less than
(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
. In general, you should be aware of the order of magnitude of the integrand, and should judge the answer in that light.
8 Parallelism and Performance
Not applicable.
The time taken by nag_1d_quad_gauss_1 (d01tac) depends on the complexity of the expression for the integrand and on the number of abscissae required.
10 Example
This example evaluates the integrals
by Gauss–Legendre quadrature;
by rational Gauss quadrature with
;
by Gauss–Laguerre quadrature with
; and
by Gauss–Hermite quadrature with
and
.
The formulae with are used in each case.
10.1 Program Text
Program Text (d01tace.c)
10.2 Program Data
None.
10.3 Program Results
Program Results (d01tace.r)