Saturday, March 25, 2023

Simpson's 3/8 rule of numerical integration using MATLAB

 Simpson's 3/8 rule is a numerical method used to approximate the definite integral of a function. It is a modification of Simpson's 1/3 rule that uses three subintervals instead of two, and it provides a more accurate approximation of the integral for some functions.

To apply Simpson's 3/8 rule, we first divide the interval of integration [a, b] into a multiple of three subintervals of equal width, h = (b - a) / n, where n is a multiple of three. The width of each subinterval is the same, so the distance between adjacent points where we evaluate the function is also the same.

Next, we approximate the area under the curve using cubic segments. We fit a cubic segment to the curve over every three adjacent subintervals. The cubic segment passes through the endpoints of the three subintervals and the two midpoints of the intervals. The area under the cubic segment represents an approximation of the area under the curve over the three subintervals.

The formula for Simpson's 3/8 rule is given by:

∫[a,b] f(x) dx ≈ 3h/8 * [f(a) + 3f(a+h) + 3f(a+2h) + f(a+3h) + 3f(a+4h) + 3f(a+5h) + ... + 3f(b-2h) + f(b- h) + 3f(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 3f(a+h), 3f(a+2h), 3f(a+4h), 3f(a+5h), ..., 3f(b-2h) represent the values of the function at the endpoints of the subintervals that are not the left or right endpoints, multiplied by a factor of 3 because they contribute to cubic segments. The terms f(a+3h), f(a+6h), f(a+9h), ..., f(b-h) represent the values of the function at the midpoints of the subintervals, multiplied by a factor of 1 because they do not contribute to cubic segments.

The error of Simpson's 3/8 rule is of order O(h^5), which means that the error decreases as the width of the subintervals decreases faster than Simpson's 1/3 rule. However, Simpson's 3/8 rule requires three subintervals per iteration, which means that it may be less efficient than Simpson's 1/3 rule for some applications. Additionally, some functions may require a large number of subintervals to achieve a desired level of accuracy, which can increase the computational cost of the method. Now let us summarize as follows:





Now let us solve this using Matlab. Here we will define faction after that computation is undertaken by calling the function.

% Let us construct a function
function I = simpson38(f, a, b, n)
% SIMPSON38 Simpson's 3/8 rule for numerical integration.
% I = SIMPSON38(f, a, b, n) approximates the definite integral of f
% from a to b using Simpson's 3/8 rule with n segments.
% The function f must accept a vector argument.
h = (b-a)/n; % h is the length of the segment
x = linspace(a, b, n+1); % define the variation
y = f(x);
% I is the Simpson's 3/8 Rule
I = 3*h/8*(y(1) + 3*sum(y(2:3:end-2)) + 3*sum(y(3:3:end-1)) + 2*sum(y(4:3:end-3)) + y(end));
end
% Finally save with respect to the function name
% Now let us call this function using another script


========================================================
% Define the function to be integrated
f = @(x) 0.2+ 25*x-200*x.^2 + 675*x.^3-900*x.^4+400*x.^5;
a = 0; % lower limit of integration
b = 0.8; % upper limit of integration
n = 10*3; % number of segments
I = simpson38(f, a, b, n); % approximate integral value
disp(I)
% Finally solve it. Actually, I did it before


Result: After computation, we obtained the following result. It should be noted
that the exact value of the integration using analytical is 1.640533.
=====================================================

>>simpson38_data
1.6405

No comments:

Post a Comment