- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Trapezoidal numerical integration in MATLAB
In mathematics, trapezoidal numerical integration is a method of approximating the definite integral of a function over a certain interval.
In the trapezoidal numerical integration, a curve is divided into multiple trapezoids and then areas of all the trapezoids are calculated and added to estimate the total area under the curve. It is a basic method for approximating the definite integral of a function. Hence, it is not much accurate as the other advanced integration methods. However, for simple functions, this method can provide a reasonable approximation.
Trapezoidal Numerical Integration Using MATLAB
In MATLAB, we have a built-in function 'trapz' that allows for computing the trapezoidal numerical integration of a function. This function calculates the approximate integration of a definite set of data points.
However, this function can have different syntaxes based on different use cases. The commonly used syntaxes of the 'trapz' function are as follows −
I = trapz(A);
I = trapz(A, B);
I = trapz(A, B, dim)
Let's discuss these syntaxes in detail with the help of example MATLAB codes.
(1). Trapezoidal Numerical Integration Assuming Unit Spacing Between Data Points
Syntax
We can use the following syntax of the 'trapz' function to calculate the numerical integration of a vector 'A' where it is assumed that there is a unit spacing between data points of the vector −
I = trapz(A);
Here, A is a vector having unit spacing between data points.
The following MATLAB program demonstrates the code implementation to perform trapezoidal numerical integration of a vector assuming unit spacing between data point.
Matlab Example (1)
% MATLAB code to calculate numerical integration with unit spacing % Create a sample vector A = [0, 2, 8, 11, 15, 27]; % Calculate the trapezoidal numerical integral I = trapz(A); % Display the integration result disp('The approximate trapezoidal integration with unit spacing is:'); disp(I);
Output
The approximate trapezoidal integration with unit spacing is: 49.5000
Explanation
This MATLAB program use the 'trapz' function to calculate the approximate trapezoidal numerical integral of the vector 'A', where it is assumed that the vector 'A' has unit spacing between data points.
(2). Trapezoidal Numerical Integration with Specified Spacing Between Data Points
Syntax
The following syntax of the 'trapz' function is used to calculate the trapezoidal numerical integration of a vector with specified spacing between data points −
I = trapz(A, B);
This function will calculate the integral of the vector 'B' with respect to the vector 'A', where the vector 'A' specifies the spacing between data points of the vector 'B'.
Consider the following MATLAB program to understand implementation of this syntax.
Matlab Example (2)
% MATLAB program to calculate integral of vector with specified spacing between Data points % Create a sample vector to specify the spacing between data points A = [2, 4, 6, 7, 8, 9]; % Create a sample vector whose integral to be calculated B = [0, 2, 8, 11, 15, 27]; % Calculate the trapezoidal integral I = trapz(A, B); % Display the result of integration disp('The approximate trapezoidal integration with specified spacing is:'); disp(I);
Output
The approximate trapezoidal integration with specified spacing is: 55.5000
Explanation
This MATLAB program uses the 'trapz' function to compute the numerical integral of the vector 'B' with respect to the vector 'A'. Here, the values of the vector 'A' specifies the spacing between data points of the vector 'B'.
(3). Trapezoidal Numerical Integration of Multidimensional Array Along Specified Dimension
Syntax
The following syntax of the 'trapz' function can be used to perform trapezoidal numerical integration of a multidimensional array along a specified dimension −
I = trapz(A, dim);
Here, A is a multidimensional array.
In this function, if 'dim = 1', then the function will perform integration along columns of the array and return a row vector of integral values.
If 'dim = 2', then the function will perform integration along rows of the array and returns a column vector containing integral values.
The following MATLAB program demonstrates the numerical integration of a multidimensional array long the rows and columns.
Matlab Example (3)
% MATLAB program to calculate integral of multidimensional array along specified dimension % Create a multidimensional array A = [1 3 5; 4 2 9; 7 5 6]; % Calculate the integral along the columns I_C = trapz(A, 1); % Calculate the integral along the rows I_R = trapz(A, 2); % Display the results of integration disp('The approximate integration of A along columns is:'); disp(I_C); disp('The approximate integration of A along rows is:'); disp(I_R);
Output
The approximate integration of A along columns is: 8.0000 6.0000 14.5000 The approximate integration of A along rows is: 6.0000 8.5000 11.5000
Explanation
This MATLAB program uses the 'trapz' function to calculate the integral of an array 'A' along the columns (dimension 1) and rows (dimension 2).
Conclusion
This is all about calculating trapezoidal numerical integration in MATLAB. We can perform the trapezoidal numerical integration of a vector or an array using the 'trapz' function. We have explained different syntaxes of the 'trapz' function in the above sections of this tutorial.