Trapezoidal rule is used in integration to compute the definite integral of the functions. It is used extensively in the process of numerical analysis. For the purpose of integration, trapezoidal rule considers the area under curve to be made up of small trapezoids and then calculates the total area by summing the area of all these small trapezoids. In MATLAB, we use ‘trapz function’ to get the integration of a function using trapezoidal rule.
Let us now understand the syntax of trapz function in Matlab:
Syntax:
I = trapz (X)
I = trapz (Y, X)
Description:
- I = trapz (X) is used to calculate the integral of X, using trapezoidal rule. For vectors, it will give approximate integral. For a matrix, it will provide integration over all columns and will return the integration values in a row vector
- I = trapz (Y, X) will integrate X w.r.t coordinates given by Y
Examples of Trapezoidal Rule Matlab
Let us now understand the code to calculate the integration in MATLAB using ‘trapezoidal rule’.
Example 1:
In this example, we will take an array representing the (x^2 + 2) and will integrate it using trapezoidal rule. We will follow the following 2 steps:
- Create the input array
- Pass the function to the trapz function
Syntax:
X = [3 6 11 18 27]
[Creating the input array for the function x^2 + 2, in the range 1 to 5]
I = trapz (X)
[Calling the trapz function and passing the input array as an argument] [Mathematically, the trapezoidal rule will give integration of (x^2 + 2) as 50]
Input:
X = [3 6 11 18 27] I = trapz (X)
Output:

As we can see in the output, we have obtained integration of our input function as 50, which is the same as expected by us.
Example 2:
In this example, we will take a function of sin and will integrate it using the trapezoidal rule. We will follow the following 3 steps:
- Define the limits for input sin function
- Create a sin function
- Pass the input function and required limits to the trapz function
Syntax:
Y = 0:100;
[Defining the limits for trapezoidal rule]
X = 10 - 10 * sin (pi / 200 * Y);
[Creating the sine wave]
I = trapz (Y, X)
[Calling the trapz function and passing the input function as an argument] [Mathematically, the trapezoidal rule will give integration of (10 – 10 * sin (pi / 200 * Y)) as 363.3933]
Input:
Y = 0:100;
X = 10 - 10 * sin (pi / 200 * Y);
I = trapz (Y, X)
Output:

As we can see in the output, we have obtained integration of our input function as 363.3933, which is the same as expected by us.
Example 3:
In this example, we will take a cos function and will integrate it using the trapezoidal rule. We will follow the following 3 steps:
- Define the limits for input cos function
- Create a cos function
- Pass the input function and required limits to the trapz function
Syntax:
Y = 0:100;
[Defining the limits for trapezoidal rule]
X = 20 - 50 * cos (2*pi / 100 * Y);
[Creating the cos wave]
I = trapz (Y, X)
[Calling the trapz function and passing the input function as an argument] [Mathematically, the trapezoidal rule will give integration of (20 – 50 * cos (2*pi / 100 * Y)) as 2.0000e+03]
Input:
Y = 0:100;
X = 20 - 50 * cos (2*pi / 100 * Y);
I = trapz (Y, X)
Output:

Example 4:
In this example, we will take a matrix and will integrate its rows using the trapezoidal rule. We will follow the following 3 steps:
- Create the vector
- Create a matrix containing observations i.e input data
- Pass the vector and matrix to the trapz function
Syntax:
Y = [2 4.5 7 9];
[Creating the vector]
X = [5 8 6 13;
[Creating the input matrix with observations]
4 7 15 15;
4 5 10.2 18];
I = trapz (Y, X, 2)
[Calling the trapz function and passing the input array and matrix as arguments. We have also passed a third argument as ‘2’, which implies that our target data is present in the rows]
Input:
Y = [2 4.5 7 9];
X = [5 8 6 13;
4 7 15 15;
4 5 10.2 18];
I = trapz (Y, X, 2)
Output:

As we can see in the output, we have obtained integration of our input as a column vector, with each element corresponding to one row of the input matrix ‘X’.
Example 5:
In this example, we will take a 3×5 matrix and will integrate its rows using the trapezoidal rule. We will follow the following 3 steps:
- Create the vector
- Create a 3×5 matrix containing observations i.e input data
- Pass the vector and matrix to the trapz function
Syntax:
Y = [1 5 8 10 14];
[Creating the vector]
X = [15 8 4 3 0;
[Creating the input matrix with observations]
12 17 5 11 3;
8 15 10 8 6];
I = trapz (Y, X, 2)
[Calling the trapz function and passing the input array and matrix as arguments. The third argument ‘2’ implies that our target data is present in the rows]
Input:
Y = [1 5 8 10 14];
X = [15 8 4 3 0;
12 17 5 11 3;
8 15 10 8 6];
I = trapz (Y, X, 2)
Output:

As we can see in the output, we have obtained integration of our input as a column vector, with each element corresponding to one row of the input matrix ‘X’.