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

Chapter Contents
Chapter Introduction
NAG Toolbox

## Purpose

nag_quad_1d_data (d01ga) integrates a function which is specified numerically at four or more points, over the whole of its specified range, using third-order finite difference formulae with error estimates, according to a method due to Gill and Miller (1972).

## Syntax

[ans, er, ifail] = d01ga(x, y, 'n', n)
[ans, er, ifail] = nag_quad_1d_data(x, y, 'n', n)

## Description

nag_quad_1d_data (d01ga) evaluates the definite integral
 xn I = ∫ y(x)dx, x1
$I = ∫ x1 xn y(x) dx ,$
where the function y$y$ is specified at the n$n$-points x1,x2,,xn${x}_{1},{x}_{2},\dots ,{x}_{n}$, which should be all distinct, and in either ascending or descending order. The integral between successive points is calculated by a four-point finite difference formula centred on the interval concerned, except in the case of the first and last intervals, where four-point forward and backward difference formulae respectively are employed. If n$n$ is less than 4$4$, the function fails. An approximation to the truncation error is integrated and added to the result. It is also returned separately to give an estimate of the uncertainty in the result. The method is due to Gill and Miller (1972).

## References

Gill P E and Miller G F (1972) An algorithm for the integration of unequally spaced data Comput. J. 15 80–83

## Parameters

### Compulsory Input Parameters

1:     x(n) – double array
n, the dimension of the array, must satisfy the constraint n4${\mathbf{n}}\ge 4$.
The values of the independent variable, i.e., the x1,x2,,xn${x}_{1},{x}_{2},\dots ,{x}_{n}$.
Constraint: either x(1) < x(2) < < x(n)${\mathbf{x}}\left(1\right)<{\mathbf{x}}\left(2\right)<\cdots <{\mathbf{x}}\left({\mathbf{n}}\right)$ or x(1) > x(2) > > x(n)${\mathbf{x}}\left(1\right)>{\mathbf{x}}\left(2\right)>\cdots >{\mathbf{x}}\left({\mathbf{n}}\right)$.
2:     y(n) – double array
n, the dimension of the array, must satisfy the constraint n4${\mathbf{n}}\ge 4$.
The values of the dependent variable yi${y}_{\mathit{i}}$ at the points xi${x}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the arrays x, y. (An error is raised if these dimensions are not equal.)
n$n$, the number of points.
Constraint: n4${\mathbf{n}}\ge 4$.

None.

### Output Parameters

1:     ans – double scalar
The estimated value of the integral.
2:     er – double scalar
An estimate of the uncertainty in ans.
3:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
Indicates that fewer than four points have been supplied to nag_quad_1d_data (d01ga).
ifail = 2${\mathbf{ifail}}=2$
Values of x are neither strictly increasing nor strictly decreasing.
ifail = 3${\mathbf{ifail}}=3$
Two points have the same X-value.
No error is reported arising from the relative magnitudes of ans and er on return, due to the difficulty when the true answer is zero.

## Accuracy

No accuracy level is specified by you before calling nag_quad_1d_data (d01ga) but on return the absolute value of er is an approximation to, but not necessarily a bound for, |Ians|$|I-{\mathbf{ans}}|$. If on exit ${\mathbf{ifail}}>{\mathbf{0}}$, both ans and er are returned as zero.

The time taken by nag_quad_1d_data (d01ga) depends on the number of points supplied, n$n$.
In their paper, Gill and Miller (1972) do not add the quantity er to ans before return. However, extensive tests have shown that a dramatic reduction in the error often results from such addition. In other cases, it does not make an improvement, but these tend to be cases of low accuracy in which the modified answer is not significantly inferior to the unmodified one. You have the option of recovering the Gill–Miller answer by subtracting er from ans on return from the function.

## Example

```function nag_quad_1d_data_example
x = [0;
0.04;
0.08;
0.12;
0.22;
0.26;
0.3;
0.38;
0.39;
0.42;
0.45;
0.46;
0.6;
0.68;
0.72;
0.73;
0.83;
0.85;
0.88;
0.9;
1];
y = [4;
3.9936;
3.9746;
3.9432;
3.8153;
3.7467;
3.6697;
3.4943;
3.4719;
3.4002;
3.3264;
3.3014;
2.9412;
2.7352;
2.6344;
2.6094;
2.3684;
2.3222;
2.2543;
2.2099;
2];
[ans, er, ifail] = nag_quad_1d_data(x, y)
```
```

ans =

3.1414

er =

-5.9352e-05

ifail =

0

```
```function d01ga_example
x = [0;
0.04;
0.08;
0.12;
0.22;
0.26;
0.3;
0.38;
0.39;
0.42;
0.45;
0.46;
0.6;
0.68;
0.72;
0.73;
0.83;
0.85;
0.88;
0.9;
1];
y = [4;
3.9936;
3.9746;
3.9432;
3.8153;
3.7467;
3.6697;
3.4943;
3.4719;
3.4002;
3.3264;
3.3014;
2.9412;
2.7352;
2.6344;
2.6094;
2.3684;
2.3222;
2.2543;
2.2099;
2];
[ans, er, ifail] = d01ga(x, y)
```
```

ans =

3.1414

er =

-5.9352e-05

ifail =

0

```