Calculus · Infinite series

Fourier Series: Writing Any Shape as a Sum of Waves

A Taylor series builds a function out of powers of \(x\). A Fourier series builds it out of sines and cosines, and that works even for functions with corners and jumps, where Taylor series fail completely. This post explains when a function has a Fourier series, how to compute it, and how it leads to surprising results like \( 1 + \frac14 + \frac19 + \cdots = \frac{\pi^2}{6} \).

1. The idea: functions made of waves

A musical note from a violin and the same note from a flute have the same pitch but sound different. The reason: each instrument produces a fundamental wave plus a different mix of overtones, waves vibrating 2, 3, 4, ... times faster. Fourier's insight was that this works for almost any periodic shape: it can be written as a sum of sines and cosines with frequencies \(1, 2, 3, \dots\) times a basic frequency.

For a function on the interval \((-l, l)\), the building blocks are

\[ 1, \quad \cos\frac{\pi x}{l},\ \sin\frac{\pi x}{l}, \quad \cos\frac{2\pi x}{l},\ \sin\frac{2\pi x}{l}, \quad \dots \]

all of which repeat with period \(2l\). The question is how much of each one to take.

Compared with Taylor series, Fourier series have a big advantage: a Taylor series needs a function that is infinitely differentiable and is built from information at a single point. A Fourier series uses information from the whole interval and needs very little smoothness. Even a function with jumps can be represented.

2. Dirichlet conditions

Definition A function \(f(x)\) satisfies the Dirichlet conditions on the interval \((a, b)\) if, on that interval,
  1. it is bounded: \( |f(x)| \le M \) for every \(x \in (a, b)\), where \(M\) is a constant;
  2. it has at most finitely many points of discontinuity, all of the first kind: at each point of discontinuity \(\xi\), the finite left and right limits \(f(\xi - 0)\) and \(f(\xi + 0)\) exist;
  3. it has at most finitely many strict extrema.

In plain terms: the graph is made of finitely many smooth-ish, monotone pieces, possibly with finitely many jumps between them. Step functions, sawtooth waves, \(|x|\), \(x^2\) and every polynomial qualify. What does not qualify is a function that oscillates infinitely often, such as \( \sin\frac1x \) near \(0\) (it is bounded, but has infinitely many maxima and minima), or one that blows up, like \( \frac1x \).

3. The expansion theorem

Theorem · Expansion in a Fourier series If \(f(x)\) satisfies the Dirichlet conditions on \((-l, l)\), then at every point \(x\) of the interval where \[ f(x) = \frac12\big[f(x - 0) + f(x + 0)\big], \] the function can be represented by the Fourier series \[ f(x) = \frac{a_0}{2} + \sum_{n=1}^\infty\left(a_n\cos\frac{n\pi x}{l} + b_n\sin\frac{n\pi x}{l}\right), \tag{1} \] where \[ a_n = \frac1l\int_{-l}^{l} f(x)\cos\frac{n\pi x}{l}\,dx \quad (n = 0, 1, 2, \dots), \qquad b_n = \frac1l\int_{-l}^{l} f(x)\sin\frac{n\pi x}{l}\,dx \quad (n = 1, 2, 3, \dots). \tag{2} \]

How to read the condition. At a point where \(f\) is continuous, \(f(x - 0) = f(x + 0) = f(x)\), so the condition holds automatically and the series equals \(f(x)\). At a jump, the series always converges to the average of the left and right limits, the midpoint of the jump. So, more precisely:

\[ \frac{a_0}{2} + \sum_{n=1}^\infty\Big(a_n\cos\frac{n\pi x}{l} + b_n\sin\frac{n\pi x}{l}\Big) = \frac12\big[f(x - 0) + f(x + 0)\big] \quad \text{for every } x \in (-l, l). \]

What happens outside \((-l, l)\)? The series is periodic with period \(2l\), so outside the interval it repeats the function: it represents the periodic extension of \(f\). In particular, at the endpoints \(x = \pm l\) it converges to \( \frac12\big[f(-l + 0) + f(l - 0)\big] \), because the end of one period meets the start of the next there.

