Trapezoidal numerical integration
Yis a vector, then
trapz(Y)is the approximate integral of
Yis a matrix, then
trapz(Y)integrates over each column and returns a row vector of integration values.
Yis 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.
Q = trapz(___, integrates along the
dim using any of the previous syntaxes. You must
Y, and optionally can specify
If you specify
X, then it can be a scalar or a vector with
length equal to
size(Y,dim). For example, if
Y is a matrix, then
integrates each row of
Integrate Vector of Data with Unit Spacing
Calculate the integral of a vector where the spacing between data points is 1.
Create a numeric vector of data.
Y = [1 4 9 16 25];
Y contains function values for in the domain [1, 5].
trapz to integrate the data 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, . The
trapz function overestimates the value of the integral because f(x) is concave up.
Integrate Vector of Data with Nonunit Spacing
Calculate the integral of a vector where the spacing between data points is uniform, but not equal to 1.
Create a domain vector.
X = 0:pi/100:pi;
Calculate the sine of
Y = sin(X);
Q = trapz(X,Y)
Q = 1.9998
When the spacing between points is constant, but not equal to 1, an alternative to creating a vector for
X is to specify the scalar spacing value. In that case,
trapz(pi/100,Y) is the same as
Integrate Matrix with Nonuniform Spacing
Integrate the rows of a matrix where the data has a nonuniform spacing.
Create a vector of x-coordinates and a matrix of observations that take place at the irregular intervals. The rows of
Y represent velocity data, taken at the times contained in
X, for 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];
trapz to integrate each row independently and find the total distance traveled in each trial. Since the data is not evaluated at constant intervals, specify
X to indicate the spacing between the data points. Specify
dim = 2 since the data is in the rows of
Q1 = trapz(X,Y,2)
Q1 = 3×1 82.8000 85.7250 82.1250
The result is a column vector of integration values, one for each row in
Multiple Numerical Integrations
Create a grid of domain values.
x = -3:.1:3; y = -5:.1:5; [X,Y] = meshgrid(x,y);
Calculate the function on 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. In cases where the functional expression is known, you can instead use
trapz to approximate the double integral
To perform double or triple integrations on an array of numeric data, nest function calls to
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.
Y — Numeric data
vector | matrix | multidimensional array
Numeric data, specified as a vector, matrix, or multidimensional
array. By default,
trapz integrates along the first
Y whose size does not equal 1.
Complex Number Support: Yes
X — Point spacing
(default) | uniform scalar spacing | vector of coordinates
Point spacing, specified as
1 (default), a uniform
scalar spacing, or a vector of coordinates.
Xis a scalar, then it specifies a uniform spacing between the data points and
trapz(X,Y)is equivalent to
Xis a vector, then it specifies x-coordinates for the data points and
length(X)must be the same as the size of the integration dimension in
dim — Dimension to operate along
positive integer scalar
Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension of size greater than 1.
Consider a two-dimensional input array,
trapz(Y,1)works on successive elements in the columns of
Yand returns a row vector.
trapz(Y,2)works on successive elements in the rows of
Yand returns a column vector.
dim is greater than
trapz returns an array of zeros of the same
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 example, here is a
trapezoidal integration of the sine function using eight evenly-spaced
For an integration with
N+1 evenly spaced
points, the approximation is
where the spacing between each point is equal to the scalar value . By default MATLAB® uses a spacing of 1.
If the spacing between the
N+1 points is not constant, then the formula
where , and is the spacing between each consecutive pair of points.
cumtrapzto perform numerical integrations on discrete data sets. Use
integral3instead if a functional expression for the data is available.
trapzreduces the size of the dimension it operates on to 1, and returns only the final integration value.
cumtrapzalso returns the intermediate integration values, preserving the size of the dimension it operates on.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
If you supply
dim, then it must be a constant.
See Variable-Sizing Restrictions for Code Generation of Toolbox Functions (MATLAB Coder).
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Introduced before R2006a