Trapezoidal numerical integration in MATLAB

04 Jul, 2021
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:
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.
trapz(Y)trapz(X,Y)trapz(_____,dim)
trapz(Y)
trapz(X,Y)
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  in domain [1,5]
Example:
Matlab
% 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 resultdisp('The approximate Integration using trapz(Y) = ');disp(Q);
Output:
 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.
Example:
Matlab
% 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/100X = 0:pi/100:pi/2;  % Y is a vector having point spacing % value of pi/100Y = cos(X);  % Integrate the data using 'trapz(X,Y)'Q = trapz(X,Y);disp('The approximate Integration using trapz(X,Y) = ');disp(Q);
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.
Example:
Matlab
% MATLAB code for Integrate the rows of% matrix Y with non-uniform point spacing% with dim as 2. Here X contains non-uniform intervalsX = [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) = ');disp(Q);
Output:
Picked
MATLAB
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Convert Three Channels of Colored Image into Grayscale Image in MATLAB?
How to Solve Histogram Equalization Numerical Problem in MATLAB?
Adaptive Histogram Equalization in Image Processing Using MATLAB
MRI Image Segmentation in MATLAB
How to detect duplicate values and its indices within an array in MATLAB?
Double Integral in MATLAB
How to Normalize a Histogram in MATLAB?
Classes and Object in MATLAB
How to remove space in a string in MATLAB?
Forward and Inverse Fourier Transform of an Image in MATLAB