Why \(\frac{a_0}{2}\) and not \(a_0\)? Writing half of \(a_0\) lets one formula (2) serve for all \(n\), including \(n = 0\). Note that \( \frac{a_0}{2} = \frac{1}{2l}\int_{-l}^{l} f\,dx \) is simply the average value of \(f\) on the interval, the "constant offset" of the wave.

4. Where the coefficient formulas come from

The formulas (2) follow from one beautiful property of sines and cosines, called orthogonality. For natural numbers \(m, n\):

\[ \int_{-l}^{l}\cos\frac{m\pi x}{l}\cos\frac{n\pi x}{l}\,dx = \int_{-l}^{l}\sin\frac{m\pi x}{l}\sin\frac{n\pi x}{l}\,dx = \begin{cases} 0, & m \ne n,\\ l, & m = n, \end{cases} \qquad \int_{-l}^{l}\cos\frac{m\pi x}{l}\sin\frac{n\pi x}{l}\,dx = 0. \]

(Each follows from the product-to-sum formulas: the integrand becomes a sum of cosines or sines that integrate to zero over a whole number of periods.)

Now take (1), multiply both sides by \( \cos\frac{m\pi x}{l} \), and integrate over \((-l, l)\), term by term. Every integral on the right vanishes except the one with \(n = m\), which equals \(a_m\cdot l\). So

\[ \int_{-l}^{l} f(x)\cos\frac{m\pi x}{l}\,dx = a_m\,l, \]

which is exactly the formula for \(a_m\). Multiplying by \( \sin\frac{m\pi x}{l} \) gives \(b_m\) the same way.

The intuition: the waves behave like perpendicular axes in space. To find the \(x\)-coordinate of a vector you take a dot product with \(\vec i\); to find "how much of \( \cos\frac{m\pi x}{l} \)" is in \(f\), you take the integral of \(f\) times that wave. The integral plays the role of the dot product from the post on vectors.

5. Even and odd functions

Symmetry halves the work. Recall: the integral of an odd function over \((-l, l)\) is zero, and the integral of an even function is twice the integral over \((0, l)\).

a) Even function: a cosine series If \(f(x)\) is even, then \(f(x)\sin\frac{n\pi x}{l}\) is odd, so all \(b_n = 0\), and \[ f(x) = \frac{a_0}{2} + \sum_{n=1}^\infty a_n\cos\frac{n\pi x}{l}, \qquad a_n = \frac2l\int_0^l f(x)\cos\frac{n\pi x}{l}\,dx \quad (n = 0, 1, 2, \dots). \]
b) Odd function: a sine series If \(f(x)\) is odd, then all \(a_n = 0\), and \[ f(x) = \sum_{n=1}^\infty b_n\sin\frac{n\pi x}{l}, \qquad b_n = \frac2l\int_0^l f(x)\sin\frac{n\pi x}{l}\,dx \quad (n = 1, 2, \dots). \]

This makes sense: cosines are even and sines are odd, so an even function can only be built from cosines (and a constant), and an odd function only from sines. Check the symmetry first, every time; it saves half the integrals.

6. Worked examples

In the examples we take \(l = \pi\), so the waves are simply \(\cos nx\) and \(\sin nx\).

