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
 $I = ∫ x1 xn yx dx ,$
where the function $y$ is specified at the $n$-points ${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$ is less than $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:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
The values of the independent variable, i.e., the ${x}_{1},{x}_{2},\dots ,{x}_{n}$.
Constraint: either ${\mathbf{x}}\left(1\right)<{\mathbf{x}}\left(2\right)<\cdots <{\mathbf{x}}\left({\mathbf{n}}\right)$ or ${\mathbf{x}}\left(1\right)>{\mathbf{x}}\left(2\right)>\cdots >{\mathbf{x}}\left({\mathbf{n}}\right)$.
2:     $\mathrm{y}\left({\mathbf{n}}\right)$ – double array
The values of the dependent variable ${y}_{\mathit{i}}$ at the points ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.

### Optional Input Parameters

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

### Output Parameters

1:     $\mathrm{ans}$ – double scalar
The estimated value of the integral.
2:     $\mathrm{er}$ – double scalar
An estimate of the uncertainty in ans.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\mathbf{ifail}}=1$
Indicates that fewer than four points have been supplied to nag_quad_1d_data (d01ga).
${\mathbf{ifail}}=2$
Values of x are neither strictly increasing nor strictly decreasing.
${\mathbf{ifail}}=3$
Two points have the same X-value.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
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, $\left|I-{\mathbf{ans}}\right|$. 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$.
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

This example evaluates the integral
 $∫01 4 1+x2 dx = π$
reading in the function values at $21$ unequally spaced points.
```function d01ga_example

fprintf('d01ga example results\n\n');

x = [0:0.04:1];
y = 4./(1+x.^2);

[ans, err, ifail] = d01ga(x, y);
fprintf('Integral =  %9.6f     Estimated error = %9.2e\n',ans,err);

```
```d01ga example results

Integral =   3.141593     Estimated error = -3.19e-07
```