A6 · Lesson 31 · Lab: summing series in code
Getting many digits from few terms
The alternating harmonic series converges to , but ten terms leave an error of 0.05, and a million leave . 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 of . They zigzag around , closing in only as .
- The dashed line is . Aitken's method takes three neighboring partial sums and extrapolates the zigzag to its center.
- The readout gives the error of and of the Aitken value built from , , .
Predict
Ten terms of leave an error of about 0.05. How many terms would plain summing need for an error of ?
About 500 000. The error after terms is about , so needs . Aitken's method gets an error near from the first eleven terms, and applying it again does better still: acceleration beats brute force by orders of magnitude.
Name it
Aitken's extrapolation
If the errors of three successive partial sums shrink by a roughly constant factor, the limit is close to
It works especially well on alternating series, whose partial sums overshoot and undershoot in a regular pattern.
Correcting a p-series' tail
For , add the first terms, then estimate the rest. The integral is the main part of the tail; the Euler–Maclaurin formula adds two smaller corrections:
With and this lands within of ; the plain partial sum is off by 0.095.
Rearranging
Taking positive terms of the alternating harmonic series for each negative one gives . Summing it in that order in code shows the sum really moves.
Worked example
Apply Aitken's method to , , of the alternating harmonic series.
Show the solution
- Take the differences.
- Apply Aitken's formula.
- 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 from ten terms and the tail correction.
Show the solution
- Add ten terms. The ten terms add to 1.549 768.
- Add the tail corrections. giving 1.644 934.
- Compare. The true value, , 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 th partial sum of the alternating harmonic series from , for = 100?
Check
What does the integral contribute to the tail of 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 plus the three tail corrections from the reading. Set z = zeta(p, 10) for = 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 .
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 with . Set n5 = harmonic_N(5) and compare it with the estimate .
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 for , 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 .
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?
Aitken's formula assumes the errors of successive partial sums form a geometric sequence, , and finds the limit that a geometric approach would have. For an alternating series the partial sums jump back and forth across the limit, with errors of nearly equal size and opposite sign, so , and the formula essentially averages neighboring partial sums with the right weights to land on the center of the zigzag. The leftover error comes from the errors not being exactly geometric (they shrink like ), which is why one round gives about and a second round, applied to the already-improved values, gains more. For series whose errors aren't close to geometric, Aitken can help little or even mislead, so it's checked against a known case first.
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.