File Exchange

image thumbnail

Simpson's Rule Integration

version (1.71 KB) by Juan Camilo Medina
Computes an integral "I" via Simpson's rule in the interval [a,b] with n+1 equally spaced points


Updated 29 Apr 2011

View Version History

View License

This function computes the integral "I" via Simpson's rule in the interval [a,b] with n+1 equally spaced points

Syntax: I = simpsons(f,a,b,n)

f= can either be an anonymous function (e.g. f=@(x) sin(x)) or a vector containing equally spaced values of the function to be integrated
a= Initial point of interval
b= Last point of interval
n= # of sub-intervals (panels), must be integer

Written by Juan Camilo Medina - The University of Notre Dame
09/2010 (copyright Dr. Simpson)

Example 1:

Suppose you want to integrate a function f(x) in the interval [-1,1].
You also want 3 integration points (2 panels) evenly distributed through the
domain (you can select more point for better accuracy).
f=@(x) ((x-1).*x./2).*((x-1).*x./2);

Example 2:

Suppose you want to integrate a function f(x) in the interval [-1,1].
You know some values of the function f(x) between the given interval,
those are fi= {1,0.518,0.230,0.078,0.014,0,0.006,0.014,0.014,0.006,0}
fi= [1 0.518 0.230 0.078 0.014 0 0.006 0.014 0.014 0.006 0];
note that there is no need to provide the number of intervals (panels) "n",
since they are implicitly specified by the number of elements in the vector fi

Cite As

Juan Camilo Medina (2021). Simpson's Rule Integration (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (19)

Andreas Hoppe


Zhr Khsrvi

Anthony Gunderman

In the function, when it is summing the even terms with 4*sum(f(2:2:end), this section, I think, needs to be 4*sum(f(2:2:end-1), otherwise you are summing the last term twice as you iterate through the function [i.e. 4*f(end) + f(end). This is almost imperceptible when this function is used with a single integral as the error only occurs once; however I am using this function twice to conduct a double integral with a for loop, resulting in cumulative error.

Regardless, thanks for providing it! Please, correct me if I am wrong to. Extremely helpful to see it done in Matlab; text books typically have the first variable as f(o), but due to Matlab's syntax, where the first array value is f(1), this provided a good example of how the code needed to be done.

Nguyen Huu Can

Dear Juan Camilo Medina,
Thank you for your nice code.
But there are not work when a = b.
I edited it, included some things new and I also cited your work.
All the best

Raúl Ibáñez Couoh


Patrick Senas

The composite rule is constructed by adding up instances of Simpson's rule applied to "pairs" of subintervals. So this means that Simpson's rule requires an even number of subintervals. So if n is odd, then you wouldn't get an even number of subintervals and you wouldn't be able to apply Simpson's rule to it.


Hey! Thank you very much for the code. Could you please precise is it has to be like this
I= h/3*(f(xi(1))+2*sum(f(xi(3:2:end-2)))+4*sum(f(xi(2:2:end-1)))+f(xi(end)));

where 4*sum(f(xi(2:2:end-1))) includes end-1


Concerning my previous comment, each value in the interval means' I have take delta to be 0.04.


I tried this code I=simpsons(fi,0,1,[]), each value in the interval has corresponding f values which is stored in the vector fi.
I ran the program and its showing undefined function 'simpsons' for input argument. What should I do?

James Vearling

I've just used this code to help me with coding my own Simpson's rule integration estimate. I noticed that the formula doesn't work for odd values of n, however when I looked closer in the code to where you're using Simpson's rule you've set h=(b-a)/n. If you were to set it instead equal to (b-a)/(2n) the formula would work for all values of n.

Jacob Davis

When I first started using it it worked great, but now whenever I try to use a function it gives me an error that says "Undefined function or variable 'x'." What am I doing wrong?

jinc chen

Maxime Boudreau

Could it be possible that the algorithm isn't complete if n is odd? In that case, I think the formula doesn't take the last subinterval into account.

To see it, you can try:




Juan Camilo Medina

Feel free to leave your comments here.

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: simpsonQuadrature, Simpson's 1/3 and 3/8 rules

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!