Integer type:** int32**** int64**** nag_int** show int32 show int32 show int64 show int64 show nag_int show nag_int

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

nag_quad_1d_indef (d01ar) evaluates definite and indefinite integrals of the form:

using the method described in Patterson (1968).

$$\underset{a}{\overset{b}{\int}}f\left(t\right)dt$$ |

In this case nag_quad_1d_indef (d01ar) must be called with iparm = 0${\mathbf{iparm}}=0$. By linear transformation the integral is changed to

where

and is then approximated by an n$n$-point quadrature rule

where w_{k}${w}_{k}$ are the weights and x_{k}${x}_{k}$ are the abscissae.

$$I=\underset{-1}{\overset{+1}{\int}}F\left(x\right)dx$$ |

F(x) = (b − a)/2 f
((b + a + (b − a)x)/2)
$$F\left(x\right)=\frac{b-a}{2}f\left(\frac{b+a+(b-a)x}{2}\right)$$ |

$$I=\sum _{k=1}^{n}{w}_{k}F\left({x}_{k}\right)$$ |

The function uses a family of nine interlacing rules based on the optimal extension of the three-point Gauss rule. These rules use 1$1$, 3$3$, 7$7$, 15$15$, 31$31$, 63$63$, 127$127$, 255$255$ and 511$511$ points and have respective polynomial integrating degrees 1$1$, 5$5$, 11$11$, 23$23$, 47$47$, 95$95$, 191$191$, 383$383$ and 767$767$. 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.

Suppose the value of the integral

is required for a number of sub-intervals [c,d]$[c,d]$, all of which lie in an interval [a,b]$[a,b]$.

$$\underset{c}{\overset{d}{\int}}f\left(t\right)dt$$ |

In this case nag_quad_1d_indef (d01ar) should first be called with the parameter iparm = 1${\mathbf{iparm}}=1$ and the interval set to [a,b]$[a,b]$. The function then calculates the integral over [a,b]$[a,b]$ **and** the Legendre expansion of the integrand, using the same integrand values. If the function is subsequently called with iparm = 2${\mathbf{iparm}}=2$ and the interval set to [c,d]$[c,d]$, the integral over [c,d]$[c,d]$ is calculated by analytical integration of the Legendre expansion, without further evaluations of the integrand.

For the interval [ − 1,1]$[-1,1]$ the expansion takes the form

where P_{i}(x)${P}_{i}\left(x\right)$ is the order i$i$ Legendre polynomial. Assuming that the integral over the full range [ − 1,1]$[-1,1]$ was evaluated to the required accuracy using an n$n$-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 F(x)$F\left(x\right)$ is a polynomial of degree ≤ m$\text{}\le m$.

$$F\left(x\right)=\sum _{i=0}^{\infty}{\alpha}_{i}{P}_{i}\left(x\right)$$ |

$${\alpha}_{i}=\frac{1}{2}(2i-1)\underset{-1}{\overset{+1}{\int}}{P}_{i}\left(x\right)F\left(x\right)dx\text{, \hspace{1em}}i=0,1,\dots ,m$$ |

m = (3n − 1) / 4.
$$m=(3n-1)/4\text{.}$$ |

Patterson T N L (1968) The Optimum addition of points to quadrature formulae *Math. Comput.* **22** 847–856

