# Lecture 11 (Composite Rules for Numerical Integration)

### Last time

We took a look at the **Newton’s Cotes Formula**, which is defined for all \(n \geq 1\), and has an order of \(O(h^{2n+1})\).

There are some special cases we discussed in the previous lecture:

**Trapezoid Rule**for \(n = 1\) we look at the points \(x_0 \) and \(x_1 \).**Simpson’s Rule**for \(n = 2\) we look at the points \(x_0 \), \(x_1\) and \(x_2 \).

## Composite Rules

Today we will be looking at composite rules, which involve splitting our original problem into sub-intervals, and applying Simpson’s or Trapezoid rule to each of those subintervals.

In the above image, you can see a simple application of Trapezoid (blue), and Simpson’s (pink) rules to a graph of some function. You can see that in this case, we only consider the entire interval.

If we add some intervals, you can see that our estimation is actually made up of multiple trapezoids, in the case of Trapezoid Rule, or multiple quadratic approximations, in the case of Simpson’s Rule.

Here you can see that including even more intervals further increases the accuracy of our numerical integration.

Let’s take a look at how that would work with the Trapezoid Rule. Recall that the Trapezoid Rule is represented by:

The following term would then be:

and so on.

You can see that if we add all these terms together, we’ll get something like:

If we separate the formula and error term into two separate sums, we get the **Composite Trapezoid Rule**

If you expand that out, you’ll notice that for every value except the endpoints, it is added twice. i.e. if you look at the first two terms listed out above, \(x_1\) appears twice, and if we continued to list out terms, the same would be true until the last term. Therefore, we can also write the rule as:

Let’s take a quick gander at the error term above. We’ve somehow ended up with an \(n\) hanging out in there. From way back when, remember that h is the “length” of each of our subintervals, and therefore \(h = \frac{b-a}{n}\). From there some simple algebra reveals that \(n=\frac{b-a}{h}\). If we look at our error term:

You can replace n with \(\frac{b-a}{h}\), and get our final error term:

You can see that because of this, when we use composite rule with the trapezoid rule, we only converge at \(O(h^2)\), as opposed to the \(O(h^3)\) for the Trapezoid rule.

The only question remaining is why we slapped an n into the error term in the first place. This is a result of using the mean value theorem. Our claim is that because \(\xi(x)\) is bound on the interval [a,b], we can simply evaluate it once in that interval, for it’s maximum, and then multiply it by the number of error terms, in this case \(n\).

### Composite Simpson’s Rules

We will derive the composite Simpson’s Rule in a very similar way. Start with listing out a couple of terms:

I’ve left out the error terms here for brevity, but you can look at the previous lecture to see them, or just keep reading.

This sum is actually interesting, since we have to go interval by interval, and each interval contains 3 points, we actually have to “skip” some values in our sum. You can see above how a simple sum that incremented by one every time wouldn’t work.

The above is one way of solving that problem. We just take our limits as 2k, to skip odd numbers. We have to adjust our maximum to \(\frac{n}{2}-1\) to ensure that \(2k\) doesn’t go above n. If we expand that sum out, we get:

The error term was derived in the same way as the one for Composite Trapezoid Rule. You’ll notice that the original error term for Simpson’s Rule had \(\frac{h^4}{90}\), but since Simpson’s looks at 3 points, our intervals are now 2h wide, which is where the factor of two comes from.)

## Pseudocode

Here is an implementation of composite Simpson’s Rule in Python. Everything should be pretty obvious, except maybe `for i in range(1, n, 2)`

. In Python, this simply means count from 1 to n, with a step size of 2. It’s just ordered differently from matlab

```
def simpson(f, a, b, n):
"""Approximates the definite integral of f from a to b by the
composite Simpson's rule, using n subintervals (with n even)"""
if n % 2:
raise ValueError("n must be even (received n=%d)" % n)
h = (b - a) / n
s = f(a) + f(b)
for i in range(1, n, 2):
s += 4 * f(a + i * h)
for i in range(2, n-1, 2):
s += 2 * f(a + i * h)
return s * h / 3
```