Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Simpson’s Rule in MATLAB

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Simpson’s 1/3 rule is a numerical method used for the evaluation of definite integrals. MATLAB does not provide an in-built function to find numerical integration using Simpson’s rule. However, we can find that using the below formula.

The formula for numerical integration using Simpson’s rule is:

where, h = (b-a)/n

In Simpson’s 1/3 rule, we evaluate the definite integral using integration by successive segments of the curve. It helps us to make the approximations more precise as compared to trapezoidal rule where straight lines segments were used instead of parabolic arcs.

Note: For Simpson’s 1/3 Rule, n must be even.

Example: Evaluate ∫logx dx     within limit 4 to 5.2


% MATLAB code for syms function that creates a variable
% dynamically and automatically assigns
% to a MATLAB variable with the same name
syms x
% Lower Limit
a = 4;
% Upper Limit
b = 5.2;
% Number of Segments
n = 6;
% Declare the function
f1 = log(x);
% inline creates a function of string containing in f1
f = inline(f1);
% h is the segment size
h = (b - a)/n;
% X stores the summation of first and last segment
X = f(a)+f(b);
% variables Odd and Even to store 
% summation of odd and even
% terms respectively
Odd = 0;
Even = 0;
for i = 1:2:n-1
for i = 2:2:n-2
% Formula to calculate numerical integration 
% using Simpsons 1/3 Rule
I = (h/3)*(X+4*Odd+2*Even);
disp('The approximation of above integral is: ');


My Personal Notes arrow_drop_up
Last Updated : 17 Dec, 2021
Like Article
Save Article
Similar Reads