Training Numerical Methods Numerical Integration
3 / 5

Numerical Integration

25 min Numerical Methods

Numerical Integration

Trapezoidal Rule

$$\int_a^b f(x)\,dx \approx \frac{h}{2}\left[f(a) + 2\sum_{i=1}^{n-1} f(x_i) + f(b)\right]$$

$h = (b-a)/n$, $x_i = a + ih$. Error $= O(h^2)$.

Simpson's Rule

Requires $n$ even:

$$\int_a^b f(x)\,dx \approx \frac{h}{3}\left[f(a) + 4\sum_{\text{odd } i} f(x_i) + 2\sum_{\text{even } i} f(x_i) + f(b)\right]$$

Error $= O(h^4)$ — much more accurate.

Example 1

Trapezoidal rule for $\int_0^1 x^2\,dx$ with $n=4$.

$h = 0.25$. Points: $f(0)=0, f(0.25)=0.0625, f(0.5)=0.25, f(0.75)=0.5625, f(1)=1$.

$T = \frac{0.25}{2}[0+2(0.0625+0.25+0.5625)+1] = 0.125[0+1.75+1] = 0.34375$.

Exact: $1/3 \approx 0.3333$.

Example 2

Simpson's rule for the same integral with $n=4$.

$S = \frac{0.25}{3}[0 + 4(0.0625+0.5625) + 2(0.25) + 1] = \frac{0.25}{3}[0+2.5+0.5+1] = \frac{0.25 \cdot 4}{3} = 0.3333$.

Exact! Simpson's is exact for polynomials of degree $\leq 3$.

Example 3

Estimate $\int_1^3 1/x\,dx$ using the trapezoidal rule with $n=2$.

$h=1$. $T = \frac{1}{2}[f(1)+2f(2)+f(3)] = 0.5[1+1+0.333] = 1.167$. Exact $= \ln 3 \approx 1.099$.

Practice Problems

1. Trapezoidal: $\int_0^2 x^3\,dx$, $n=4$.
2. Simpson's: $\int_0^2 x^3\,dx$, $n=4$.
3. Compare both to exact value.
4. Trapezoidal: $\int_0^{\pi} \sin x\,dx$, $n=4$.
5. Why must $n$ be even for Simpson's?
6. Error order for trapezoidal?
7. Error order for Simpson's?
8. Midpoint rule formula?
9. $\int_0^1 e^{-x^2}\,dx$ with $n=2$, trapezoidal.
10. Why is numerical integration necessary for $e^{-x^2}$?
11. If $n$ is doubled, trapezoidal error is divided by?
12. Simpson's: $\int_0^1 \frac{1}{1+x^2}\,dx$, $n=4$. Compare to $\pi/4$.
Show Answer Key

1. $h=0.5$; $T = 0.25[0+2(0.125+1+3.375)+8] = 0.25[0+9+8] = 4.25$

2. $S = (0.5/3)[0+4(0.125+3.375)+2(1)+8] = (0.5/3)[14+2+8] = 4.0$

3. Exact $= 4$. Simpson's exact (degree 3); Trapezoidal off by 0.25

4. $h = \pi/4$; $T \approx 1.8961$. Exact $= 2$.

5. Uses parabolic arcs through 3 consecutive points → need pairs of subintervals

6. $O(h^2)$

7. $O(h^4)$

8. $M = h\sum_{i=0}^{n-1} f(\bar{x}_i)$ where $\bar{x}_i$ is midpoint of $[x_i,x_{i+1}]$

9. $h=0.5$; $T = 0.25[1+2e^{-0.25}+e^{-1}] \approx 0.25[1+1.5576+0.3679] \approx 0.7314$

10. $e^{-x^2}$ has no closed-form antiderivative

11. $4$ (error is $O(h^2)$; $h$ halved → $h^2$ quartered)

12. $S \approx 0.7854 \approx \pi/4$; very close