Saturday, March 25, 2023

Numerical integration using Trapezoidal Rule: using MATLAB solver

 Trapezoidal rule of integration

 The trapezoidal rule is a numerical method used to approximate the definite integral of a function. It is based on the idea of approximating the area under a curve by dividing it into trapezoids of equal width.

To apply the trapezoidal rule, we first divide the interval of integration [a, b] into n subintervals of equal width, h = (b - a) / n. The width of each subinterval is the same, so the distance between adjacent points where we evaluate the function is also the same. The value of n determines the number of subintervals and, therefore, the accuracy of the approximation.

Next, we approximate the area under the curve using trapezoids. The area of each trapezoid is given by the formula:

Area of trapezoid = (base1 + base2) * height / 2

where base1 and base2 are the lengths of the parallel sides of the trapezoid, and height is the perpendicular distance between the parallel sides.

To apply the trapezoidal rule, we first evaluate the function at the endpoints of each subinterval and connect the points to form trapezoids. The area of each trapezoid represents an approximation of the area under the curve over that subinterval. The sum of the areas of all the trapezoids gives an approximation of the definite integral of the function over the interval [a, b].

The formula for the trapezoidal rule is given by:

∫[a,b] f(x) dx ≈ h/2 * [f(a) + 2f(a+h) + 2f(a+2h) + ... + 2f(b-h) + f(b)]

where h is the width of each subinterval (h = (b - a) / n), and f(x) is the function being integrated.

In this formula, the term f(a) represents the value of the function at the left endpoint of the interval [a, b], and f(b) represents the value of the function at the right endpoint of the interval. The terms 2f(a+h), 2f(a+2h), ..., 2f(b-h) represent the values of the function at the endpoints of the intermediate subintervals, multiplied by a factor of 2 because they contribute to two adjacent trapezoids.

The error of the trapezoidal rule is of order O(h^2), which means that the error decreases quadratically as the width of the subintervals decreases. Therefore, the trapezoidal rule can be quite accurate for functions that are well-behaved and smooth over the interval of integration. However, for some functions, the accuracy may be insufficient, and other numerical integration methods may be required. Now let us summarize in a simple representative way:





Now let us solve this using Matlab. Here is the source code and the result
=================================================

% Trapezoidal Rule
% Define the function to be integrated
f = @(x) 0.2+ 25*x-200*x.^2 + 675*x.^3-900*x.^4+400*x.^5;
% Define the integration limits
a = 0;
b = 0.8;
% Define the number of subintervals
n = 10;
% Compute the width of each subinterval
h = (b-a)/n;
% Compute the approximated integral using the trapezoidal rule
integral_approx = (h/2)*(f(a) + 2*sum(f(a+h:h:b-h)) + f(b));
% Display the result || num2str:converts integers into character
disp(['Approximated integral using the trapezoidal rule: ', num2str(integral_approx)]);
Result of the above trapezoidal integration
===================================

>> Trapizoidal
Approximated integral using the trapezoidal rule: 1.615


No comments:

Post a Comment