Convergence Tests
Most series cannot be summed in closed form, but you can often still prove whether they converge. This lesson is a toolbox of tests: which one to reach for, what it says, and how to check its verdict numerically in Python.
The toolbox
| Test | Use it when | Verdict |
|---|---|---|
| nth-term (divergence) test | always try first | . ( says nothing.) |
| Geometric series | terms are | |
| p-series | terms are | |
| Comparison | terms are like a known series | ; |
| Limit comparison | terms behave like for large | |
| Integral test | , positive and decreasing | and converge or diverge together |
| Ratio test | factorials, powers like or | : converges, diverges, no verdict |
| Alternating series test | signs alternate |
The p-series and the integral test
The integral test turns sums into integrals you know. For :
So converges (its sum is the famous ), while and diverge.
import sympy as sp
n = sp.symbols("n", integer=True, positive=True)
print("∑ 1/n^2 =", sp.summation(1 / n**2, (n, 1, sp.oo)))
print("∑ 1/n =", sp.summation(1 / n, (n, 1, sp.oo)))
print("∑ n/2^n =", sp.summation(n / 2**n, (n, 1, sp.oo)))
S = sum(1 / k**2 for k in range(1, 1001))
print(f"1000 terms of ∑ 1/n^2 = {S:.6f}, π²/6 = {float(sp.pi**2 / 6):.6f}")∑ 1/n^2 = pi**2/6
∑ 1/n = oo
∑ n/2^n = 2
1000 terms of ∑ 1/n^2 = 1.643935, π²/6 = 1.644934
oo is SymPy's . The 1000-term partial sum is still about 0.001 short: the tail is roughly , which is the integral test giving an error estimate for free.
The ratio test
For terms built from factorials and exponentials, compare each term with the previous one. For :
For the ratio is , so it diverges.
import math
def ratios(a, ns):
return [round(a(k + 1) / a(k), 5) for k in ns]
ns = [1, 10, 100]
print("n/2^n :", ratios(lambda k: k / 2**k, ns), "→ 0.5, converges")
print("10^n/n! :", ratios(lambda k: 10**k / math.factorial(k), ns), "→ 0, converges")
print("n^n/n! :", ratios(lambda k: k**k / math.factorial(k), ns), "→ e, diverges")
print("1/n^2 :", ratios(lambda k: 1 / k**2, ns), "→ 1, no verdict")n/2^n : [1.0, 0.55, 0.505] → 0.5, converges
10^n/n! : [5.0, 0.90909, 0.09901] → 0, converges
n^n/n! : [2.0, 2.59374, 2.70481] → e, diverges
1/n^2 : [0.25, 0.82645, 0.9803] → 1, no verdict
The last line shows the ratio test's blind spot: for any p-series the ratio tends to 1, so it cannot tell (converges) from (diverges).
Alternating series
If the signs alternate and the sizes shrink to 0, the series converges, and the error after stopping is at most the first term you left out. The alternating harmonic series converges to :
import math
S = 0.0
for k in range(1, 1001):
S += (-1) ** (k + 1) / k
if k in (10, 100, 1000):
print(f"{k:5d} terms: {S:.6f} error {abs(S - math.log(2)):.2e} bound 1/(n+1) = {1 / (k + 1):.2e}") 10 terms: 0.645635 error 4.75e-02 bound 1/(n+1) = 9.09e-02
100 terms: 0.688172 error 4.98e-03 bound 1/(n+1) = 9.90e-03
1000 terms: 0.692647 error 5.00e-04 bound 1/(n+1) = 9.99e-04
A series like this, which converges but whose absolute values diverge, is conditionally convergent. If converges too, the series is absolutely convergent, which is the safer kind: you can reorder its terms freely.
Note: Reordering a conditionally convergent series can change its sum (Riemann's rearrangement theorem). Floating-point code has a cousin of this problem: summing the same numbers in a different order gives slightly different results.
In software
- Loop termination: before writing
while term > eps: total += term, a convergence test tells you whether the loop will ever give a stable answer, and the error bounds above tell you when to stop. - Algorithm analysis: the cost of the inner steps of an algorithm is harmonic (), which is why expected quicksort comparisons and the coupon-collector problem have behaviour (see Data Structures & Algorithms).
- Dynamic arrays: a geometric sum over resize steps shows that doubling a dynamic array costs per insert on average.
Exercises
Exercise 1: Quick divergence
Does converge?
Answer
No, it diverges.
Solution
Exercise 2: Pick the test
Decide convergence: a) , b) , c) .
Answer
a) converges, b) converges, c) diverges.
Solution
Exercise 3: Ratio test with a factorial
Show that converges and check numerically that its sum is .
Answer
Ratio , so it converges; the sum is .
Solution
import math
S = sum(3**k / math.factorial(k) for k in range(30))
print(f"{S:.10f} {math.exp(3):.10f}")20.0855369232 20.0855369232
Exercise 4: How many terms?
How many terms of guarantee an error below ? Check it.
Answer
terms (the 32nd term is the first one below 0.001).
Solution
The alternating series bound says . We need , so , . The exact sum is .
import math
S = sum((-1) ** (k + 1) / k**2 for k in range(1, 32))
print(f"error after 31 terms: {abs(S - math.pi**2 / 12):.6f}")error after 31 terms: 0.000504
Test yourself
The ratio test gives for a series. What do you know?
- It converges
- It diverges
- Its sum is 1
- Nothing: try another test
Which series converges?
You stop an alternating series (decreasing terms) after the term . What is the most the error can be?
- The next term you skipped
- 0
- Twice the last term