PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_quad_withdraw_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
Description
General
nag_quad_1d_gauss (d01ba) evaluates an estimate of the definite integral of a function
, over a finite or infinite range, by
-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
where the
are called the weights, and the
the abscissae. A selection of values of
is available. (See
Arguments.)
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
.
One Limit Infinite
Two quadrature formulae are available for these integrals.
(a) |
The Gauss–Laguerre formula is exact for any function of the form:
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. |
(b) |
The rational Gauss formula is exact for any function of the form:
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. |
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
.
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:
– 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:
– double scalar
- 3:
– double scalar
-
The parameters
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 One Limit Infinite(b). The range 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 One Limit Infinite(a). The range of integration is if , and is .
- Gauss–Hermite:
- and must be chosen so as to make the integrand match as closely as possible the exact form given in Both Limits Infinite.
Constraints:
- Rational Gauss: ;
- Gauss–Laguerre: ;
- Gauss–Hermite: .
- 4:
– int64int32nag_int scalar
-
, the number of abscissae to be used.
Constraint:
, , , , , , , , , , , , , , or .
- 5:
– function handle or string containing name of m-file
-
fun must return the value of the integrand
at a specified point.
[result] = fun(x)
Input Parameters
- 1:
– double scalar
-
The point at which the integrand must be evaluated.
Output Parameters
- 1:
– double scalar
-
The value of
evaluated at
x.
Some points to bear in mind when coding
fun are mentioned in
Accuracy.
Optional Input Parameters
None.
Output Parameters
- 1:
– double scalar
The result of the function.
- 2:
– int64int32nag_int scalar
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
-
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.
-
-
The value of
a and/or
b is invalid.
Rational Gauss: .
Gauss–Laguerre: .
Gauss–Hermite: .
If the soft fail option is used, the answer is returned as zero.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
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 (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
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.
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
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.
Open in the MATLAB editor:
d01ba_example
function d01ba_example
fprintf('d01ba example results\n\n');
global funid;
for funid=1:4
switch funid
case 1
fprintf('\nGauss-Legendre example\n');
a = 0.0;
b = 1.0;
key = 'd01baz';
case 2
fprintf('\nRational Gauss example\n');
a = 2.0;
b = 0.0;
key = 'd01bay';
case 3
fprintf('\nGauss-Laguerre example\n');
a = 2.0;
b = 1.0;
key = 'd01bax';
case 4
fprintf('\nGauss-Hermite example\n');
a = -1.0;
b = 3.0;
key = 'd01baw';
end
for i=1:3
n = int64(2*2^i);
[dinest, ifail] = d01ba(key, a, b, n, @f);
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)
global funid;
switch funid
case 1
fv = 4./(1+x.*x);
case 2
fv = 1./(x.*x.*log(x));
case 3
fv = exp(-x)./x;
case 4
fv = exp(-3.*x.*x-4.*x-1);
otherwise
fv = zeros(nx, 1);
iflag = -1;
end
d01ba example results
Gauss-Legendre example
4 Points Answer = 3.14161
8 Points Answer = 3.14159
16 Points Answer = 3.14159
Rational Gauss example
4 Points Answer = 0.37910
8 Points Answer = 0.37876
16 Points Answer = 0.37869
Gauss-Laguerre example
4 Points Answer = 0.04887
8 Points Answer = 0.04890
16 Points Answer = 0.04890
Gauss-Hermite example
4 Points Answer = 1.42803
8 Points Answer = 1.42817
16 Points Answer = 1.42817
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015