Example 1 · The square wave Let \( f(x) = -1 \) for \(-\pi < x < 0\) and \( f(x) = 1 \) for \(0 < x < \pi\). It is odd, so \[ b_n = \frac2\pi\int_0^\pi \sin nx\,dx = \frac{2}{\pi}\cdot\frac{1 - \cos n\pi}{n} = \begin{cases} \frac{4}{\pi n}, & n \text{ odd},\\ 0, & n \text{ even}. \end{cases} \] Therefore \[ f(x) = \frac4\pi\left(\sin x + \frac{\sin 3x}{3} + \frac{\sin 5x}{5} + \cdots\right) = \frac4\pi\sum_{k=1}^\infty\frac{\sin(2k-1)x}{2k-1}. \] At \(x = 0\) every term is \(0\): the series gives \(0\), the midpoint of the jump from \(-1\) to \(1\). At \(x = \frac\pi2\) it gives \( 1 = \frac4\pi\big(1 - \frac13 + \frac15 - \cdots\big) \), i.e. Leibniz's series \( \frac\pi4 \) again.
x −π π 1 −1 1 term 2 terms 5 terms
Figure 1. The square wave (black) and the partial sums of its Fourier series with 1, 2 and 5 terms. The sums hug the flat parts better and better, pass through the midpoint \(0\) at the jump, and overshoot slightly next to it.
The Gibbs phenomenon Look at the red curve next to the jump: it overshoots the value \(1\). Adding more terms makes the overshoot narrower, but its height does not shrink: it stays at about 9% of the jump size. This is the Gibbs phenomenon. It does not contradict the theorem, because at each fixed point the series still converges; it shows that near a jump the convergence is not uniform, consistent with the post on series of functions (continuous terms cannot converge uniformly to a discontinuous sum).
Example 2 · The sawtooth \(f(x) = x\) On \((-\pi, \pi)\), \(f(x) = x\) is odd. Integrating by parts, \[ b_n = \frac2\pi\int_0^\pi x\sin nx\,dx = \frac2\pi\left[-\frac{x\cos nx}{n}\right]_0^\pi + \frac{2}{\pi n}\int_0^\pi\cos nx\,dx = \frac{2(-1)^{n+1}}{n}. \] So \[ x = 2\left(\sin x - \frac{\sin 2x}{2} + \frac{\sin 3x}{3} - \cdots\right) = 2\sum_{n=1}^\infty\frac{(-1)^{n+1}\sin nx}{n} \qquad (-\pi < x < \pi). \] At \(x = \pm\pi\) the series gives \(0\): the periodic extension jumps from \(\pi\) to \(-\pi\) there, and \(0\) is the midpoint.
Example 3 · \(f(x) = x^2\) and the Basel problem On \((-\pi, \pi)\), \(x^2\) is even. We get \( a_0 = \frac2\pi\int_0^\pi x^2\,dx = \frac{2\pi^2}{3} \), and integrating by parts twice, \( a_n = \frac{4(-1)^n}{n^2} \). Hence \[ x^2 = \frac{\pi^2}{3} + 4\sum_{n=1}^\infty\frac{(-1)^n\cos nx}{n^2} \qquad (-\pi \le x \le \pi). \] (The periodic extension is continuous, so the formula also holds at \(\pm\pi\).) Put \(x = \pi\), where \(\cos n\pi = (-1)^n\): \[ \pi^2 = \frac{\pi^2}{3} + 4\sum_{n=1}^\infty\frac{1}{n^2} \quad\Longrightarrow\quad \sum_{n=1}^\infty\frac{1}{n^2} = \frac{\pi^2}{6}. \] This is the famous Basel problem, solved by Euler in 1734. In the first post of this series we only knew that this series converges; now we know its sum.
Example 4 · \(f(x) = |x|\) Even, with \( a_0 = \pi \) and \( a_n = \frac2\pi\int_0^\pi x\cos nx\,dx = \frac{2\big((-1)^n - 1\big)}{\pi n^2} \), which is \(-\frac{4}{\pi n^2}\) for odd \(n\) and \(0\) for even \(n\): \[ |x| = \frac\pi2 - \frac4\pi\left(\cos x + \frac{\cos 3x}{9} + \frac{\cos 5x}{25} + \cdots\right). \] At \(x = 0\): \( \sum_{k\ge1}\frac{1}{(2k-1)^2} = \frac{\pi^2}{8} \). Notice that the coefficients here decay like \(\frac1{n^2}\), faster than the \(\frac1n\) of the square wave and the sawtooth: the smoother the function, the faster its Fourier coefficients decay. (\(|x|\) has a corner but no jump.)

7. Functions given on half an interval

In applications (a vibrating string, heat in a rod), the function is often given only on \((0, l)\). We may then extend it to \((-l, 0)\) however we like, and the most convenient choices are:

Both series represent the same \(f\) on \((0, l)\); they differ only on \((-l, 0)\) and at the endpoints. This mirrors exactly what we did with the Fourier integral on a half-line in an earlier post.

