Manually calculating the Numerical Integration of a function or data set using the trapezoidal method is a lengthy and time-consuming task that requires many calculations. But in the era of computing, it has become an easy and efficient way using MATLAB’s trapz() function.
This tutorial is going to present how to perform Numerical Integration in MATLAB using the trapz() function through different syntaxes and examples.
What is Numerical Integration
Numerical Integration is a common mathematical concept that is used to approximate integral computation through numerical techniques. It is useful in cases when it isn’t possible to compute the exact solution to the integral using analytic methods.
How to Perform Numerical Integration in MATLAB Using the trapz
As we discussed earlier, we can calculate the Numerical Integration in MATLAB using the trapz() function which offers an easy and efficient way to calculate the numerical integration of the set of data points using the trapezoidal rule. This function accepts a set of data points as a mandatory input and provides its computed Numerical Integration using the trapezoidal method.
Syntax
We can implement the trapz() function in MATLAB in the following ways:
Q = trapz(X,Y)
Q = trapz(___,dim)
Here,
The function Q = trapz(Y) is responsible for computing the Numerical Integration of the given data set named Y using the trapezoidal integration method having the unit spacing. This function determines the dimension of integration using the size of Y.
- If Y represents a vector, this function computes the approximate Numerical Integration of Y.
- If Y represents a matrix, this function computes the approximate Numerical Integration of each column of Y and provides a row vector containing the integration value of each column.
- If Y represents a multidirectional array, this function computes the approximate Numerical Integration of Y along the first dimension having a size not equal to 1. After that, the dimension size equals 1 and other dimensions remain unchanged.
The function Q = trapz(X,Y) is responsible for calculating the approximate Numerical Integration of Y using the numerical trapezoidal rule along the vector X that represents the coordinates or scalar spacing.
- If X represents a vector containing coordinates, the length(X) has to be equal to the first dimension of Y having a size not equal to 1.
- If X represents scalar spacing, then trapz(X,Y)=X*taz(Y).
The function Q = trapz(___,dim) is responsible for computing the approximate Numerical Integration of Y using the numerical trapezoidal method along the given dimension dim using any previous syntax.
Example 1: How to Numerically Integrate a Vector, Matrix, and Multidirectional Array?
This MATLAB code computes the Numerical Integration of the given data sets vector, matrix, and multidirectional array using the trapz() function and stores the result in the variables int_Y, int_A, and int_arr, respectively.
A = magic(8);
Arr = randn(2,7,2);
int_Y = trapz(Y)
int_A = trapz(A)
int_arr = trapz(Arr)
Example 2: How to Numerically Integrate the Given Vector With Respect to Specified Scalar Spacing?
In this example, we use the trapz() function to calculate the approximated Numerical Integration of the given vector Y with respect to constant spacing X.
X = linspace(0,1,10);
Q = trapz(X,Y)
Example 3: How to Numerically Integrate an Array Along the Specified Dimension?
In this MATLAB code, we use the trapz() function to compute the approximated Numerical Integration of the given multidirectional array Arr along dimension dim = 2.
Q = trapz(Arr,2)
Conclusion
Manually determining the Numerical Integration of the set of data points using the trapezoidal rule is a time-consuming and lengthy method that requires many calculations. However, this task can be easily and efficiently executed using the trapz() function in MATLAB. This guide has presented the functionality of the trapz() function with the help of examples to help us understand its use in MATLAB.