Accelerating the pace of engineering and science

# trapz

Trapezoidal numerical integration

## Description

example

Q = trapz(Y) returns the approximate integral of Y via the trapezoidal method with unit spacing. The size of Y determines the dimension to integrate along:

• If Y is a vector, then trapz(Y) is the approximate integral of Y.

• If Y is a matrix, then trapz(Y) integrates over each column and returns a row vector of integration values.

• If Y is a multidimensional array, then trapz(Y) integrates over the first dimension whose size does not equal 1. The size of this dimension becomes 1, and the sizes of other dimensions remain unchanged.

example

Q = trapz(X,Y) integrates Y with spacing increment X. By default, trapz operates on the first dimension of Y whose size does not equal 1. length(X) must be equal to the size of this dimension . If X is a scalar, then trapz(X,Y) is equivalent to X*trapz(Y).

example

Q = trapz(___,dim) integrates along the dimension dim using any of the previous syntaxes. You must specify Y, and optionally can specify X. The length of X, if specified, must be the same as size(Y,dim). For example, if Y is a matrix, then trapz(X,Y,2) integrates each row of Y.

## Examples

expand all

### Integrate Vector of Data with Unit Spacing

Create a numeric vector of data.

`Y = [1 4 9 16 25];`

Y contains five evenly spaced points from the function $f\left(x\right)={x}^{2}\text{\hspace{0.17em}}\text{\hspace{0.17em}}.$

Use trapz to integrate the data points with unit spacing.

`Q = trapz(Y)`
```Q =

42```

This approximate integration yields a value of 42. In this case, the exact answer is a little less, $41\frac{1}{3}\text{\hspace{0.17em}}\text{\hspace{0.17em}}.$ The trapz function overestimates the value of the integral because f(x) is concave up.

### Integrate Vector of Data with Nonunit Spacing

Create a domain vector, X.

`X = 0:pi/100:pi;`

Calculate the sine of X and store the result in Y.

`Y = sin(X);`

Integrate the function values contained in Y using trapz.

`Q = trapz(X,Y)`
```Q =

1.9998```

When the spacing between points is constant, but not equal to 1, you can multiply by the spacing value, in this case pi/100*trapz(Y). The answer is the same if you pass the value directly to the function with trapz(X,Y).

### Integrate Matrix with Nonuniform Spacing

Create a vector of time values, X. Also create a matrix, Y, containing values evaluated at the irregular intervals in X.

```X = [1 2.5 7 10]';
Y = [5.2 4.8 4.9 5.1; 7.7 7.0 6.5 6.8; 9.6 10.5 10.5 9.0; 13.2 14.5 13.8 15.2]```
```Y =

5.2000    4.8000    4.9000    5.1000
7.7000    7.0000    6.5000    6.8000
9.6000   10.5000   10.5000    9.0000
13.2000   14.5000   13.8000   15.2000```

The columns of Y represent velocity data, taken at the times contained in X, for several different trials.

Use trapz to integrate each column independently and find the total distance traveled in each trial. Since the function values are not evaluated at constant intervals, specify X to indicate the spacing between the data points.

`Q = trapz(X,Y)`
```Q =

82.8000   85.7250   83.2500   80.7750```

The result is a row vector of integration values, one for each column in Y. By default, trapz integrates along the first dimension of Y whose size does not equal 1.

Alternatively, you can integrate the rows of a matrix by specifying dim = 2.

In this case, use trapz on Y', which contains the velocity data in the rows.

```dim = 2;
Q1 = trapz(X,Y',dim)```
```Q1 =

82.8000
85.7250
83.2500
80.7750```

The result is a column vector of integration values, one for each row in Y'.

### Multiple Numerical Integrations

Created a grid of domain values.

```x = -3:.1:3;
y = -5:.1:5;
[X,Y] = meshgrid(x,y); ```

Calculate the function $f\left(x,y\right)={x}^{2}+{y}^{2}$ over the grid.

`F = X.^2 + Y.^2;`

trapz integrates numeric data rather than functional expressions, so in general the expression does not need to be known to use trapz on a matrix of data.

Use trapz to approximate the double integral

$I=\underset{-5}{\overset{5}{\int }}\underset{-3}{\overset{3}{\int }}\left({x}^{2}+{y}^{2}\right)\text{\hspace{0.17em}}\text{\hspace{0.17em}}dx\text{\hspace{0.17em}}dy\text{\hspace{0.17em}}\text{\hspace{0.17em}}.$

To perform double or triple integrations on an array of numeric data, nest function calls to trapz.

`I = trapz(y,trapz(x,F,2))`
```I =

680.2000```

trapz performs the integration over x first, producing a column vector. Then, the integration over y reduces the column vector to a single scalar. trapz slightly overestimates the exact answer of 680 because f(x,y) is concave up.

## Input Arguments

expand all

### Y — Numeric datavector | matrix | multidimensional array

Numeric data, specified as a vector, matrix, or multidimensional array. By default, trapz integrates along the first dimension of Y whose size does not equal 1.

Data Types: single | double
Complex Number Support: Yes

### X — Point spacing1 (default) | uniform scalar spacing | vector of nonuniform spacings

Point spacing, specified as 1 (default), a uniform scalar spacing, or a vector of nonuniform spacings. If X is a vector, then length(X) must be the same as the size of the integration dimension in Y.

Data Types: single | double
Complex Number Support: Yes

### dim — Dimension to operate alongpositive integer scalar

Dimension to operate along, specified as a positive integer scalar. If no value is specified, the default is the first array dimension whose size does not equal 1.

Consider a two-dimensional input array, Y:

• trapz(Y,1) works on successive elements in the columns of Y and returns a row vector of integration values.

• trapz(Y,2) works on successive elements in the rows of Y and returns a column vector of integration values.

If dim is greater than ndims(Y), then trapz returns an array of zeros of the same size as Y.

expand all

### Trapezoidal Method

trapz performs numerical integration via the trapezoidal method. This method approximates the integration over an interval by breaking the area down into trapezoids with more easily computable areas.

For an integration with N+1 evenly spaced points, the approximation is

$\begin{array}{c}\underset{a}{\overset{b}{\int }}f\left(x\right)dx\text{\hspace{0.17em}}\text{\hspace{0.17em}}\approx \text{\hspace{0.17em}}\text{\hspace{0.17em}}\frac{b-a}{2N}\sum _{n=1}^{N}\left(f\left({x}_{n}\right)+f\left({x}_{n+1}\right)\right)\\ =\frac{b-a}{2N}\left[f\left({x}_{1}\right)+2f\left({x}_{2}\right)+...+2f\left({x}_{N}\right)+f\left({x}_{N+1}\right)\right]\text{\hspace{0.17em}}\text{\hspace{0.17em}},\end{array}$

where the spacing between each point is equal to the scalar value $\frac{b-a}{N}\text{\hspace{0.17em}}.$

If the spacing between the points is not constant, then the formula generalizes to

$\underset{a}{\overset{b}{\int }}f\left(x\right)dx\text{\hspace{0.17em}}\text{\hspace{0.17em}}\approx \text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\frac{1}{2}\sum _{n=1}^{N}\left({x}_{n+1}-{x}_{n}\right)\left[f\left({x}_{n}\right)+f\left({x}_{n+1}\right)\right]\text{\hspace{0.17em}},$

where $\left({x}_{n+1}-{x}_{n}\right)$ is the spacing between each consecutive pair of points.

### Tips

• trapz reduces the size of the dimension it operates on to 1, and returns only the final integration value. cumtrapz also returns the intermediate integration values, preserving the size of the dimension it operates on.