- Trending Categories
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 without using trapz in MATLAB
Introduction to Trapezoidal Numerical Integration
Trapezoidal numerical integration is a method used for finding the approximate value of a definite integral. It is also called as the trapezoidal rule. In the case of trapezoidal numerical integration method, we use trapezoids to calculate the area under a curve.
In the trapezoidal rule, the total interval over which we want to integrate is divided into smaller subintervals. The area under the curve is then approximated in each subintervals by using trapezoids. After that the total area under the curve is estimated through the sum of the areas of sub-trapezoids. This will be the approximate value of the definite integral.
Trapezoidal Rule Formula
The following expression is the formula for the trapezoidal numerical integration that gives an approximation of the definite integral.
$$\mathrm{\int_{a}^{b}f(x)dx\approx \frac{h}{2}[f(a)+2\lbrace{^{n-1}_{i=1}} f(x_{i})+f(b)\rbrace]}$$
Where,
$$\mathrm{x_{i}=a+ih}$$
$$\mathrm{h=\frac{(b-a)}{n}}$$
Here, "h" is the step size or the width of each subinterval, "n" is the number of subintervals, "a" is the lower limit of the integration, and "b" is the upper limit of the integration.
Trapezoidal Numerical Integration without using 'trapz' function in MATLAB
In MATLAB, we can use a built-in function “trapz” to approximate the definite integrals numerically. However, in this article, we will learn the implementation of the trapezoidal numerical integration without using “trapz” function.
For this, let us consider some example programs in MATLAB.
Problem (1): Estimate the area under the curve$\mathrm{^{2}_{0}(\frac{1}{1+x^{3}})dx}$ using 15 subintervals.
Example
% MATLAB program demonstrate the trapezoidal numerical integration without using trapz function. % MATLAB program to find area under the curve ∫_0^2 1/(1+x^3) dx using 15 subintervals. % Create a dynamic variable x by using syms function syms x % Specify the lower and upper limits of the integration a = 0; b = 2; % Specify the number of subintervals n = 15; % Define the function to integrate fun1 = 1 / (1+x^3); % Create a function of string containing in fun1 fun = inline(fun1); % h is the step size h = (b - a) / n; % Create a variable S1 that stores the sum of first and last subintervals S1 = fun(a) + fun(b); % Create a variable S that stores the sum of all the subintervals from 1 to (n-1) S = 0; for i = 1:1:n-1 xi = a + (i * h); S = S + fun(xi); end % Apply the formula to perform the numerical integration using Trapezoidal Rule I = (h / 2) * (S1 + 2 * S); % Display the result of the integration disp('Area under the curve 1/(1+x^3) = '); disp(I);
Output
Area under the curve 1/(1+x^3) = 1.0898
Explanation
In this MATLAB program, the function “fun1” specifies the function that we want to integrate. The variables “a” and “b” define the lower limit and upper limit of the integration respectively. The function “inline” generates creates a function “fun” of string containing in the function “fun1”. Then, an expression “h” is called to calculate the step size. After that we calculate the sum of first and last subintervals and stores the summation in “S1”. Then, we specify a variable “S” to store the sum of all subintervals from 1 to (n-1). After that we perform the numerical integration by using the trapezoidal rule formula. Finally, we call “disp” function to display the result.
Problem (2): Estimate the area under the curve $\mathrm{^{\pi}_{0}(sin(x))dx}$ using 10 subintervals.
Example
% MATLAB program demonstrate the trapezoidal numerical integration without using trapz function. % MATLAB program to find area under the curve ∫_0^π sin(x) dx using 10 subintervals. % Create a dynamic variable x by using syms function syms x % Specify the lower and upper limits of the integration a = 0; b = pi; % Specify the number of subintervals n = 10; % Define the function to integrate fun1 = sin(x); % Create a function of string containing in fun1 fun = inline(fun1); % h is the step size h = (b - a) / n; % Create a variable S1 that stores the sum of first and last subintervals S1 = fun(a) + fun(b); % Create a variable S that stores the sum of all the subintervals from 1 to (n-1) S = 0; for i = 1:1:n-1 xi = a + (i * h); S = S + fun(xi); end % Apply the formula to perform the numerical integration using Trapezoidal Rule I = (h / 2) * (S1 + 2 * S); % Display the result of the integration disp('Area under the curve sin (x) = '); disp(I);
Output
Area under the curve sin (x) = 1.9835
Conclusion
This is all about performing trapezoidal numerical integration without using “trapz” function in MATLAB. The above example programs in MATLAB illustrate a straightforward implementation of trapezoidal numerical integration without using the “trapz” function.