The composite simpson's rule subdivides the interval into n equal subintervals, with n even.Then $$\int_a^b f(x) dx \approx \frac{h}{3}[f(x_0) + 4f(x_1) + 2f(x_2) + 4f(x_3) + ... + 2f(x_{n-2}) + 4f(x_{n-1}) + f(x_n)] $$(where $x_0 = a$ and $x_n=b$, $h=(b-a)/n$)sourceSo what to do if given n equal intervals, with n odd?One solution I implemented is to calculate using Simpson's rule for all but the last interval and use the trapezoid rule for the last interval. This seems to work well enough.But I started thinking, how would I calculate the last interval using Simpson's rule as well?Consider$$\begin{aligned}I_1 &= \frac{h}{3}[f(x_{n-3}) + 4f(x_{n-2}) + f(x_{n-1})] \\I_2 &= \frac{h}{3}[f(x_{n-2}) + 4f(x_{n-1}) + f(x_n)] \\\end{aligned}$$$x_n$ is the end of the integral interval. $I_1$ would be the sum corresponding to the last subinterval such that the total number of subintervals considered is even. $I_2$ is Simpson's rule applied to the final, odd subinterval, but half of the subinterval overlaps with $I_1$. Is there a way to identify what's common to $I_1$ and $I_2$ and subtract it out? Just staring at it, I can't easily see what is the common part. Is it as simple as $(1/2)(h/3)[5f(x_{n-2}) + 5f(x_{n-1})]$?
Composite simpson's rule with odd intervals
integration
A simple solution is to apply Simpson's (standard) rule to the first $n-3$ grid points, where $n-3$ is even for $n$ odd, and cover the remaining three gridpoints via the Simpson 3/8 formula:$$I_{3/8} = \frac{3h}{8}[f(x_{n-3}) + 3f(x_{n-2}) + 3f(x_{n-1}) + f(x_n)].$$Both have remainder terms of order $\mathcal O(h^5)$, so it keeps the order of the Simpson integration.