Example 5 · Two series for \(f(x) = x\) on \((0, \pi)\)
  • Odd extension gives the sawtooth of Example 2: \( x = 2\sum\frac{(-1)^{n+1}\sin nx}{n} \).
  • Even extension gives \(|x|\) from Example 4: \( x = \frac\pi2 - \frac4\pi\sum_k\frac{\cos(2k-1)x}{(2k-1)^2} \).
On \((0, \pi)\) both equal \(x\). The cosine version converges much faster, because the even extension has no jump.

8. Term-by-term integration

Theorem · Integration of a Fourier series If \(f(x)\) is integrable on \((-l, l)\), its Fourier series (1), which may even be divergent, can be integrated term by term on that interval: for any \(a, x\) in \([-l, l]\), \[ \int_a^x f(t)\,dt = \int_a^x\frac{a_0}{2}\,dt + \sum_{n=1}^\infty\int_a^x\Big(a_n\cos\frac{n\pi t}{l} + b_n\sin\frac{n\pi t}{l}\Big)dt, \] and the integrated series converges.

This is remarkable. In the post on series of functions, integrating term by term required uniform convergence. Fourier series are much better behaved: integration only makes the coefficients smaller (it divides them by \(n\)), so the integrated series converges whatever the original series does. Differentiation is the dangerous operation: it multiplies the coefficients by \(n\), and in general may not be done term by term.

Example 6 · From the sawtooth to \(x^2\) Integrate the series of Example 2 from \(0\) to \(x\): \[ \frac{x^2}{2} = 2\sum_{n=1}^\infty\frac{(-1)^{n+1}}{n}\int_0^x\sin nt\,dt = 2\sum_{n=1}^\infty\frac{(-1)^{n+1}(1 - \cos nx)}{n^2}. \] With \( \sum\frac{(-1)^{n+1}}{n^2} = \frac{\pi^2}{12} \) (from Example 3 at \(x = 0\)), this rearranges to \[ x^2 = \frac{\pi^2}{3} + 4\sum_{n=1}^\infty\frac{(-1)^n\cos nx}{n^2}, \] exactly Example 3. Integration turned one Fourier series into another, with no new integrals to compute.

9. Summary

CaseSeriesCoefficients
General, on \((-l, l)\)\( \frac{a_0}{2} + \sum\big(a_n\cos\frac{n\pi x}{l} + b_n\sin\frac{n\pi x}{l}\big) \)\( a_n = \frac1l\int_{-l}^l f\cos\frac{n\pi x}{l}dx,\ \ b_n = \frac1l\int_{-l}^l f\sin\frac{n\pi x}{l}dx \)
Even \(f\)\( \frac{a_0}{2} + \sum a_n\cos\frac{n\pi x}{l} \)\( a_n = \frac2l\int_0^l f\cos\frac{n\pi x}{l}dx \)
Odd \(f\)\( \sum b_n\sin\frac{n\pi x}{l} \)\( b_n = \frac2l\int_0^l f\sin\frac{n\pi x}{l}dx \)
\(f\) on \((0, l)\)cosine series (even ext.) or sine series (odd ext.)formulas for even / odd \(f\)

Convergence: under the Dirichlet conditions, the series converges at every point to \( \frac12[f(x - 0) + f(x + 0)] \): to \(f(x)\) where \(f\) is continuous, to the midpoint at jumps, and to the periodic extension outside the interval. Integration term by term is always allowed for integrable \(f\).

Standard series (\(l = \pi\))Famous value obtained
square wave \(= \frac4\pi\sum\frac{\sin(2k-1)x}{2k-1}\)\( 1 - \frac13 + \frac15 - \cdots = \frac\pi4 \)
\( x^2 = \frac{\pi^2}{3} + 4\sum\frac{(-1)^n\cos nx}{n^2} \)\( \sum\frac{1}{n^2} = \frac{\pi^2}{6} \), \( \sum\frac{(-1)^{n+1}}{n^2} = \frac{\pi^2}{12} \)
\( |x| = \frac\pi2 - \frac4\pi\sum\frac{\cos(2k-1)x}{(2k-1)^2} \)\( \sum\frac{1}{(2k-1)^2} = \frac{\pi^2}{8} \)

