Open In App

Trapezoidal numerical integration in MATLAB

Last Updated : 04 Jul, 2021
Like Article

Trapz function in MATLAB is used to find the numerical integration using the trapezoidal rule. The basic idea in the Trapezoidal rule is to assume the region under the graph of the given function to be a trapezoid instead of the rectangle and calculate its area. 

The formula for numerical integration using trapezoidal rule is:

∫_a^bf(x)dx=h/2[ f(a)+2\{ ∑^{n-1}_{i=1}f(a+ih)\}  +f(b)]

where h = (b-a)/n

MATLAB allows us to perform numerical integration by simply using trapz function instead of going through the lengthy procedure of the above formula.

In MATLAB, trapz function takes the input arguments in 3 different ways.

  1. trapz(Y)
  2. trapz(X,Y)
  3. trapz(_____,dim)


In this method, trapz function considers unit spacing by default. Here Y is numeric data which can be a vector, a matrix, or multidimensional array.

  • If Y is a vector, then trapz function computes the approximate integral of Y.
  • If Y is a matrix, then trapz function integrates over each column of the matrix and returns a row vector of integration values as output.
  • If Y is a multidimensional array, then trapz function integrates over the first dimension whose size does not equal 1.

Let’s take an example for calculating the integral of a vector Y containing function values for f(x) = 2^x       in domain [1,5]



% MATLAB code for calculate Y is a vector
% of numeric data containing function values 
% for f(x) = 2^x in domain [1,5]
Y = [2 4 8 16 32];
% Integrate the data using 'trapz(Y)'
% with unit spacing (by default)
Q = trapz(Y);
% display the result
disp('The approximate Integration using trapz(Y) = ');




 In this method, X specifies the scalar spacing or the coordinates and trapz function integrates Y with respect to X.

  • If X is a vector of coordinates, then the length of X must be equal to the size of the first dimension of Y whose size is not equal to 1.
  • If X is scalar spacing, then the value of trapz(X,Y) is equivalent to X*trapz(Y).

Here below example demonstrate the trapz(X,Y) and calculate the integral value of vector Y having a point spacing value of pi/100.



% MATLAB code for calculate the
% integral value of vector Y having
% a point spacing value of pi/100. Where 
% X ranges from 0 to pi/2 with uniform 
% spacing of pi/100
X = 0:pi/100:pi/2;
% Y is a vector having point spacing 
% value of pi/100
Y = cos(X);
% Integrate the data using 'trapz(X,Y)'
Q = trapz(X,Y);
disp('The approximate Integration using trapz(X,Y) = ');


trapz(______, dim)

Here, dim is the dimension to operate along and in this method, X is an optional argument. 

  • If dim is specified as 1, then trapz(Y,1) integrates over each column and returns a row vector of integration values.
  • If dim is specified as 2, then trapz(Y,2) integrates over each row and returns a column vector of integration values.

The below example Integrates the rows of matrix Y with non-uniform point spacing with dim as 2 by using trapz(_____, dim) function.



% MATLAB code for Integrate the rows of
% matrix Y with non-uniform point spacing
% with dim as 2. Here X contains non-uniform intervals
X = [1 2.5 5.5 10];
% Y is a matrix  with non-uniform point spacing 
% with value of dim = 2 as data is in the rows of Y 
Y = [13.5   23.7   17.2   13.2;
     22.5   16.0  20.5   24.5;
     15.6   27.7  10.2   12.9];
% Integrate the data using 'trapz(X,Y,dim)' 
Q = trapz(X,Y,2);
disp('The approximate Integration using trapz(X,Y,2) = ');



Similar Reads

MATLAB - Trapezoidal numerical integration without using trapz
Trapezoidal rule is utilized to discover the approximation of a definite integral. The main idea in the Trapezoidal rule is to accept the region under the graph of the given function to be a trapezoid rather than a rectangle shape and calculate its region. The formula for numerical integration using trapezoidal rule is: [Tex]∫_a^bf(x)dx=h/2[ f(a)+2
2 min read
Definite Numerical Integration Using Quad in MATLAB
Pre-requisites: Definite Integration MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. With the help of this function, we can compute definite integration. We use quad function which takes the function and its limits as its arguments.
2 min read
How to Solve Histogram Equalization Numerical Problem in MATLAB?
Histogram Equalization is a mathematical technique to widen the dynamic range of the histogram. Sometimes the histogram is spanned over a short range, by equalization the span of the histogram is widened. In digital image processing, the contrast of an image is enhanced using this very technique. Use of Histogram Equalization:It is used to increase
3 min read
Integration in MATLAB
Integration is defined as the process of finding the anti derivative of a function. It is used to calculate area, volume, displacement, and many more. In this article, we will see how to perform integration on expressions in MATLAB. There are two types of Integration: Indefinite integral: Let f(x) be a function. Then the family of all antiderivativ
3 min read
How to Select Random Rows from a Matrix in MATLAB?
A matrix is an n x n array that stores integers, floating point numbers or alphanumeric data in MATLAB. Indexing a matrix is the same as indexing an array. Syntax:matrix_name(i,j)where, i is the row number, and J is the column number which is to be indexed. Example 1: C/C++ Code % MATLAB code for select % random matrix elements mat = magic(5); mat(
2 min read
MATLAB Annotation
Annotations in MATLAB is a way of adding explanation or notes to the plots. Annotations add more information to the plots. There are different syntax formations for adding annotations to a plot: annotation(lineType,x,y)annotation(lineType)annotation(shapeType,dim)annotation(shapeType)annotation(___,Name,Value) Let's discuss all the above functions
3 min read
Global Variables in MATLAB
Variables in programming are generally storage spaces to store a certain type of data. There are many types of variables, but two commonly used types are local and Global variables. Generally, each MATLAB function has its own local variables. But sometimes for the sake of programming, we need to change the value of the local variable in another fun
4 min read
How to Calculate Harmonic Mean in MATLAB?
Harmonic mean is a type of mean, which is a measure of central tendencies of data, in statistics that gives large weightage to smaller data and small weightage to larger data. The Harmonic Mean in mathematical terms is nothing but the reciprocal of the mean of reciprocal values of all the data elements. Harmonic Mean in MATLAB:MATLAB provides a bui
2 min read
Copy Objects in MATLAB
In MATLAB, there are two kinds of objects - handles and values. The value objects are ordinary MATLAB objects which behave normally to copy operations. It means that when a value object is copied, the new copies of that object are completely independent of the original one i.e., if the original's value is changed, the copied object will not be chan
2 min read
How to create a function in MATLAB ?
A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: function output_params = function_name(iput_params) %
2 min read
Article Tags :