41. Adv: Fourier series — a first look

📖 Reading · 10 min
💡 Every code box below is live — edit it and hit Run.

Every series in this module has been a power series — built from 1,x,x^2,x^3,\ldots. This closing lesson swaps that building-block set for a different one: 1,\cos x,\sin x,\cos2x,\sin2x,\ldots. The payoff is a way to represent periodic functions — including ones with corners, jumps, or no elementary formula at all — as an infinite sum of pure oscillations, using nothing beyond §4.7's orthogonality identities and §8.4's uniform convergence.

Why sines and cosines, and why they can be separated out

§4.7 proved the trigonometric functions are orthogonal on [-\pi,\pi]:

\int_{-\pi}^\pi\sin(mx)\sin(nx)\,dx=\begin{cases}\pi&m=n\\0&m\ne n\end{cases}\qquad\int_{-\pi}^\pi\cos(mx)\cos(nx)\,dx=\begin{cases}\pi&m=n\\0&m\ne n\end{cases}

\int_{-\pi}^\pi\sin(mx)\cos(nx)\,dx=0\ \text{(always)}

Suppose a function can be written as f(x)=\frac{a_0}2+\sum_{n=1}^\infty\big[a_n\cos(nx)+b_n\sin(nx)\big]. Multiply both sides by \cos(mx) and integrate over [-\pi,\pi] (term by term — licensed by §8.4's uniform convergence, exactly the same justification power series needed for term-by-term operations). Orthogonality kills every term except the one matching m, isolating a single coefficient:

\int_{-\pi}^\pi f(x)\cos(mx)\,dx=a_m\int_{-\pi}^\pi\cos^2(mx)\,dx=a_m\pi

a_m=\frac1\pi\int_{-\pi}^\pi f(x)\cos(mx)\,dx

The identical trick with \sin(mx) isolates b_m:

b_m=\frac1\pi\int_{-\pi}^\pi f(x)\sin(mx)\,dx

This is the same "match coefficients by making everything else vanish" strategy §8.1 used for Taylor coefficients — there, differentiation and evaluation at the center killed every term but one; here, multiplying by an orthogonal partner and integrating does the same job.

Odd and even functions simplify everything

If f is odd (f(-x)=-f(x), §0.2), every a_n vanishes automatically: f(x)\cos(nx) is odd (odd times even), and an odd function integrates to 0 over a symmetric interval (§4.7's worked example used exactly this fact). Only the sine terms — themselves odd — survive.

If f is even, the mirror argument kills every b_n, leaving only cosine terms.

Doing it in Python

The Fourier sine series of f(x)=x on (-\pi,\pi) — an odd function, so only b_n terms appear:

import sympy as sp

x, n = sp.symbols('x n')
n = sp.Symbol('n', positive=True, integer=True)
f = x

b_n = sp.simplify(sp.Rational(1, 1) / sp.pi * sp.integrate(f * sp.sin(n * x), (x, -sp.pi, sp.pi)))
print(f"b_n = {b_n}")
for k in range(1, 5):
    print(f"  b_{k} = {b_n.subs(n, k)}")

The Fourier series of a square wave — a genuinely discontinuous function, something no Taylor series could ever represent, since Taylor series require infinitely many derivatives to exist at the center:

import sympy as sp

x, n = sp.symbols('x n')
n = sp.Symbol('n', positive=True, integer=True)

# f(x) = -1 on (-pi, 0), +1 on (0, pi)
b_n = sp.simplify(
    sp.Rational(1, 1) / sp.pi * (
        sp.integrate(-1 * sp.sin(n * x), (x, -sp.pi, 0)) +
        sp.integrate(1 * sp.sin(n * x), (x, 0, sp.pi))
    )
)
print(f"b_n = {b_n}")
for k in range(1, 7):
    print(f"  b_{k} = {b_n.subs(n, k)}")
print("\nzero for even n, 4/(n*pi) for odd n -- only odd harmonics appear")

Approximating the square wave with a truncated Fourier series and checking it against the true value away from the jump:

import math

def square_wave_approx(x, n_terms):
    return (4 / math.pi) * sum(math.sin((2*k+1) * x) / (2*k+1) for k in range(n_terms))

test_x = math.pi / 2   # true value here is +1 (comfortably inside (0, pi))
for n_terms in (1, 5, 20, 100):
    approx = square_wave_approx(test_x, n_terms)
    print(f"{n_terms:>4} terms: approx = {approx:.6f}  (true value: 1)")

Worked example

Find the Fourier series of f(x)=x on (-\pi,\pi).

f(x)=x is odd, so every a_n=0 immediately, without computing a single integral. For b_n, integrate by parts (§4.6):

b_n=\frac1\pi\int_{-\pi}^\pi x\sin(nx)\,dx

With u=x, dv=\sin(nx)\,dx, so du=dx, v=-\frac{\cos(nx)}n:

\int x\sin(nx)\,dx=-\frac{x\cos(nx)}n+\frac1n\int\cos(nx)\,dx=-\frac{x\cos(nx)}n+\frac{\sin(nx)}{n^2}

Evaluating from -\pi to \pi: the \sin(nx) term vanishes at both endpoints (\sin(\pm n\pi)=0), leaving

\left[-\frac{x\cos(nx)}n\right]_{-\pi}^\pi=-\frac{\pi\cos(n\pi)}n-\frac{\pi\cos(n\pi)}n=-\frac{2\pi\cos(n\pi)}n

Since \cos(n\pi)=(-1)^n:

b_n=\frac1\pi\cdot\left(-\frac{2\pi(-1)^n}n\right)=\frac{-2(-1)^n}n=\frac{2(-1)^{n+1}}n

\boxed{x=2\sum_{n=1}^\infty\frac{(-1)^{n+1}}n\sin(nx)=2\left(\sin x-\frac{\sin2x}2+\frac{\sin3x}3-\cdots\right)}

Sanity check. At x=\frac\pi2: the right side becomes 2\left(1-0+\left(-\frac13\right)+0+\frac15+\cdots\right) (every even-n term vanishes since \sin(n\pi/2)=0 for even n), i.e. 2\left(1-\frac13+\frac15-\cdots\right) — that's exactly §8.3's Leibniz series for \frac\pi4, so the sum equals 2\cdot\frac\pi4=\frac\pi2, matching f\left(\frac\pi2\right)=\frac\pi2 exactly. ✓ A Fourier series built from a totally different derivation reproduces a result already seen from computing \pi directly — strong independent confirmation.

Your turn

1. Without computing any integral, explain why f(x)=x^2 on (-\pi,\pi) has a Fourier series containing only cosine terms.

2. For the square wave from the "Doing it in Python" section (f(x)=-1 on (-\pi,0), f(x)=1 on (0,\pi)), use the pattern b_n=\frac4{n\pi} (odd n), 0 (even n) to write out the first three nonzero terms of its Fourier series explicitly.

3. True or false: because sines and cosines are infinitely differentiable, every function representable by a Fourier series must also be infinitely differentiable.

Solutions

1. f(x)=x^2 is even (f(-x)=(-x)^2=x^2=f(x)). By the odd/even argument in the concept section, an even function's Fourier series has every b_n=0, leaving only the constant term a_0 and the cosine terms a_n\cos(nx) — no computation needed, purely from the symmetry.

2. Using b_1=\frac4\pi, b_3=\frac4{3\pi}, b_5=\frac4{5\pi} (odd n only, even n contributing 0):

f(x)\approx\frac4\pi\sin x+\frac4{3\pi}\sin3x+\frac4{5\pi}\sin5x+\cdots=\frac4\pi\left(\sin x+\frac{\sin3x}3+\frac{\sin5x}5+\cdots\right)

3. False. This is the entire point of using Fourier series where Taylor series can't reach: the square wave in this lesson is discontinuous — not even continuous, let alone differentiable — and yet it has a perfectly well-defined Fourier series built from infinitely-differentiable sines and cosines. A Fourier series can converge to a function with jumps or corners; the smoothness of the building blocks doesn't transfer to the sum, the same lesson §8.4 already taught about pointwise limits not automatically inheriting the terms' good properties.

Check yourself in code

Compute the Fourier sine coefficients b_1 through b_5 for the square wave (f(x)=-1 on (-\pi,0), f(x)=1 on (0,\pi)).

Print exactly this:

b_1 = 4/pi
b_2 = 0
b_3 = 4/(3*pi)
b_4 = 0
b_5 = 4/(5*pi)
import sympy as sp

x = sp.Symbol('x')
n = sp.Symbol('n', positive=True, integer=True)

b_n = sp.simplify(
    sp.Rational(1, 1) / sp.pi * (
        sp.integrate(-1 * sp.sin(n * x), (x, -sp.pi, 0)) +
        sp.integrate(1 * sp.sin(n * x), (x, 0, sp.pi))
    )
)

for k in range(1, 6):
    print(f"b_{k} = ...")
import sympy as sp

x = sp.Symbol('x')
n = sp.Symbol('n', positive=True, integer=True)

b_n = sp.simplify(
    sp.Rational(1, 1) / sp.pi * (
        sp.integrate(-1 * sp.sin(n * x), (x, -sp.pi, 0)) +
        sp.integrate(1 * sp.sin(n * x), (x, 0, sp.pi))
    )
)

for k in range(1, 6):
    print(f"b_{k} = {b_n.subs(n, k)}")

A Fourier series rebuilds §8.1's coefficient-matching idea with sines and cosines in place of powers of x, using §4.7's orthogonality to isolate each coefficient by multiplying and integrating rather than differentiating and evaluating. The payoff is real: Fourier series can represent functions — like a square wave — that are discontinuous and have no Taylor series at all, because the building blocks only need to be integrable against, not matched derivative-by-derivative at a single point.

That closes this module, and with it, every single-variable topic this course covers — limits, derivatives, integrals, their applications, and now both major families of infinite series used to represent functions. Next: calculus breaks free of the single x-axis entirely, starting with vectors and curves that live in three-dimensional space.