MATLAB trapz trapezoidal numerical integration function

trapz

trapezoidal numerical integration

full page collapse

Grammar

Q = trapz(Y)

Q = trapz(X,Y)

Q = trapz(___,dim)

Description

example

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

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

  • If Y is a matrix, then trapz(Y) integrates each column and returns a row vector of integrated 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, while the sizes of the other dimensions remain the same.

example

Q = trapz(X,Y) integrates Y with respect to the coordinates or scalar spacing specified by X.

  • If X is a coordinate vector, then length(X) must be equal to the size of the first dimension where the size of Y is not equal to 1.

  • If X is a scalar spacing, then trapz(X,Y) is equivalent to X*trapz(Y).

example

Q = trapz(___,dim) integrates along the dimension dim using any of the above syntaxes. Y must be specified, X is optional. If X is specified, it can be a scalar or vector of length equal to size(Y,dim). For example, if Y is a matrix, then trapz(X,Y,2) integrates each row of Y.

Example

collapse all

Integrate data vectors with unit spacing

Open Live Script

Computes the integral of a vector with spacing of 1 between data points.

Create a numeric vector of data.

Y = [1 4 9 16 25];

Y contains the value of the function f(x)=x2 in the domain [1, 5].

Use trapz to integrate the data at unit intervals.

Q = trapz(Y)
Q = 42

This approximate integral yields the value 42. In this case, the exact answer is somewhat small, 4113. The trapz function overestimates the integral because f(x) is concave upward.

Integrate data vectors with non-unit spacing

Open Live Script

Computes the integral of a vector whose data points are evenly spaced but not equal to 1.

Create domain vectors.

X = 0:pi/100:pi;

Computes the sine of X.

Y = sin(X);

Integrate Y using trapz.

Q = trapz(X,Y)
Q = 1.9998

An alternative to creating a vector for X when the spacing between points is constant but not equal to 1 is to specify a scalar spacing value. In this case, trapz(pi/100,Y) is the same as pi/100*trapz(Y).

Integrate a matrix with non-uniform spacing

Open Live Script

Integrate the rows of a matrix with non-uniform data spacing.

Create a vector of x coordinates and a matrix of observations measured at irregular intervals. The rows in Y represent velocity data measured at various times in X, from three different trials.

X = [1 2.5 7 10];
Y = [5.2 7.7 9.6 13.2;
     4.8 7.0 10.5 14.5;
     4.9 6.5 10.2 13.8];

Integrate each row separately using trapz and find the total distance traveled in each trial. Since the data is not calculated at regular intervals, specify X to represent the spacing between data points. Since the data is in the row of Y, specify dim = 2.

Q1 = trapz(X,Y,2)
Q1 = <em>3×1</em>

   82.8000
   85.7250
   82.1250

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

Multiple numerical points

Open Live Script

Create a grid of field values.

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

Computes the function f(x,y)=x2 + y2 on a grid.

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

trapz integrates numeric data, not functional expressions, so the expressions generally do not need to be known to use trapz on data matrices. Where the function expression is known, you can use integral, integral2, or integral3 instead.

Approximate double integrals using trapz

I=∫5?5∫3?3(x2 + y2)dxdy

To perform double or triple integral operations on arrays of numeric data, nest function calls to trapz .

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

trapz first integrates x to produce a column vector. Integral over y then reduces the column vector to a single scalar. trapz slightly overestimates the exact answer by 680 because f(x,y) is concave upward.

Input parameters

collapse all

Y – numeric data
vector | matrix | multidimensional array

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

Data type: single | double
Plural Support: Yes

X – point spacing
1 (default) | uniform scalar spacing | coordinate vector

Point spacing, specified as 1 (the default), a uniform scalar spacing, or a coordinate vector.

  • If X is a scalar, it specifies that the data points are evenly spaced, and trapz(X,Y) is equal to X*trapz(Y).

  • If X is a vector, it specifies the x-coordinates of the data points, and length(X) must be the same size as the integration dimension of Y.

Data type: single | double

dim – the dimension along which to operate
Positive integer scalar

Dimension along which to operate, specified as a positive integer scalar. If no dimension is specified, it defaults to the first array dimension greater than 1.

Take a two-dimensional input array Y as an example:

  • trapz(Y,1) operates on consecutive elements in the columns of Y and returns a row vector.

  • trapz(Y,2) operates on consecutive elements in the rows of Y and returns a column vector.

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

Details

collapse all

Trapezoid

trapz performs numerical integration via the trapezoidal method. This method approximates the integral over the interval by dividing a region into trapezoids containing more easily computed regions. For example, here is the trapezoidal integral of the sine function using eight evenly spaced trapezoids:

For integrals with N + 1 uniformly distributed points, the approximation is

b∫af(x)dx≈b?a2NN∑n=1(f(xn) + f(xn + 1))=b?a2N[f(x1) + 2f(x2) + … + 2f(xN) + f(xN + 1)],

where the spacing between points is equal to the scalar value b?aN. By default, MATLAB? uses a spacing of 1.

If the spacing between N + 1 points is not constant, the formula can be extrapolated to

b∫af(x)dx≈?12N∑n=1(xn + 1?xn)[f(xn) + f(xn + 1)]?,

where a=x1

Hint

  • Use trapz and cumtrapz to perform numerical integration on discrete data sets. If the data can be represented by a function expression, use integral, integral2, or integral3 instead.

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

Extended Capabilities

C/C++ code generation
Generate C code and C++ code with MATLAB? Coder?.

Thread-based environment
Run code in the background with MATLAB? backgroundPool or speed it up with Parallel Computing Toolbox? ThreadPool.

GPU arrays
Accelerate code execution by running on a graphics processing unit (GPU) with Parallel Computing Toolbox?.

Distributed arrays
Use Parallel Computing Toolbox? to partition large arrays in the combined memory of a cluster.

The following is the time and speed array to find the distance MATLAB function, the speed unit is km/h, the distance unit is m, and the time interval is 0.2s

function Distance = DistanceCacl(V)
V=V./3.6;
t=0.2:0.2:length(V)*0.2;
Distance=trapz(t,V);
end