version 1.6.0.0 (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

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)

Where,

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).

Thus:

f=@(x) ((x-1).*x./2).*((x-1).*x./2);

I=simpsons(f,-1,1,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}

Thus:

fi= [1 0.518 0.230 0.078 0.014 0 0.006 0.014 0.014 0.006 0];

I=simpsons(fi,-1,1,[])

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

Juan Camilo Medina (2021). Simpson's Rule Integration (https://www.mathworks.com/matlabcentral/fileexchange/28726-simpson-s-rule-integration), MATLAB Central File Exchange. Retrieved .

Created with
R2010a

Compatible with any release

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

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

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

Andreas HoppeHARI GOVIND H PANICKERRIJOHN K PIUSZhr KhsrviAnthony GundermanIn 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 CanDear 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 CouohExcelente!!!!

Apostolos AgrafiadisPatrick SenasThe 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.

MarineHey! 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

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

ALOK PRASADI 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?

Thanks

James VearlingI'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 DavisWhen 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 chenMaxime BoudreauCould 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:

simpsons(@sin,0,pi,100)

simpsons(@sin,0,pi,101)

simpsons(@sin,0,pi,102)

...

Ivanshingoddd

Juan Camilo MedinaFeel free to leave your comments here.