Problem 43003. Simpsons's rule (but not Homer Simpson)
I wonder what Homer Simpson would have thought of Simpson's rule? Somehow I doubt his thoughts would have included the phrase NewtonCotes, or even numerical integration.
In this problem, I want you to use Simpson's rule to integrate a function, provided as a list of points in a vector. The points must be equally spaced in x, so all that need be provided is the spacing between points in x. As well, I'll be nice and always give you an odd number of points, since that is important for Simpson's rule to work smoothly.
As a test case, we should know that the integral of cos(x) over the interval [0,pi/2] is 1, at least analytically that is true. Your Simpson's rule code, for only 7 points spanning that interval will do pretty well:
deltax = pi/12; Fx = cos(linspace(0,pi/2,7)); simpsInt(Fx,deltax) ans = 1.00002631217059
Thus, Fx is a vector of supplied function values, and deltax is the step size taken on the xaxis.
Homer would be proud.
Solution Stats
Problem Comments

9 Comments
If this problem gains some interest, I'll start trying to post additional problems. My idea is to build a new problem set, for general numerical methods like this.
Great problem along with a very clear description. Looking forward to your addtional problems in numerical analysis.
I've asked Ned how to create a new problem group first. (I don't think if I have that privilege.) Then I plan to establish a few new problem groups for problems like this. Perhaps a numerical methods group, a computational geometry group, others? I hope there will also be some existing problems I can then include in addition to those I will write.
Great idea to create a new problem group! Numerical analysis is definitely good problem group to start with. There are already some numerical integration problems which I have solved on Cody (such as a general integration problem: https://www.mathworks.com/matlabcentral/cody/problems/1031compositetrapezoidalrulefornumericintegration, and a problem dealing with trapezoidal rule: https://www.mathworks.com/matlabcentral/cody/problems/1031compositetrapezoidalrulefornumericintegration). I guess these problems can be included in a numerical analysis group.
Sorry for the wrong link. A general numerical integration problem: https://www.mathworks.com/matlabcentral/cody/problems/1197numericalintegration, and a trapezoidal rule problem: https://www.mathworks.com/matlabcentral/cody/problems/1031compositetrapezoidalrulefornumericintegration. I realize that Cody does not allow users to modify/delete their own comments, which is very inconvenient.
Thanks for the problem links. I'll forward that idea about editing comments to the site developers.
Nice problem, but #7 in the Test Suite needs an assert in it.
There's something I don't understand. The first function I wrote, according to Simpson's rule, passed all tests except #6; here the absolute error was still very small (~1.53e15) but greater than the tolerance, so my first solution failed the test set. Thus I had to add a small correction factor of 1e15. Now the function passes all tests, but I can't understand the error in my implementation.
@Giovanni I had the same problem; the test case is overzealous.
Solution Comments
Show commentsProblem Recent Solvers37
Suggested Problems

Find the sum of all the numbers of the input vector
47762 Solvers

2902 Solvers

Matrix indexing with two vectors of indices
716 Solvers

Relative ratio of "1" in binary number
1380 Solvers

448 Solvers
More from this Author4
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!