MATLAB Trapezoidal Rule

Ammar Ali Dec 08, 2021
MATLAB Trapezoidal Rule

This tutorial will discuss computing the trapezoidal numerical integration using the trapz() function in Matlab.

Compute the Trapezoidal Numerical Integration Using the trapz() Function in MATLAB

Trapezoidal rule is used to find the numerical integration of a function. We can use Matlab’s built-in function trapz() to compute the trapezoidal numerical integration of a function.

If the input is a vector, the trapz() function will return the approximate integral of the input.

If the input is a matrix, the trapz() function will integrate the input over each column and return the integration values in a row vector.

If the input is a multidimensional array, the trapz() function will integrate the input over the first dimension.

For example, let’s create a vector and find its integration using the trapz() function. See the code below.

vector = [1 4 10 10 25];
Integration = trapz(vector)

Output:

Integration =

    37

We can also integrate an input with respect to the coordinates or scalar spacing present in another variable. The length of the vector containing the coordinates should be equal to the size of the first dimension of the input vector or matrix.

Let’s find the integration of a vector using non-unit spacing. See the code below.

vector = [1 4 10 10 25];
c = sin(vector);
Integration = trapz(vector,c)

Output:

Integration =

   -8.8483

We can also specify the dimension along which the integration will take place. The dimension should be a positive integer scalar.

If a functional expression is available, we can use the integral(), integral2(), and integral3() function.

We can use the cumtrapz() function to compute a vector or matrix’s cumulative trapezoidal numerical integration.

We can use the cumtrapz() function in the same way we used the trapz() function.

We can also find multiple numerical integrations using the trapz() function multiple times.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - MATLAB Matrix