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 Newton-Cotes, 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 x-axis.
Homer would be proud.
Solution Stats
Problem Comments
-
9 Comments
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.53e-15) but greater than the tolerance, so my first solution failed the test set. Thus I had to add a small correction factor of 1e-15. 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 Solvers39
Suggested Problems
-
8805 Solvers
-
745 Solvers
-
Area of an equilateral triangle
6314 Solvers
-
Let's get back to school, and create multiplication tables
214 Solvers
-
5460 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!