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:
Your task is to evaluate from this series alone for some and some , with a relative error under one part in . An infinite sum is mathematics, not an algorithm: the machine has to stop. The algorithm is the finite recipe
The remaining question is when 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 that forces a large ; the terms may even grow for a long stretch before they start to shrink and the whole sum settles.
Do not form 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 — 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 past . The ratio stays large until has outrun ; then it drops through the dashed line.
Power-series summation
1 / 4Why is the infinite sine series not yet an algorithm?
Pick an answer to see if you are right.
Lab: to
Implement the recurrence and the stop. Do not call math.sin or math.factorial. The wrapper prints eight digits after the decimal. One test uses and one uses .
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 is tighter than the machine can represent, the ratio will bottom out around 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, reduction, bad factorial form, and short write-ups.