Skip to main content

3.3 Errors with power series

Source: Series-summation ideas from Computational Physics (Landau, Páez, Bordeianu), §3.3. Rewritten in original wording. Continues 3.2 Experimental error investigation.

A standard job in computational physics is to turn a series into a number. The sine expansion is exact as an infinite sum:

sin⁡x=x−x33!+x55!−x77!+⋯\sin x = x - \frac{x^{3}}{3!} + \frac{x^{5}}{5!} - \frac{x^{7}}{7!} + \cdots

Your task is to evaluate sin⁡x\sin x from this series alone for some x<2πx<2\pi and some x>2πx>2\pi, with a relative error under one part in 10810^{8}. An infinite sum is mathematics, not an algorithm: the machine has to stop. The algorithm is the finite recipe

sin⁡x≃∑n=1N(−1)n−1x2n−1(2n−1)!.\sin x \simeq \sum_{n=1}^{N} \frac{(-1)^{n-1} x^{2n-1}}{(2n-1)!}.

The remaining question is when NN is enough. Do not stop because the printout matches a table or math.sin — that is grading the answer with the answer. A usable test is: the next term is already smaller than the error you are willing to live with. For large ∣x∣|x| that forces a large NN; the terms may even grow for a long stretch before they start to shrink and the whole sum settles.

Do not form x2n−1/(2n−1)!x^{2n-1}/(2n-1)! from scratch​

Computing a huge power and a huge factorial, then dividing, is a bad loop. Neighbouring terms are cheaper — one multiply per step — and the last term is a usable stop proxy while round-off is small. Walk the finite sum, the recursive ratio, and ∣tn/Sn∣<ε|t_n/S_n|<\varepsilon — pick the matching equation at each step.

Theorem (term as remainder). If round-off is not yet in charge, the first omitted term is a usable estimate of the truncation error. Stop when that term is a small fraction of the running sum.

term ← x, sum ← x, n ← 1, ε ← 1e-8
repeat
n ← n + 1
term ← term * (-x*x) / ((2n-1)*(2n-2))
sum ← sum + term
until |term/sum| < ε

Slide xx past 2π2\pi. The ratio ∣tn/Sn∣|t_n/S_n| stays large until nn has outrun ∣x∣|x|; then it drops through the dashed ε\varepsilon line.

Quiz

Power-series summation

1 / 4

Why is the infinite sine series not yet an algorithm?

Pick an answer to see if you are right.

Lab: sin⁡x\sin x to 10−810^{-8}​

Implement the recurrence and the ∣tn/Sn∣|t_n/S_n| stop. Do not call math.sin or math.factorial. The wrapper prints eight digits after the decimal. One test uses x<2πx<2\pi and one uses x>2πx>2\pi.

Activity: Sine by recurrence

Input: x eps. Print my_sin(x, eps) with :.8f. Start from term = sum = x. Each new term is the previous times -x*x/((2*n-1)*(2*n-2)) with n counting terms (n starts at 1, then 2, 3, …).

If ε\varepsilon is tighter than the machine can represent, the ratio ∣tn/Sn∣|t_n/S_n| will bottom out around ϵm\epsilon_m and the loop may spin. Keep the requested tolerance above machine precision, as in 3.2’s trough: more terms are not always more digits.

Next: 3.3b Series labs — table, false convergence, cancellation dump, 2π2\pi reduction, bad factorial form, and short write-ups.