Lesson 8 of 18

Alternating Series

Alternating Series

An alternating series has terms that switch sign:

sumk=1infty(1)k+1ak=a1a2+a3a4+cdotssum_{k=1}^{infty} (-1)^{k+1} a_k = a_1 - a_2 + a_3 - a_4 + cdots

Alternating Series Test (Leibniz)

If ak>0a_k > 0, aka_k is decreasing, and ako0a_k o 0, then the series converges.

Famous Example: Alternating Harmonic

1 - rac{1}{2} + rac{1}{3} - rac{1}{4} + cdots = ln(2) approx 0.6931

The partial sums oscillate around ln(2)ln(2), narrowing with each term.

Error Bound

The error of stopping at term nn is at most an+1|a_{n+1}| — the next term's absolute value. This makes alternating series easy to approximate with controlled accuracy.

Comparison

nnSnS_n (alternating harmonic)
111.00001.0000
220.50000.5000
330.83330.8333
440.58330.5833
550.78330.7833
inftyinfty0.69310.6931

The partial sums zigzag toward ln(2)ln(2), each one closer than the last.

Your Task

Implement double alternating_sum(double (*a_n)(int), int n) that computes sumk=1n(1)k+1an(k)sum_{k=1}^n (-1)^{k+1} a_n(k).

TCC compiler loading...
Loading...
Click "Run" to execute your code.