10. Practice problems

Try each one before opening the solution. More solved problems from this topic are in Series.

Problem 1: Expand \( f(x) = x \) on \((-1, 1)\) in a Fourier series.

Here \(l = 1\) and \(f\) is odd: \( b_n = 2\int_0^1 x\sin n\pi x\,dx = \frac{2(-1)^{n+1}}{n\pi} \). So \( x = \frac2\pi\sum_{n=1}^\infty\frac{(-1)^{n+1}\sin n\pi x}{n} \) for \(-1 < x < 1\).

Problem 2: Expand \( f(x) = 0 \) for \(-\pi < x < 0\), \( f(x) = 1 \) for \(0 < x < \pi\).

\( a_0 = \frac1\pi\int_0^\pi dx = 1 \), \( a_n = \frac1\pi\int_0^\pi\cos nx\,dx = 0 \), \( b_n = \frac1\pi\int_0^\pi\sin nx\,dx = \frac{1 - (-1)^n}{\pi n} \). So \( f(x) = \frac12 + \frac2\pi\sum_{k=1}^\infty\frac{\sin(2k-1)x}{2k-1} \). (It is the square wave of Example 1, halved and shifted up by \(\frac12\).)

Problem 3: To which value does the Fourier series of \(f(x) = x\) on \((-\pi, \pi)\) converge at \(x = \pi\)? And that of \(x^2\)?

For \(x\): to \( \frac12[f(-\pi + 0) + f(\pi - 0)] = \frac12(-\pi + \pi) = 0 \). For \(x^2\): the periodic extension is continuous at \(\pi\), so to \(\pi^2\).

Problem 4: Using Example 3 at \(x = 0\), find \( \displaystyle\sum_{n=1}^\infty\frac{(-1)^{n+1}}{n^2} \).

\( 0 = \frac{\pi^2}{3} + 4\sum\frac{(-1)^n}{n^2} \Rightarrow \sum\frac{(-1)^{n+1}}{n^2} = \frac{\pi^2}{12} \).

Problem 5: Find the sine series of \( f(x) = 1 \) on \((0, \pi)\).

Odd extension gives the square wave: \( 1 = \frac4\pi\sum_{k=1}^\infty\frac{\sin(2k-1)x}{2k-1} \) for \(0 < x < \pi\). (The cosine series of \(1\) is just \(1\) itself.)

Problem 6: Expand \( f(x) = \pi - x \) on \((0, 2\pi)\) by shifting Example 2.

Put \(x = \pi - t\) in Example 2 (\(t \in (0, 2\pi)\) means \(x \in (-\pi, \pi)\)): \( \pi - t = 2\sum\frac{(-1)^{n+1}\sin n(\pi - t)}{n} \). Since \( \sin n(\pi - t) = (-1)^{n+1}\sin nt \), this becomes \( \pi - t = 2\sum_{n=1}^\infty\frac{\sin nt}{n} \), i.e. \( \sum\frac{\sin nt}{n} = \frac{\pi - t}{2} \) on \((0, 2\pi)\), the formula quoted in the post on series of functions.

Problem 7: Does \( f(x) = \sin\frac1x \) (with \(f(0) = 0\)) satisfy the Dirichlet conditions on \((-1, 1)\)?

No. It is bounded and has only one point of discontinuity, but near \(x = 0\) it has infinitely many maxima and minima, violating condition 3.

Final thoughts

Fourier series split a function into its "notes": a constant average plus waves of frequencies \(1, 2, 3, \dots\). Two habits make the computations easy: check symmetry first (even means cosines only, odd means sines only), and remember the midpoint rule at jumps. Beyond computation, Fourier series are one of the great ideas of mathematics: they are how we solve the heat and wave equations, how audio is compressed, and, as Examples 3 and 4 show, a surprisingly powerful way to sum numerical series.

Next in the series: infinite products. See also the Fourier integral, which extends these ideas to non-periodic functions, and series of functions, which explains the convergence behaviour seen here.