A631 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

A6 · Lesson 31 · Lab: summing series in code

Getting many digits from few terms

The alternating harmonic series converges to ln⁡2, but ten terms leave an error of 0.05, and a million leave 5×10−7. Libraries don't sum series that way. They estimate the tail they haven't added, or combine a few partial sums to cancel their errors. In this lab you'll sum slow series in Python, then speed them up two ways: Aitken's extrapolation for alternating series, and integral corrections for the tail of a p-series. You'll also add the same terms in a different order and watch the sum move.

  • The stems are the partial sums Sn of 1−12+13−⋯. They zigzag around ln⁡2, closing in only as 1/n.
  • The dashed line is ln⁡2. Aitken's method takes three neighboring partial sums and extrapolates the zigzag to its center.
  • The readout gives the error of Sn and of the Aitken value built from Sn−2, Sn−1, Sn.

Predict

Ten terms of 1−12+13−⋯ leave an error of about 0.05. How many terms would plain summing need for an error of 10−6?

Name it

Aitken's extrapolation

If the errors of three successive partial sums shrink by a roughly constant factor, the limit is close to

A=Sn+2−(Sn+2−Sn+1)2(Sn+2−Sn+1)−(Sn+1−Sn)

It works especially well on alternating series, whose partial sums overshoot and undershoot in a regular pattern.

Correcting a p-series' tail

For ∑1np, add the first N terms, then estimate the rest. The integral ∫N∞x−pdx=N1−pp−1 is the main part of the tail; the Euler–Maclaurin formula adds two smaller corrections:

∑n=1∞1np≈∑n=1N1np+N1−pp−1−12Np+p12Np+1

With N=10 and p=2 this lands within 4×10−7 of π26; the plain partial sum is off by 0.095.

Rearranging

Taking a positive terms of the alternating harmonic series for each negative one gives ln⁡2+12ln⁡a. Summing it in that order in code shows the sum really moves.

Worked example

Apply Aitken's method to S9=0.745635, S10=0.645635, S11=0.736544 of the alternating harmonic series.

Show the solution
  1. Take the differences. S10−S9=−0.1S11−S10=+0.090909
  2. Apply Aitken's formula. A=S11−0.09090920.090909+0.1=0.736544−0.043290=0.693254
  3. Compare. The true value is 0.693 147: an error of 0.000 11 from partial sums whose own errors are about 0.05.

Answer Aitken's method gives 0.693 254, within 0.000 11 of the true 0.693 147.

Worked example

Estimate ∑1n2 from ten terms and the tail correction.

Show the solution
  1. Add ten terms. The ten terms add to 1.549 768.
  2. Add the tail corrections. 10−11=0.1−1200=−0.005212×1000=0.000167 giving 1.644 934.
  3. Compare. The true value, π26, is 1.644 934 07: right to six places from ten terms.

Answer ten terms and the tail correction give 1.644 934, right to six places.

Check

About how far is the nth partial sum of the alternating harmonic series from ln⁡2, for n = 100?

Check

What does the integral contribute to the tail of ∑1n3 after 10 terms?

Check

Write aitken(s): given a list of partial sums s, return the list of Aitken values from each three consecutive sums. Set S to the first 11 partial sums of the alternating harmonic series and A = aitken(S).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write zeta(p, N): the sum of the first N terms of ∑1np plus the three tail corrections from the reading. Set z = zeta(p, 10) for p = 3.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write rearranged(a, groups): sum the alternating harmonic series taking a positive (odd-denominator) terms, then one negative (even-denominator) term, for groups rounds. Set r = rearranged(a, 100000) and target to ln⁡2+12ln⁡a.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write harmonic_N(T) returning the first N with HN=1+12+⋯+1N≥T. Set n5 = harmonic_N(5) and compare it with the estimate eT−0.5772.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write sum_until(tol): add 1k! for k=1,2,…, stopping before the first term below tol, and return (total, k) where k is that first small term's index. Check that the total is close to e−1.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does Aitken's method work so well on the alternating harmonic series' partial sums?

Practice

Keep practicing

6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.