File Exchange

image thumbnail

Simpson's Rule Integration

version 1.6 (1.71 KB) by

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

91 Downloads

Updated

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)

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

Comments and Ratings (5)

jinc chen

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:
simpsons(@sin,0,pi,100)
simpsons(@sin,0,pi,101)
simpsons(@sin,0,pi,102)
...

Ivan

Ivan (view profile)

shin

shin (view profile)

goddd

Feel free to leave your comments here.

Updates

1.6

.

1.5

Added an extension to handle vectors as well and anonymous function.

1.4

I added an example

1.3

.

1.2

.

1.1

n/a

MATLAB Release
MATLAB 7.10 (R2010a)
Acknowledgements

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

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video