- 1: a – double scalar
- a$a$, the lower limit of integration.
- 2: b – double scalar
- b$b$, the upper limit of integration. It is not necessary that a < b$a<b$.
- 3: fun – function handle or string containing name of m-file
- [result] = fun(x)
**Input Parameters****Output Parameters** - 4: relacc – double scalar
- The relative accuracy required. If convergence according to absolute accuracy is required, relacc should be set to zero (but see also Section [Accuracy]). If relacc < 0.0${\mathbf{relacc}}<0.0$, its absolute value is used.
- 5: absacc – double scalar
- The absolute accuracy required. If convergence according to relative accuracy is required, absacc should be set to zero (but see also Section [Accuracy]). If absacc < 0.0${\mathbf{absacc}}<0.0$, its absolute value is used.
- 6: maxrul – int64int32nag_int scalar
- The maximum number of successive rules that may be used.
- 7: iparm – int64int32nag_int scalar
- Indicates the task to be performed by the function.
- iparm = 0${\mathbf{iparm}}=0$
- Only the definite integral over [a,b]$[a,b]$ is evaluated.
- iparm = 1${\mathbf{iparm}}=1$
- As well as the definite integral, the expansion of the integrand in Legendre polynomials over [a,b]$[a,b]$ 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.
- iparm = 2${\mathbf{iparm}}=2$
- f(t)$f\left(t\right)$ is integrated analytically over [a,b]$[a,b]$ using the previously computed expansion, stored in alpha. No further evaluations of the integrand are required. The function must previously have been called with iparm = 1${\mathbf{iparm}}=1$ and the interval [a,b]$[a,b]$ must lie within that specified for the previous call. In this case only the arguments a, b, iparm, ans, alpha and ifail are used.

- 8: alpha(390$390$) – double array

None.

None.

- 1: acc – double scalar
- 2: ans – double scalar
- The estimated value of the integral.
- 3: n – int64int32nag_int scalar
- 4: alpha(390$390$) – double array
- If iparm = 1${\mathbf{iparm}}=1$, the first m$m$ elements of alpha hold the coefficients of the Legendre expansion of the integrand, and the value of m$m$ is stored in alpha(390)${\mathbf{alpha}}\left(390\right)$. alpha must not be changed between a call with iparm = 1${\mathbf{iparm}}=1$ and subsequent calls with iparm = 2${\mathbf{iparm}}=2$.
- 5: ifail – int64int32nag_int scalar
- ifail = 0${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and 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 = 1${\mathbf{ifail}}=1$- If iparm = 0${\mathbf{iparm}}=0$ or 1$1$, 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 evaluateIf iparm = 2${\mathbf{iparm}}=2$, this indicates failure of convergence during the run with iparm = 1${\mathbf{iparm}}=1$ in which the Legendre expansion was created.
b c b ∫ f(t)dt as ∫ f(t)dt + ∫ f(t)dt for some a < c < b. a a c $$\underset{a}{\overset{b}{\int}}f\left(t\right)dt\text{\hspace{1em} as \hspace{1em}}\underset{a}{\overset{c}{\int}}f\left(t\right)dt+\underset{c}{\overset{b}{\int}}f\left(t\right)dt\text{\hspace{1em} for some}a<c<b\text{.}$$

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 parameter for the other to zero. If both relacc and absacc are specified as zero, then the function uses the value 10.0 × (machine precision)$10.0\times \left(\mathit{machineprecision}\right)$ for relacc.

If on exit ifail = 0${\mathbf{ifail}}={\mathbf{0}}$, then it is likely that the result is correct to one or other of these accuracies. If on exit ifail = 1${\mathbf{ifail}}={\mathbf{1}}$, 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 parameter to a small positive value (e.g., 10^{ − 9}${10}^{-9}$ for an implementation of 11$11$-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 10^{ − 3}${10}^{-3}$ be avoided.

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.

Open in the MATLAB editor: nag_quad_1d_indef_example

function nag_quad_1d_indef_examplea = 0; b = 1; fun = @(x) 4/(1+x^2); relacc = 0; absacc = 1e-05; maxrul = int64(0); iparm = int64(0); alpha = zeros(390,1); [acc, ans, n, alphaOut, ifail] = ... nag_quad_1d_indef(a, b, fun, relacc, absacc, maxrul, iparm, alpha); acc, ans, n, ifail

acc = 1.8377e-08 ans = 3.1416 n = 15 ifail = 0

Open in the MATLAB editor: d01ar_example

function d01ar_examplea = 0; b = 1; fun = @(x) 4/(1+x^2); relacc = 0; absacc = 1e-05; maxrul = int64(0); iparm = int64(0); alpha = zeros(390,1); [acc, ans, n, alphaOut, ifail] = ... d01ar(a, b, fun, relacc, absacc, maxrul, iparm, alpha); acc, ans, n, ifail

acc = 1.8377e-08 ans = 3.1416 n = 15 ifail = 0

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