Simpson 1/3 Rule with MATLAB

Simpson’s 1/3 rule is a numerical integration technique used to approximate the definite integral of a function. It is based on approximating the area under a curve by fitting parabolic arcs to small sections of the curve.

The formula for Simpson’s 1/3 rule is:

I ≈ (b-a)/6 [f(a) + 4f((a+b)/2) + f(b)]

where:

I is the approximate value of the definite integral
a and b are the lower and upper limits of integration, respectively
f(x) is the function being integrated
This formula uses the values of the function at the endpoints and the midpoint of the interval of integration to construct a parabolic approximation of the curve. The area under the parabolic curve is then used to approximate the area under the original curve.

The Simpson’s 1/3 rule provides a higher degree of accuracy than the trapezoidal rule, but it can only be used on equally spaced data points. If the number of intervals is odd, then the trapezoidal rule is used for the last interval.

MATLAB Coding of Simpson 1/3 Rule

Here’s an example implementation of Simpson’s 1/3 rule using MATLAB:

% Define the function to be integrated
f = @(x) x^2;

% Define the limits of integration
a = 0;
b = 2;

% Define the number of intervals (must be even)
n = 4;

% Calculate the interval width and the points at which to evaluate the function
h = (b-a)/n;
x = a:h:b;

% Evaluate the function at the endpoints and midpoint of each interval
y = f(x);
y(2:2:end-1) = 4*y(2:2:end-1);
y(3:2:end-2) = 2*y(3:2:end-2);

% Apply Simpson's 1/3 rule
I = (b-a)/(6*n) * sum(y);

% Display the result
fprintf('The approximate value of the integral is %.4f\n', I);

In this example, we define the function ‘f(x)‘ as ‘x^2‘, and the limits of integration as ‘a=0‘ and ‘b=2‘. We also set the number of intervals ‘n=4‘, which must be even for Simpson’s 1/3 rule.

We then calculate the interval width h and the points at which to evaluate the function ‘x‘. We evaluate the function at the endpoints and midpoint of each interval using the vectorized indexing operation ‘y(2:2:end-1)‘ for the odd-indexed elements and ‘y(3:2:end-2)‘ for the even-indexed elements, as specified by Simpson’s 1/3 rule. We then apply the formula for Simpson’s 1/3 rule to obtain the approximate value of the integral I.

Finally, we display the result using the ‘fprintf‘ function with a precision of 4 decimal places.