PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_quad_1d_indef (d01ar)
Purpose
nag_quad_1d_indef (d01ar) computes definite and indefinite integrals over a finite range to a specified relative or absolute accuracy, using the method described in
Patterson (1968).
Syntax
[
acc,
ans,
n,
alpha,
ifail] = d01ar(
a,
b,
fun,
relacc,
absacc,
maxrul,
iparm,
alpha)
[
acc,
ans,
n,
alpha,
ifail] = nag_quad_1d_indef(
a,
b,
fun,
relacc,
absacc,
maxrul,
iparm,
alpha)
Description
nag_quad_1d_indef (d01ar) evaluates definite and indefinite integrals of the form:
using the method described in
Patterson (1968).
Definite Integrals
In this case
nag_quad_1d_indef (d01ar) must be called with
. By linear transformation the integral is changed to
where
and is then approximated by an
-point quadrature rule
where
are the weights and
are the abscissae.
The function uses a family of nine interlacing rules based on the optimal extension of the three-point Gauss rule. These rules use , , , , , , , and points and have respective polynomial integrating degrees , , , , , , , and . Each rule has the property that the next in sequence includes all the points of its predecessor and has the greatest possible increase in integrating degree.
The integration method is based on the successive application of these rules until the absolute value of the difference of two successive results differs by not more than
absacc, or relatively by not more than
relacc. The result of the last rule used is taken as the value of the integral (
ans), and the absolute difference of the results of the last two rules used is taken as an estimate of the absolute error (
acc). Due to their interlacing form no integrand evaluations are wasted in passing from one rule to the next.
Indefinite Integrals
Suppose the value of the integral
is required for a number of sub-intervals
, all of which lie in an interval
.
In this case nag_quad_1d_indef (d01ar) should first be called with the argument and the interval set to . The function then calculates the integral over and the Legendre expansion of the integrand, using the same integrand values. If the function is subsequently called with and the interval set to , the integral over is calculated by analytical integration of the Legendre expansion, without further evaluations of the integrand.
For the interval
the expansion takes the form
where
is the order
Legendre polynomial. Assuming that the integral over the full range
was evaluated to the required accuracy using an
-point rule, then the coefficients
are evaluated by that same rule, up to
The accuracy for indefinite integration should be of the same order as that obtained for the definite integral over the full range. The indefinite integrals will be exact when
is a polynomial of degree
.
References
Patterson T N L (1968) The Optimum addition of points to quadrature formulae Math. Comput. 22 847–856
Parameters
Compulsory Input Parameters
- 1:
– double scalar
-
, the lower limit of integration.
- 2:
– double scalar
-
, the upper limit of integration. It is not necessary that .
- 3:
– 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 in at which the integrand must be evaluated.
Output Parameters
- 1:
– double scalar
-
The value of
evaluated at
x.
If
,
fun is not called.
- 4:
– double scalar
-
The relative accuracy required. If convergence according to absolute accuracy is required,
relacc should be set to zero (but see also
Accuracy). If
, its absolute value is used.
If
,
relacc is not used.
- 5:
– double scalar
-
The absolute accuracy required. If convergence according to relative accuracy is required,
absacc should be set to zero (but see also
Accuracy). If
, its absolute value is used.
If
,
absacc is not used.
- 6:
– int64int32nag_int scalar
-
The maximum number of successive rules that may be used.
Constraint:
. If
maxrul is outside these limits, the value
is assumed.
If
,
maxrul is not used.
- 7:
– int64int32nag_int scalar
-
Indicates the task to be performed by the function.
- Only the definite integral over is evaluated.
- As well as the definite integral, the expansion of the integrand in Legendre polynomials over is calculated, using the same values of the integrand as used to compute the integral. The expansion coefficients, and some other quantities, are returned in alpha for later use in computing indefinite integrals.
- is integrated analytically over using the previously computed expansion, stored in alpha. No further evaluations of the integrand are required. The function must previously have been called with and the interval must lie within that specified for the previous call. In this case only the arguments a, b, iparm, ans, alpha and ifail are used.
Constraint:
, or .
- 8:
– double array
-
If
,
alpha must contain the coefficients of the Legendre expansions of the integrand, as returned by a previous call of
nag_quad_1d_indef (d01ar) with
and a range containing the present range.
If
or
,
alpha need not be set on entry.
Optional Input Parameters
None.
Output Parameters
- 1:
– double scalar
-
If
or
,
acc contains the absolute value of the difference between the last two successive estimates of the integral. This may be used as a measure of the accuracy actually achieved.
If
,
acc is not used.
- 2:
– double scalar
-
The estimated value of the integral.
- 3:
– int64int32nag_int scalar
-
When
or
,
n contains the number of integrand evaluations used in the calculation of the integral.
If
,
n is not used.
- 4:
– double array
-
If
, the first
elements of
alpha hold the coefficients of the Legendre expansion of the integrand, and the value of
is stored in
.
alpha must not be changed between a call with
and subsequent calls with
.
If
, the first
elements of
alpha are unchanged on exit.
- 5:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Note: nag_quad_1d_indef (d01ar) 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
-
If
or
, this indicates that all
maxrul rules have been used and the integral has not converged to the accuracy requested. In this case
ans contains the last approximation to the integral, and
acc contains the difference between the last two approximations. To check this estimate of the integral,
nag_quad_1d_indef (d01ar) could be called again to evaluate
If
, this indicates failure of convergence during the run with
in which the Legendre expansion was created.
-
-
On entry, , or
-
-
The function is called with but a previous call with has been omitted or was invoked with an integration interval of length zero.
-
-
On entry, with , the interval for indefinite integration is not contained within the interval specified when nag_quad_1d_indef (d01ar) was previously called with .
-
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 relative or absolute accuracy required is specified by you in the variables
relacc or
absacc.
nag_quad_1d_indef (d01ar) will terminate whenever either the relative accuracy specified by
relacc or the absolute accuracy specified by
absacc is reached. One or other of these criteria may be ‘forced’ by setting the argument for the other to zero. If both
relacc and
absacc are specified as zero, then the function uses the value
for
relacc.
If on exit , then it is likely that the result is correct to one or other of these accuracies. If on exit , then it is likely that neither of the requested accuracies has been reached.
When you have no prior idea of the magnitude of the integral, it is possible that an unreasonable accuracy may be requested, e.g., a relative accuracy for an integral which turns out to be zero, or a small absolute accuracy for an integral which turns out to be very large. Even if failure is reported in such a case, the value of the integral may still be satisfactory. The device of setting the other ‘unused’ accuracy argument to a small positive value (e.g., for an implementation of -digit precision) rather than zero, may prevent excessive calculation in such a situation.
To avoid spurious convergence, it is recommended that relative accuracies larger than about be avoided.
Further Comments
The time taken by nag_quad_1d_indef (d01ar) depends on the complexity of the integrand and the accuracy required.
This function uses the Patterson method over the whole integration interval and should therefore be suitable for well behaved functions. However, for very irregular functions it would be more efficient to submit the differently behaved regions separately for integration.
Example
This example evaluates the following integrals
(i) |
Definite integral only for
|
(ii) |
Definite integral together with expansion coefficients for
|
(iii) |
Indefinite integral using previous expansion for
|
Open in the MATLAB editor:
d01ar_example
function d01ar_example
fprintf('d01ar example results\n\n');
absacc = 1e-05;
relacc = 0;
maxrul = int64(0);
alpha = zeros(390,1);
iparm = int64(0);
a = 0;
b = 1;
[acc1, ans1, n1, alpha, ifail] = ...
d01ar(...
a, b, @f1, relacc, absacc, maxrul, iparm, alpha);
fprintf('Definite integral of 4/(1+x*x) over (0,1)\n');
fprintf('Estimated value of the integral = %9.5f\n', ans1);
fprintf('Estimated absolute error = %10.1e\n', acc1);
fprintf('Number of points used = %4d\n\n', n1);
iparm = int64(1);
a = 1;
b = 2;
[acc2, ans2, n2, alpha, ifail] = ...
d01ar(...
a, b, @f2, relacc, absacc, maxrul, iparm, alpha);
fprintf('Definite integral of x^(1/8) over (1,2)\n');
fprintf('Estimated value of the integral = %9.5f\n', ans2);
fprintf('Estimated absolute error = %10.1e\n', acc2);
fprintf('Number of points used = %4d\n\n', n2);
iparm = int64(2);
a = 1.2;
b = 1.8;
[acc3, ans3, n3, alpha, ifail] = ...
d01ar(...
a, b, @f2, relacc, absacc, maxrul, iparm, alpha);
fprintf('Indefinite integral of x^(1/8) over (1.2,1.8)\n');
fprintf('Estimated value of the integral = %9.5f\n', ans3);
function f = f1(x)
f = 4/(1+x^2);
function f = f2(x)
f = x^(1/8);
d01ar example results
Definite integral of 4/(1+x*x) over (0,1)
Estimated value of the integral = 3.14159
Estimated absolute error = 1.8e-08
Number of points used = 15
Definite integral of x^(1/8) over (1,2)
Estimated value of the integral = 1.04979
Estimated absolute error = 5.9e-07
Number of points used = 7
Indefinite integral of x^(1/8) over (1.2,1.8)
Estimated value of the integral = 0.63073
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015