# Calculus: Integrals II

## Trapezoidal Rule

Rather than use flat rectangles to estimate the area under a curve, wouldn't it be better to try to approximate the curve itself? Couldn't we try to fit a line between the top corners of each region and form a trapezoid instead? Well have we got news for you - such a method exists. The Trapezoid Rule estimates the area under an integral using trapezoids. Why this approach is called a rule and the midpoint method is a method is unclear.

As you might expect, the formula for computing the area using trapezoids rather than rectangles is a bit more complicated. The area of a trapezoid is given by $A= \dfrac{1}{2}w(a+b)$, where $w$ is the width of the trapezoid, and $a$ and $b$ are the heights of the two sides. In our case, the width is $\delta x$, and the heights are the values at the function on the endpoints of each region, namely $f(a+\Delta x\cdot i)$ and $f(a+ \Delta x (i+1)$. Each side of a trapezoid gets counted twice - once when it serves as the left side, and once when it serves as the right side. Only the very first and very last get counted once each. Putting it all together gives us the following formula:

$$\int\limits_{a}^{b} f(x) \, dx \approx \dfrac{\Delta x}{2}\left( f(a) + 2f\left(a + \Delta x\right) + f\left(a + 2\Delta x\right) + \cdots + f(b) \right)$$

In case the above explanation doesn't make sense, writing out the areas of the first few trapezoids can help:

$\displaystyle\int\limits_{a}^{b} f(x) \, dx \approx \dfrac{\Delta x}{2}\left[f(a) + f(a+\Delta x)\right] + \dfrac{\Delta x}{2}\left[f(a + \Delta x) + f(a+2\Delta x)\right] + \dfrac{\Delta x}{2}\left[f(a + 2\Delta x) + f(a+3\Delta x)\right] + \ldots$

As you can see, the right side of the one trapezoid becomes the left side of the next trapezoid. As always, a picture is worth a thousand words:

With only 3 samples, the above approximation looks pretty close. Using 6 samples, it's hard to make out the difference between the tops of the trapezoids and the curve itself:

## Problems

1. Estimate $\displaystyle\int\limits_{0}^{2} 4x+5 \, dx$ where $N=4$.

Based on what you know about geometry, what is the exact solution?

Step 1: Calculate $\Delta x$:

$\Delta x = \dfrac{b-a}{N} \\ \Delta x = \dfrac{2-0}{4} \\ \Delta x = \dfrac{1}{2} \\$

Step 2: Calculate sum:

$\displaystyle\int\limits_{0}^{2} 4x+5 \, dx \approx \dfrac{\Delta x}{2}\left(f(0) + 2f\left(\dfrac{1}{2}\right) + 2f(1) + 2f\left(\dfrac{3}{2}\right) + f(2) \right) \\ \displaystyle\int\limits_{0}^{2} 4x+5 \, dx \approx \dfrac{\frac{1}{2}}{2}\left((4(0)+5) + 2\left(4\left(\dfrac{1}{2}\right)+5\right) + 2(4(1)+5) + 2\left(4\dfrac{3}{2} + 5 \right) + (4(2)+5) \right) \\ \displaystyle\int\limits_{0}^{2} 4x+5 \, dx \approx \dfrac{1}{4}\left(5 + 14 + 18 + 22 + 13 \right) \\ \displaystyle\int\limits_{0}^{2} 4x+5 \, dx \approx 18 \\$

Step 3: Exact solution with geometry:

The integrand can be broken down into two regions whose area can be computed geometrically, namely the rectangle below $f(x)=5$ and the triangle that sits on top of it whose hypotenuse is formed by the line $g(x) = 4x+5$. The area of a rectangle is width $\times$ height, which here is $5 \times 2 = 10$. The area of the triangle above is is $\dfrac{1}{2}bh = \dfrac{1}{2}(5)(8) = 8$. The sum is indeed 18! Why is this so? The trapezoidal rule uses the area under lines to estimate the area under the curve, but here the curve is itself a line. Hence, all the little trapezoids line up flush with the function itself, resulting in no under- or over-estimation.

2. Estimate $\displaystyle\int\limits_{-1}^{1} x^2 \, dx$ where $N=5$.

Step 1: Calculate $\Delta x$:

$\Delta x = \dfrac{b-a}{N} \\ \Delta x = \dfrac{1-(-1)}{5} \\ \Delta x = \dfrac{2}{5} \\$

Step 2: Calculate sum:

$\displaystyle\int\limits_{-1}^{1} x^2 \, dx \approx \dfrac{\Delta x}{2}\left(f(-1) + 2f\left(\dfrac{-3}{5}\right) + 2f\left(\dfrac{-1}{5}\right) + 2f\left(\dfrac{1}{5}\right) + 2f\left(\dfrac{3}{5}\right) + f(1) \right) \\ \displaystyle\int\limits_{-1}^{1} x^2 \, dx \approx \dfrac{1}{5} \left( (-1)^2 + 2\left(\dfrac{-3}{5}\right)^2 + 2\left(\dfrac{-1}{5}\right)^2 + 2\left(\dfrac{1}{5}\right)^2 + 2\left(\dfrac{3}{5}\right)^2 + 1^2 \right) \\ \displaystyle\int\limits_{-1}^{1} x^2 \, dx \approx \dfrac{1}{5} \left( 1 + 2\left(\dfrac{9}{25}\right) + 2\left(\dfrac{1}{25}\right) + 2\left(\dfrac{1}{25}\right) + 2\left(\dfrac{9}{25}\right) + 1 \right) \\ \displaystyle\int\limits_{-1}^{1} x^2 \, dx \approx \dfrac{1}{5} \left( 1 + \dfrac{18}{25} + \dfrac{2}{25} + \dfrac{2}{25} + \dfrac{18}{25} + 1 \right) \\ \displaystyle\int\limits_{-1}^{1} x^2 \, dx \approx \dfrac{18}{25} \\$

3. Estimate $\displaystyle\int\limits_{0}^{\pi} \sin(x) \, dx$ where $N=4$.

Step 1: Calculate $\Delta x$:

$\Delta x = \dfrac{b-a}{N} \\ \Delta x = \dfrac{\pi-0}{4} \\ \Delta x = \dfrac{\pi}{4} \\$

Step 2: Calculate sum:

$\displaystyle\int\limits_{0}^{\pi} \sin(x) \, dx \approx \dfrac{\Delta x}{2}\left( f(0) + 2f\left(\dfrac{\pi}{4}\right) + 2f\left(\dfrac{\pi}{2}\right) + 2f\left(\dfrac{3\pi}{4}\right) + f(\pi) \right) \\ \displaystyle\int\limits_{0}^{\pi} \sin(x) \, dx \approx \dfrac{\pi}{8}\left( \sin(0) + 2\sin\left(\dfrac{\pi}{4}\right) + 2\sin\left(\dfrac{\pi}{2}\right) + 2\sin\left(\dfrac{3\pi}{4}\right) + \sin(\pi) \right) \\ \displaystyle\int\limits_{0}^{\pi} \sin(x) \, dx \approx \dfrac{\pi}{8}\left( 0 + 2\left(\dfrac{\sqrt{2}}{2}\right) + 2(1) + 2\left(\dfrac{\sqrt{2}}{2}\right) + 0 \right) \\ \displaystyle\int\limits_{0}^{\pi} \sin(x) \, dx \approx \dfrac{\pi(1+\sqrt{2})}{4} \\$

4. Estimate $\displaystyle\int\limits_{0}^{2\pi} \cos(2\theta) \, d\theta$ where $N=4$.

Step 1: Calculate $\Delta x$:

$\Delta x = \dfrac{b-a}{N} \\ \Delta x = \dfrac{2\pi-0}{8} \\ \Delta x = \dfrac{\pi}{4} \\$

Step 2: Calculate sum:

$\displaystyle\int\limits_{0}^{2\pi} \cos(2\theta) \, d\theta \approx \dfrac{\Delta x}{2}\left( f(0) + 2f\left(\dfrac{\pi}{4}\right) + 2f\left(\dfrac{\pi}{2}\right) + 2f\left(\dfrac{3\pi}{4}\right) + 2f(\pi) + 2f\left(\dfrac{5\pi}{4}\right) + 2f\left(\dfrac{3\pi}{2}\right) + 2f\left(\dfrac{7\pi}{4}\right) + f(2\pi)\right) \\ \displaystyle\int\limits_{0}^{2\pi} \cos(2\theta) \, d\theta \approx \dfrac{\pi}{8}\left( \cos(2(0)) + 2\cos\left(2\dfrac{\pi}{4}\right) + 2\cos\left(2\dfrac{\pi}{2}\right) + 2\cos\left(2\dfrac{3\pi}{4}\right) + 2\cos(2\pi) + 2\cos\left(2\dfrac{5\pi}{4}\right) + 2\cos\left(2\dfrac{3\pi}{2}\right) + 2\cos\left(2\dfrac{7\pi}{4}\right) + \cos(4\pi)\right) \\ \displaystyle\int\limits_{0}^{2\pi} \cos(2\theta) \, d\theta \approx \dfrac{\pi}{8}\left( 1 + 2(0) + 2(-1) + 2(0) + 2(1) + 2(0) + 2(-1) + 2(0) + 1\right) \\ \displaystyle\int\limits_{0}^{2\pi} \cos(2\theta) \, d\theta \approx 0 \\$

5. Estimate $\displaystyle\int\limits_{-2}^{2} x^3 + 2x \, dx$ where $N=7$.

Step 1: Calculate $\Delta x$:

$\Delta x = \dfrac{b-a}{N} \\ \Delta x = \dfrac{2-(-1)}{7} \\ \Delta x = \dfrac{4}{7} \\$

Step 2: Calculate sum:

$\displaystyle\int\limits_{-2}^{2} x^3 + 2x \, dx \approx \dfrac{\Delta x}{2}\left( f(-2) + 2f\left(\dfrac{-10}{7}\right) + 2f\left(\dfrac{-6}{7}\right) + 2f\left(\dfrac{2}{7}\right) + 2f\left(\dfrac{2}{7}\right) + 2f\left(\dfrac{6}{7}\right) + 2f\left(\dfrac{11}{7}\right) + f(2) \right) \\ \displaystyle\int\limits_{-2}^{2} x^3 + 2x \, dx \approx \dfrac{\Delta x}{2}\left( \left((-2)^3 + 2(-2)\right) + 2\left(\left(-\dfrac{10}{7}\right)^3 - 2\left(\dfrac{10}{7}\right)\right) + \\ 2\left(\left(-\dfrac{6}{7}\right)^3 - 2\left(\dfrac{6}{7}\right)\right) + 2\left(\left(-\dfrac{2}{7}\right)^3 - 2\left(\dfrac{2}{7}\right)\right) + 2\left(\left(\dfrac{2}{7}\right)^3 + 2\left(\dfrac{2}{7}\right)\right) \\ + 2\left(\left(\dfrac{6}{7}\right)^3 + 2\left(\dfrac{6}{7}\right)\right) + 2\left(\left(\dfrac{10}{7}\right)^3 + 2\left(\dfrac{10}{7}\right)\right) + (2^3 + 2) \right) \\ \displaystyle\int\limits_{-2}^{2} x^3 + 2x \, dx \approx \dfrac{\Delta x}{2}\left( -12 - 2\left(\dfrac{1000}{343} + \dfrac{20}{7}\right) - \\ 2\left(\dfrac{216}{343} + \dfrac{12}{7}\right) - 2\left(\dfrac{8}{343} + \dfrac{4}{7}\right) + 2\left(\dfrac{8}{343} + \dfrac{4}{7}\right) \\ + 2\left(\dfrac{216}{343} + \dfrac{12}{7}\right) + 2\left(\dfrac{1000}{343} + \dfrac{20}{7}\right) + 12 \right) \\$

Rather than compute this all out, notice that each term is precisely negative by another term. Thus we can see that the integral is approximately zero:

$\displaystyle\int\limits_{-2}^{2} x^3 + 2x \, dx \approx 0 \\$

6. Estimate $\displaystyle\int\limits_{1}^{2} \dfrac{2}{x} \, dx$ where $N=5$.

Step 1: Calculate $\Delta x$:

$\Delta x = \dfrac{b-a}{N} \\ \Delta x = \dfrac{2-1}{5} \\ \Delta x = \dfrac{1}{5} \\$

Step 2: Calculate sum:

$\displaystyle\int\limits_{1}^{2} \dfrac{2}{x} \, dx \approx \dfrac{\Delta x}{2} \left( f(1) + 2f\left(\dfrac{6}{5}\right) + 2f\left(\dfrac{7}{5}\right) + 2f\left(\dfrac{8}{5}\right) + 2f\left(\dfrac{9}{5}\right) + f(2) \right) \\ \displaystyle\int\limits_{1}^{2} \dfrac{2}{x} \, dx \approx \dfrac{1}{10} \left( \dfrac{2}{1} + 2\dfrac{2}{\frac{6}{5}} + 2\dfrac{2}{\frac{7}{5}} + 2\dfrac{2}{\frac{8}{5}} + 2\dfrac{2}{\frac{9}{5}} + \dfrac{2}{2} \right) \\ \displaystyle\int\limits_{1}^{2} \dfrac{2}{x} \, dx \approx \dfrac{1}{10} \left( 2 + \dfrac{10}{3} + \dfrac{20}{7} + \dfrac{5}{2} + \dfrac{20}{9} + 1 \right) \\ \displaystyle\int\limits_{1}^{2} \dfrac{2}{x} \, dx \approx \dfrac{1753}{1260} \\$

7. Estimate $\displaystyle\int\limits_{-4}^{-1} 3x^4 + 7x + 2 \, dx$ where $N=6$.

Step 1: Calculate $\Delta x$:

$\Delta x = \dfrac{b-a}{N} \\ \Delta x = \dfrac{-1-(-4)}{6} \\ \Delta x = \dfrac{3}{6} \\ \Delta x = \dfrac{1}{2} \\$

Step 2: Calculate sum:

$\displaystyle\int\limits_{-4}^{-1} 3x^4 + 7x + 2 \, dx \approx \dfrac{\Delta x}{2}\left( f(-4) + 2f\left(-\dfrac{7}{2}\right) + 2f(-3) + 2f\left(-\dfrac{5}{2}\right) + \\ 2f(-2) + 2f\left(-\dfrac{3}{2}\right) + f(-1) \right) \\ \displaystyle\int\limits_{-4}^{-1} 3x^4 + 7x + 2 \, dx \approx \dfrac{1}{4} \left( \left( (-4)^4 + 7(-4) + 2 \right) + 2\left( \left(\dfrac{-7}{2}\right)^4 + 7\left(\dfrac{-7}{2}\right) + 2 \right) + \\ 2\left( (-3)^4 + 7(-3) + 2 \right) + 2\left( \left(\dfrac{-5}{2}\right)^4 + 7\left(\dfrac{-5}{2}\right) + 2 \right) + 2\left( (-2)^4 + 7(-2) + 2 \right) + \\ 2\left( \left(\dfrac{-3}{2}\right)^4 + 7\left(\dfrac{-3}{2}\right) + 2 \right) + \left( (-1)^4 + 7(-1) + 2 \right) \right) \\ \displaystyle\int\limits_{-4}^{-1} 3x^4 + 7x + 2 \, dx \approx \dfrac{1}{4} \left( \left( 256 - 28 + 2 \right) + 2\left( \dfrac{2401}{16} - \dfrac{49}{2} + 2 \right) + \\ 2\left( 81 - 21 + 2 \right) + 2\left( \dfrac{625}{16} - \dfrac{35}{2} + 2 \right) + 2\left( 16 - 14 + 2 \right) + \\ 2\left( \dfrac{81}{16} - \dfrac{21}{2} + 2 \right) + \left( 1 - 7 + 2 \right) \right) \\ \displaystyle\int\limits_{-4}^{-1} 3x^4 + 7x + 2 \, dx \approx \dfrac{1}{4} \left( 230 + \dfrac{2041}{8} + 124 + \dfrac{377}{8} + 8 - \dfrac{55}{8} - 4 \right) \\ \displaystyle\int\limits_{-4}^{-1} 3x^4 + 7x + 2 \, dx \approx \dfrac{5227}{32} \\$

8. Estimate $\displaystyle\int\limits_{0}^{8} 2^x \, dx$ where $N=4$.

Step 1: Calculate $\Delta x$:

$\Delta x = \dfrac{b-a}{N} \\ \Delta x = \dfrac{8-0}{4} \\ \Delta x = 2 \\$

Step 2: Calculate sum:

$\displaystyle\int\limits_{0}^{8} 2^x \, dx \approx \dfrac{\Delta x}{2} \left( f(0) + 2f(2) + 2f(4) + 2f(6) + f(8) \right) \\ \displaystyle\int\limits_{0}^{8} 2^x \, dx \approx \dfrac{2}{2} \left( 2^0 + 2(2^2) + 2(2^4) + 2(2^6) + 2^8 \right) \\ \displaystyle\int\limits_{0}^{8} 2^x \, dx \approx \left( 2^0 + 2^3 + 2^5 + 2^7 + 2^8 \right) \\ \displaystyle\int\limits_{0}^{8} 2^x \, dx \approx 425 \\$

9. Estimate $\displaystyle\int\limits_{0}^{8} 2^x \, dx$ where $N=8$.
Step 1: Calculate $\Delta x$:

$\Delta x = \dfrac{b-a}{N} \\ \Delta x = \dfrac{8-0}{8} \\ \Delta x = 1 \\$

Step 2: Calculate sum:

$\displaystyle\int\limits_{0}^{8} 2^x \, dx \approx \dfrac{\Delta x}{2} \left( f(0) + 2f(1) + 2f(2) + 2f(3) + 2f(4) + 2f(5) + 2f(6) + f(7) + f(8) \right) \\ \displaystyle\int\limits_{0}^{8} 2^x \, dx \approx \dfrac{1}{2} \left( 2^0+ 2(2^1) + 2(2^2) + 2(2^3) + 2(2^4) + 2(2^5) + 2(2^6) + 2(2^7) + 2^8 \right) \\ \displaystyle\int\limits_{0}^{8} 2^x \, dx \approx \dfrac{1}{2} + 2^1 + 2^2 + 2^3 + 2^4 + 2^5 + 2^6 + 2^7 + 2^7 \\ \displaystyle\int\limits_{0}^{8} 2^x \, dx \approx \dfrac{765}{2} \\$
10. Estimate $\displaystyle\int\limits_{a}^{b} C \, dx$ where $N=N_0 \geq 1$.

Based on what you know about geometry, what is the exact solution? Does trapezoidal rule produce the exact result?

Step 1: Calculate $\Delta x$:

$\Delta x = \dfrac{b-a}{N} \\ \Delta x = \dfrac{b-a}{N_0} \\$

Step 2: Calculate sum:

$\displaystyle\int\limits_{a}^{b} C \, dx \approx \dfrac{b-a}{2N_0} \left( C + \underbrace{2C + 2C + \cdots + 2C}_{N_0-1} + C \right) \\ \displaystyle\int\limits_{a}^{b} C \, dx \approx \dfrac{b-a}{2N_0} \left( 2C + \underbrace{2C + 2C + \cdots + 2C}_{N_0-1} \right) \\ \displaystyle\int\limits_{a}^{b} C \, dx \approx \dfrac{b-a}{2N_0} \left( 2N_0C \right) \\ \displaystyle\int\limits_{a}^{b} C \, dx \approx C(b-a) \\$

The graph is a rectangle, and geometry tells us that the area of a rectangle is width $*$ height. Here, the height of the rectangle is $C$ and the width is $b?a$. We can see that the result of the trapezoidal rule is exact:

$\displaystyle\int\limits_{a}^{b} C \, dx \approx \dfrac{b-a}{2N_0} \left( C + \underbrace{2C + 2C + \cdots + 2C}_{N_0-1} + C \right) = C(b-a) \\$

This shouldn't be surprising. The trapezoidal rule uses trapezoids to estimate the area under a curve, and when the curve is flat, the trapezoids become rectangles. Since the curve being split up is itself a rectangle, the trapezoids cover the area under the curve exactly.