Flow Lab

Syllabus / Module 8: Boundary Value Problems & Fourier Series

Lesson 8.3

Fourier Series

Representing periodic functions as sums of sines and cosines; Euler–Fourier coefficient formulas from orthogonality; complex form.

Written byClaude Sonnet

🌊
Why this matters for simulation: Turbulence is described by its energy spectrum — Fourier analysis of velocity fluctuations.

By the end of this lesson you can…

  • Prove the orthogonality relations for $\sin kx$ and $\cos kx$ on a full period and use them to derive the Euler–Fourier coefficient formulas.
  • Compute the Fourier series of the square wave, the sawtooth and the triangle wave by hand, and state how fast each spectrum decays.
  • Rescale the formulas to a period $2L$ and convert between the real form $a_k, b_k$ and the complex form $c_k$.
  • Use Parseval's identity to compute the energy captured by $N$ terms and to sum series such as $\sum 1/k^4$.
  • Implement coefficient quadrature and the partial sum $S_N$ numerically and check them against closed forms.
On this page
·
  1. Why sines and cosines, and what “periodic” means
  2. Orthogonality on a full period
  3. The Euler–Fourier formulas
  4. Worked example 1: the square wave
  5. Worked example 2: the sawtooth and the triangle
  6. Reading the partial sums
  7. Smoothness decides how fast the coefficients fall
  8. The complex form
  9. Parseval: energy is conserved in the transform
  10. Other periods
  11. Computing it
  12. The catch: we are not done with the heat equation
  13. Concept checks
  14. Problems
  15. Key Takeaways

8.2 Eigenvalue Problems & Sturm–Liouville Theory ended with a promise. Every Sturm–Liouville problem hands you eigenfunctions yky_k that are mutually perpendicular, and the heat equation needs the initial temperature f(x)f(x) written as ∑ckyk(x)\sum c_k y_k(x). Perpendicular directions mean the coefficients are found by projecting, exactly as in 1.1 Vectors, Linear Combinations & Span: ck=(f,yk)/(yk,yk)c_k = (f, y_k)/(y_k, y_k). This lesson does the projection for the most important family of all, the sines and cosines. The expansion is a Fourier series. The same idea sits behind the FFT in a fluid solver and the way we read a turbulence spectrum.

▶ Video Building a square wave from sines one at a time, with the coefficients appearing as projectionscaptions only
Transcript
  1. A function has period two pi if it repeats every two pi. Give it on one period and repeat it forever: the periodic extension. Why sines and cosines? Take minus y double prime equals lambda y with periodic conditions: same value and same slope at both ends. The boundary bracket from the last lesson cancels, so the operator is symmetric. The eigenvalues are k squared, with two eigenfunctions each, cosine k x and sine k x, plus the constant for k equal 0.
  2. Now the products. The integral of cosine j x times cosine k x over a full period is zero when j and k differ, and pi when they are equal. Sines behave the same. The mixed integral of sine times cosine is zero for every j and k, because the integrand is odd. The picture shows it: sine x times sine 2 x has equal positive and negative areas. That pi is the squared length of each basis function.
  3. Suppose f is a constant over two, plus a sum of cosines and sines. Multiply by cosine j x and integrate over a period, term by term. Orthogonality kills every term except k equal j, leaving a j times pi. So a k is one over pi times the integral of f cosine k x, and b k the same with sine. The constant a 0 over 2 is the average of f. It is a vector projection: dot product over squared length. Nothing needs f smooth. Jumps are allowed.
  4. Take the square wave: minus 1 on the left half, plus 1 on the right. It is odd, so every cosine coefficient is zero. For the sines, b k equals 4 over k pi for odd k, and zero for even k. So the square wave is 4 over pi times sine x plus sine 3 x over 3, plus sine 5 x over 5, and so on. The sim finds the coefficients by quadrature, never seeing this formula: 1.273240, 0.424413 and 0.254648.
  5. Now add the terms. One term overshoots the target 1: its maximum is 1.2732. Three terms: 1.2004. Five terms: 1.1884. The corners sharpen, yet the maximum does not return to 1. At 39 terms the sim still reports a maximum near 1.179. That surviving bump of about 18 percent is the Gibbs phenomenon: next to a jump the overshoot gets narrower, but not shorter. Exactly at the jump every sine is zero, so the series lands on the midpoint.
  6. The sawtooth f equal x is odd. Integrating by parts, b k is 2 times minus 1 to the k plus 1, over k. The triangle, absolute value of x, is even, with a k equal minus 4 over pi k squared for odd k. The difference matters. The sawtooth has a jump, and its coefficients fall like 1 over k. The triangle has only a corner, and they fall like 1 over k squared. Differentiate the triangle series term by term and you get the square wave.
  7. Smoothness sets the decay, and integration by parts shows it. A jump in f leaves a boundary term of size 1 over k. A jump in the slope gives 1 over k squared. A smooth periodic function falls faster than any power. The root mean square error agrees. After 39 terms the square wave still has 10 percent error, the triangle 0.15 percent, and exp of cosine x nothing visible. All converge in the mean-square sense, at very different speeds.
  8. Write cosine and sine as complex exponentials and the series becomes a sum of c k times e to the i k x. For real f, c k is a k minus i b k, over 2. The gain is the shift theorem: slide the function by s and c k is multiplied by e to the minus i k s. Only the phase turns. For the square wave, a 1 and b 1 are 0 and 1.2732, then minus 1.071 and 0.688 at s equal 1, then minus 1.158 and minus 0.530 at s equal 2. The length of c 1 stays 0.6366. These magnitudes are the spectrum.
  9. Square the series and orthogonality kills every cross term. The mean of f squared equals the constant squared plus one half the sum of a k squared and b k squared: Pythagoras in infinitely many dimensions. So the energy missed by the partial sum equals the mean square of its error. For the square wave, one term holds 81.06 percent of the energy, three terms 90.06, five terms 93.31, and 39 terms 98.99. The direct error at 3 terms is 0.31523, and so is the square root of 1 minus 0.90063. Parseval also sums series: pi squared over 8.
  10. One catch remains. The heat problem gives f only on 0 to L, with the ends held at zero, so the eigenfunctions are sines alone. A full-period series needs f on the negative half too. Which extension? Take f equal x. The odd extension is the sawtooth, with a jump at the wall. The even extension is the triangle, with only a corner. After 39 terms the sawtooth's error is 0.225, against 0.00145 for the triangle: about 150 times worse. The extension must respect the boundary condition. That is the next lesson.

Why sines and cosines, and what “periodic” means

A function has period 2π2\pi if f(x+2π)=f(x)f(x + 2\pi) = f(x) for every xx. Give ff only on (−π,π](-\pi, \pi] and repeat it forever to the left and right: that is its periodic extension. Everything below lives on that circle of length 2π2\pi (period 2L2L comes later by rescaling).

Why these functions? Consider the eigenvalue problem −y′′=λy-y'' = \lambda y with the periodic conditions y(−π)=y(π)y(-\pi) = y(\pi) and y′(−π)=y′(π)y'(-\pi) = y'(\pi). The boundary term from the previous lesson, [−u′v+uv′]−ππ[-u'v + uv']_{-\pi}^{\pi}, vanishes for any two functions obeying these conditions, because each term at π\pi equals the term at −π-\pi. So the operator is symmetric and the orthogonality argument applies. The solutions are λk=k2\lambda_k = k^2 with two eigenfunctions, cos⁡kx\cos kx and sin⁡kx\sin kx, for each k≥1k \ge 1, and λ0=0\lambda_0 = 0 with the constant. (Last lesson’s “eigenvalues are simple” assumed separated end conditions, which periodic ones are not; the multiplicity two is the cost of that.) So we are expanding in an orthogonal basis exactly as promised. The only new work is computing the inner products.

Orthogonality on a full period

For integers j,k≥1j, k \ge 1 use the product identities cos⁡Acos⁡B=12[cos⁡(A−B)+cos⁡(A+B)]\cos A\cos B = \tfrac12[\cos(A - B) + \cos(A + B)], and the analogous ones for sin⁡sin⁡\sin\sin and sin⁡cos⁡\sin\cos. The integral of cos⁡(mx)\cos(mx) over (−π,π)(-\pi, \pi) is 00 for an integer m≠0m \ne 0 and 2π2\pi for m=0m = 0, which gives

∫−ππcos⁡jx cos⁡kx dx={0j≠kπj=k≥1,∫−ππsin⁡jx sin⁡kx dx={0j≠kπj=k≥1,\int_{-\pi}^{\pi}\cos jx\,\cos kx\,dx = \begin{cases} 0 & j \ne k\\ \pi & j = k \ge 1\end{cases}, \qquad \int_{-\pi}^{\pi}\sin jx\,\sin kx\,dx = \begin{cases} 0 & j \ne k\\ \pi & j = k \ge 1\end{cases}, ∫−ππsin⁡jx cos⁡kx dx=0 for all j,k,∫−ππ1 dx=2π,∫−ππcos⁡kx dx=∫−ππsin⁡kx dx=0.\int_{-\pi}^{\pi}\sin jx\,\cos kx\,dx = 0 \text{ for all } j, k, \qquad \int_{-\pi}^{\pi} 1\,dx = 2\pi, \qquad \int_{-\pi}^{\pi}\cos kx\,dx = \int_{-\pi}^{\pi}\sin kx\,dx = 0 .

The mixed integral vanishes by symmetry alone: sin⁡jxcos⁡kx\sin jx\cos kx is odd, and an odd function integrates to zero over an interval symmetric about 00. The squares carry the number π\pi because cos⁡2=12(1+cos⁡2kx)\cos^2 = \tfrac12(1 + \cos 2kx) averages to one half over a length 2π2\pi. That π\pi is the “length squared” of the basis vectors and appears in every coefficient formula.

The Euler–Fourier formulas

Suppose ff can be written

f(x)=a02+∑k=1∞(akcos⁡kx+bksin⁡kx).f(x) = \frac{a_0}{2} + \sum_{k=1}^{\infty}\bigl(a_k\cos kx + b_k\sin kx\bigr).

Multiply by cos⁡jx\cos jx and integrate over a period, term by term (the exchange of sum and integral is the step we are assuming; the next lessons discuss when it is legal). Orthogonality kills every term except the one with k=jk = j:

∫−ππf(x)cos⁡jx dx=aj π⟹ak=1π∫−ππf(x)cos⁡kx dx,bk=1π∫−ππf(x)sin⁡kx dx.\int_{-\pi}^{\pi} f(x)\cos jx\,dx = a_j\,\pi \quad\Longrightarrow\quad a_k = \frac1\pi\int_{-\pi}^{\pi} f(x)\cos kx\,dx,\qquad b_k = \frac1\pi\int_{-\pi}^{\pi} f(x)\sin kx\,dx .

The sine formula is the same calculation with sin⁡jx\sin jx. The constant term is written a0/2a_0/2 so that the same formula serves k=0k = 0: with j=0j = 0 the left side is ∫f=a02⋅2π\int f = \tfrac{a_0}{2}\cdot 2\pi, so a0/2a_0/2 is simply the average of ff over a period. Compare with a vector: the component along a unit direction is the dot product; here cos⁡kx\cos kx has squared length π\pi, so we divide by it. The series is the projection of ff onto each direction, added back together.

Nothing in the derivation required ff to be smooth. It needed only that the integrals exist, which holds for any piecewise continuous ff, jumps included. That is the main advantage over a Taylor series.

Worked example 1: the square wave

Let f=−1f = -1 on (−π,0)(-\pi, 0) and f=+1f = +1 on (0,π)(0, \pi). This ff is odd, so fcos⁡kxf\cos kx is odd and every ak=0a_k = 0. For the sine coefficients, fsin⁡kxf\sin kx is even, so we double the right half:

bk=2π∫0πsin⁡kx dx=2π[−cos⁡kxk]0π=2kπ(1−(−1)k)={4kπk odd0k evenb_k = \frac{2}{\pi}\int_0^\pi \sin kx\,dx = \frac{2}{\pi}\Bigl[-\frac{\cos kx}{k}\Bigr]_0^\pi = \frac{2}{k\pi}\bigl(1 - (-1)^k\bigr) = \begin{cases}\dfrac{4}{k\pi} & k\text{ odd}\\[2mm] 0 & k\text{ even}\end{cases}

so

f(x)=4π(sin⁡x+sin⁡3x3+sin⁡5x5+⋯ ).f(x) = \frac4\pi\Bigl(\sin x + \frac{\sin 3x}{3} + \frac{\sin 5x}{5} + \cdots\Bigr).

The sim, which computes the coefficients by quadrature on 16000 cells and never looks at this formula, gives b1=1.273240b_1 = 1.273240, b3=0.424413b_3 = 0.424413, b5=0.254648b_5 = 0.254648, with all even bkb_k and all aka_k zero to six digits. These agree with 4/π=1.2732404/\pi = 1.273240, 4/3π=0.4244134/3\pi = 0.424413 and 4/5π=0.2546484/5\pi = 0.254648.

Worked example 2: the sawtooth and the triangle

For f(x)=xf(x) = x on (−π,π)(-\pi, \pi), again odd, so ak=0a_k = 0. Integrate by parts, ∫xsin⁡kx dx=−xcos⁡kxk+sin⁡kxk2\int x\sin kx\,dx = -\frac{x\cos kx}{k} + \frac{\sin kx}{k^2}:

bk=2π∫0πxsin⁡kx dx=2π(−πcos⁡kπk)=2(−1)k+1k.b_k = \frac{2}{\pi}\int_0^\pi x\sin kx\,dx = \frac2\pi\Bigl(-\frac{\pi\cos k\pi}{k}\Bigr) = \frac{2(-1)^{k+1}}{k}.

The coefficients are 2,−1,23,−12,25,…2, -1, \tfrac23, -\tfrac12, \tfrac25, \dots, and the sim returns 2.000000,−1.000000,0.666667,−0.500000,0.4000002.000000, -1.000000, 0.666667, -0.500000, 0.400000.

For f(x)=∣x∣f(x) = |x| the function is even, so bk=0b_k = 0 and a0=2π∫0πx dx=πa_0 = \frac{2}{\pi}\int_0^\pi x\,dx = \pi. For k≥1k \ge 1, ∫xcos⁡kx dx=xsin⁡kxk+cos⁡kxk2\int x\cos kx\,dx = \frac{x\sin kx}{k} + \frac{\cos kx}{k^2}, so

ak=2π[cos⁡kπ−1k2]={−4πk2k odd0k even⟹∣x∣=π2−4π(cos⁡x+cos⁡3x9+cos⁡5x25+⋯ ).a_k = \frac2\pi\Bigl[\frac{\cos k\pi - 1}{k^2}\Bigr] = \begin{cases}-\dfrac{4}{\pi k^2} & k\text{ odd}\\[2mm] 0 & k\text{ even}\end{cases} \qquad\Longrightarrow\qquad |x| = \frac\pi2 - \frac4\pi\Bigl(\cos x + \frac{\cos 3x}{9} + \frac{\cos 5x}{25} + \cdots\Bigr).

The sim gives a0=3.141593a_0 = 3.141593 and a1,a3,a5=−1.273240, −0.141471, −0.050930a_1, a_3, a_5 = -1.273240,\ -0.141471,\ -0.050930, matching −4/π-4/\pi, −4/9π-4/9\pi, −4/25π-4/25\pi.

◆ Interactive Pick a function and the number of terms N; watch the partial sum, the coefficients, the energy captured and the error

Loading simulation…

What to try. (1) Square wave, N=1,3,7,15,39N = 1, 3, 7, 15, 39. (2) Switch to the triangle at the same NN and compare the error panels. (3) Select exp⁡(cos⁡x)\exp(\cos x) and watch the error plunge. (4) Slide the shift ss on the square wave: the bars ak,bka_k, b_k change, the ticks ∣ck∣|c_k| do not. (5) Read the energy percentage against the readout of the direct error.

Reading the partial sums

Write SN(x)=a02+∑k=1N(akcos⁡kx+bksin⁡kx)S_N(x) = \frac{a_0}{2} + \sum_{k=1}^N(a_k\cos kx + b_k\sin kx). For the square wave S1=4πsin⁡xS_1 = \frac4\pi\sin x, a smooth bump that overshoots the target value 11 at its peak: the sim gives max⁡S1=1.2732\max S_1 = 1.2732. Adding k=3k = 3 puts in a dip and lifts the plateau: max⁡S3=1.2004\max S_3 = 1.2004 and max⁡S5=1.1884\max S_5 = 1.1884. The corners keep sharpening, yet the maximum does not return to 11. At N=39N = 39 the sim still reports a maximum near 1.1791.179. That surviving 18% bump is the Gibbs phenomenon: the overshoot next to a jump shrinks in width but not in height. 8.5 Convergence & the Gibbs Phenomenon explains its size and why it is no contradiction; the next section measures what the series does get right.

At the jump itself every term sin⁡kx\sin kx vanishes at x=0x = 0, so SN(0)=0S_N(0) = 0 for all NN: the series assigns the midpoint of the jump, 12(−1+1)\tfrac12(-1 + 1). This holds at any jump (from memory).

Smoothness decides how fast the coefficients fall

The three waves above have coefficients that fall like 1/k1/k (square, sawtooth: a jump in ff) and 1/k21/k^2 (triangle: a corner, a jump in f′f'). The reason is integration by parts. For a function with a jump of size JJ at some point, bk=1π∫fsin⁡kxb_k = \frac1\pi\int f\sin kx leaves a boundary term of size J/(kπ)J/(k\pi) that does not shrink faster; if ff is continuous but f′f' jumps, the first boundary term cancels and the next is of size 1/k21/k^2; and so on. For a smooth periodic function (all derivatives continuous and periodic) every integration by parts gains another 1/k1/k, so the coefficients fall faster than any power (from memory: this is the standard smoothness-decay theorem; the sim shows examples).

A second view of the same fact: differentiate the triangle’s series term by term. ddx∣x∣=sign⁡(x)\frac{d}{dx}|x| = \operatorname{sign}(x), and

ddx[−4π∑k oddcos⁡kxk2]=4π∑k oddsin⁡kxk,\frac{d}{dx}\Bigl[-\frac4\pi\sum_{k\text{ odd}}\frac{\cos kx}{k^2}\Bigr] = \frac4\pi\sum_{k\text{ odd}}\frac{\sin kx}{k},

the square wave series exactly. Differentiation multiplies the kk-th coefficient by kk, and the 1/k21/k^2 became 1/k1/k. This is why spectral methods differentiate in Fourier space, and why derivatives of rough data amplify noise.

The sim puts numbers on it. The sizes of the root-mean-square error of SNS_N (the square root of the average of (f−SN)2(f - S_N)^2 over a period), measured directly on a 4000-point grid, are:

NNsquare wavetriangleexp⁡(cos⁡x)\exp(\cos x)
110.43520.43520.10910.10910.19460.1946
330.31520.31520.04350.04350.00390.0039
550.25870.25870.02440.02440.000030.00003
39390.1010.1010.001450.00145below 10−510^{-5}

Forty terms leave the square wave with 10% error, the triangle with 0.15%, and exp⁡(cos⁡x)\exp(\cos x) with nothing visible. All three converge in this root-mean-square sense; they differ enormously in speed.

The complex form

Write cos⁡kx=12(eikx+e−ikx)\cos kx = \tfrac12(e^{ikx} + e^{-ikx}) and sin⁡kx=12i(eikx−e−ikx)\sin kx = \tfrac1{2i}(e^{ikx} - e^{-ikx}). The series becomes

f(x)=∑k=−∞∞ck eikx,ck=12π∫−ππf(x) e−ikx dx,c0=a02,ck=ak−i bk2, c−k=ck‾ (k>0).f(x) = \sum_{k=-\infty}^{\infty} c_k\,e^{ikx},\qquad c_k = \frac1{2\pi}\int_{-\pi}^{\pi}f(x)\,e^{-ikx}\,dx,\qquad c_0 = \frac{a_0}{2},\quad c_k = \frac{a_k - i\,b_k}{2},\ c_{-k} = \overline{c_k}\ (k \gt 0).

The orthogonality is the single statement ∫−ππeijxeikx‾ dx=2πδjk\int_{-\pi}^{\pi}e^{ijx}\overline{e^{ikx}}\,dx = 2\pi\delta_{jk} (the bar means complex conjugate; one integral instead of six). The derivation of the formula for ckc_k is the same projection, and the relation to ak,bka_k, b_k follows by substituting e−ikx=cos⁡kx−isin⁡kxe^{-ikx} = \cos kx - i\sin kx. One advantage is the shift theorem: moving the function, g(x)=f(x−s)g(x) = f(x - s), gives

ck(g)=12π∫f(x−s)e−ikxdx=e−iks ck(f),c_k(g) = \frac1{2\pi}\int f(x - s)e^{-ikx}dx = e^{-iks}\,c_k(f),

so only the phase of ckc_k changes and the magnitude ∣ck∣|c_k| does not. The sim confirms it for the square wave: ∣c1∣=0.6366|c_1| = 0.6366 for every shift, while (a1,b1)=(0,1.2732)(a_1, b_1) = (0, 1.2732) at s=0s = 0 turns into (−1.071,0.688)(-1.071, 0.688) at s=1s = 1, and (−1.158,−0.530)(-1.158, -0.530) at s=2s = 2. These match a1=−b1sin⁡sa_1 = -b_1\sin s and b1cos⁡sb_1\cos s, from sin⁡(x−s)=sin⁡xcos⁡s−cos⁡xsin⁡s\sin(x - s) = \sin x\cos s - \cos x\sin s.

For a real ff the information in ckc_k is the pair (amplitude, phase) of the kk-th wave. The collection ∣ck∣|c_k| is the spectrum of ff: how much of each wavenumber is present, whatever the position.

Parseval: energy is conserved in the transform

The mean of f2f^2 over a period is the energy. Squaring the series and using orthogonality kills every cross term:

12π∫−ππf2 dx=(a02)2+12∑k=1∞(ak2+bk2)=∑k=−∞∞∣ck∣2.\frac{1}{2\pi}\int_{-\pi}^{\pi} f^2\,dx = \Bigl(\frac{a_0}{2}\Bigr)^2 + \frac12\sum_{k=1}^\infty\bigl(a_k^2 + b_k^2\bigr) = \sum_{k=-\infty}^{\infty}|c_k|^2 .

This is Pythagoras in infinitely many dimensions: the squared length of the vector equals the sum of the squares of its components. It also tells us exactly what NN terms cost. The energy missed by SNS_N equals the tail of the sum, and equals the mean square of the error f−SNf - S_N. The sim checks this two ways. For the square wave the mean square is 1.0000001.000000, and the energy in NN terms is 12∑bk2=8π2(1+19+… )\tfrac{1}{2}\sum b_k^2 = \tfrac{8}{\pi^2}\bigl(1 + \tfrac19 + \dots\bigr): 81.06%81.06\% for N=1N = 1, 90.06%90.06\% for N=3N = 3, 93.31%93.31\% for N=5N = 5 and 98.99%98.99\% for N=39N = 39. Its direct rms error at N=3N = 3 is 0.315230.31523, and 1−0.90063=0.31523\sqrt{1 - 0.90063} = 0.31523 (the readout prints both). For the triangle N=1N = 1 already captures 99.64%99.64\% of the energy.

Parseval also sums series. For the square wave, 1=8π2∑k oddk−21 = \frac{8}{\pi^2}\sum_{k\text{ odd}}k^{-2} gives ∑k odd1/k2=π2/8\sum_{k\text{ odd}} 1/k^2 = \pi^2/8. For the triangle the mean square is π2/3\pi^2/3 and (a0/2)2=π2/4(a_0/2)^2 = \pi^2/4, so 12⋅16π2∑k oddk−4=π212\frac{1}{2}\cdot\frac{16}{\pi^2}\sum_{k\text{ odd}}k^{-4} = \frac{\pi^2}{12}, i.e. ∑k odd1/k4=π4/96\sum_{k\text{ odd}} 1/k^4 = \pi^4/96.

Other periods

If ff has period 2L2L, substitute x=πt/Lx = \pi t/L so that t∈(−π,π)t \in (-\pi, \pi). The basis becomes cos⁡(kπx/L)\cos(k\pi x/L) and sin⁡(kπx/L)\sin(k\pi x/L) and the formulas read

ak=1L∫−LLf(x)cos⁡kπxL dx,bk=1L∫−LLf(x)sin⁡kπxL dx,f=a02+∑k≥1(akcos⁡kπxL+bksin⁡kπxL).a_k = \frac1L\int_{-L}^{L} f(x)\cos\frac{k\pi x}{L}\,dx,\qquad b_k = \frac1L\int_{-L}^{L} f(x)\sin\frac{k\pi x}{L}\,dx,\qquad f = \frac{a_0}{2} + \sum_{k\ge1}\Bigl(a_k\cos\frac{k\pi x}{L} + b_k\sin\frac{k\pi x}{L}\Bigr).

On [0,L][0, L] the sines here are the Dirichlet eigenfunctions of the previous lesson and the cosines the Neumann ones.

Computing it

🐍 PythonLab: coefficients by quadrature, energy captured and the overshoot
⌘/Ctrl + Enter

Now build the two pieces yourself: the projection and the sum.

🧪 Coding exerciseExercise: Fourier coefficients by the midpoint rule, and the partial sum
⌘/Ctrl + Enter

The catch: we are not done with the heat equation

The heat problem of the last lesson gives f(x)f(x) only on [0,L][0, L], with u=0u = 0 at both ends, so the eigenfunctions are the sines sin⁡(kπx/L)\sin(k\pi x/L) alone. A full-period series on [−L,L][-L, L] needs ff on the negative half too. Which extension should we use? Take f(x)=xf(x) = x on [0,π][0, \pi]. Extending it as an odd function gives the sawtooth, whose series is all sines with coefficients 2(−1)k+1/k2(-1)^{k+1}/k. Extending it as an even function gives the triangle, whose series is all cosines with coefficients −4/(πk2)-4/(\pi k^2) (odd kk). Both series equal xx on (0,π)(0, \pi), but at N=39N = 39 the sawtooth’s rms error is 0.2250.225 against 0.001450.00145 for the triangle, about 150 times worse, because the odd extension has a jump at the wall. The jump costs a 1/k1/k tail and the Gibbs overshoot; the corner of the even extension costs only 1/k21/k^2. So the choice of extension decides convergence and must match the boundary condition. That is the subject of 8.4 Even & Odd Extensions: Sine and Cosine Series, where sine and cosine series follow from this lesson’s formulas by symmetry. What we still have not proved here is that the series converges to ff, and where; the sim shows mean-square convergence and a stubborn overshoot, which 8.5 Convergence & the Gibbs Phenomenon handles honestly.

Concept checks

✅ Concept check

Why does the formula for $a_k$ carry the prefactor $1/\pi$?

✅ Concept check

Which function has Fourier coefficients that fall like $1/k^2$?

✅ Concept check

What percentage of the square wave's energy (mean square $1$) is held by the terms $k = 1$ and $k = 3$? Use Parseval with $b_k = 4/(k\pi)$.

✅ Concept check

Shift a periodic function: $g(x) = f(x - s)$. Which quantities change?

Problems

✏️ Pen & paperRead it off●○○

Let g(x)=3+2cos⁡x−sin⁡3x+12cos⁡4xg(x) = 3 + 2\cos x - \sin 3x + \tfrac12\cos 4x. Without integrating, list a0,a1,b3,a4a_0, a_1, b_3, a_4 and the complex coefficient c3c_3. Then use Parseval to find the mean of g2g^2 over a period.

Show worked solution

The series is already in Fourier form: a0/2=3a_0/2 = 3 so a0=6a_0 = 6; a1=2a_1 = 2; b3=−1b_3 = -1; a4=12a_4 = \tfrac12. With ck=(ak−ibk)/2c_k = (a_k - ib_k)/2, c3=(0−i(−1))/2=i/2c_3 = (0 - i(-1))/2 = i/2. Parseval: (a02)2+12∑(ak2+bk2)=9+12(4+1+14)=9+2.625=11.625\bigl(\tfrac{a_0}{2}\bigr)^2 + \tfrac12\sum(a_k^2 + b_k^2) = 9 + \tfrac12\bigl(4 + 1 + \tfrac14\bigr) = 9 + 2.625 = 11.625. A numerical average over a period returns 11.62511.625.

✏️ Pen & paperFourier series of $x^2$, and two sums●●●

Find the Fourier series of f(x)=x2f(x) = x^2 on (−π,π)(-\pi, \pi). Evaluate the series at x=πx = \pi to sum ∑1/k2\sum 1/k^2, and use Parseval to sum ∑1/k4\sum 1/k^4.

Show worked solution

ff is even, so bk=0b_k = 0. a0=2π∫0πx2dx=2π23a_0 = \frac2\pi\int_0^\pi x^2dx = \frac{2\pi^2}{3}. For k≥1k \ge 1 integrate by parts twice: ∫x2cos⁡kx dx=x2sin⁡kxk+2xcos⁡kxk2−2sin⁡kxk3\int x^2\cos kx\,dx = \frac{x^2\sin kx}{k} + \frac{2x\cos kx}{k^2} - \frac{2\sin kx}{k^3}, so ak=2π⋅2πcos⁡kπk2=4(−1)kk2a_k = \frac2\pi\cdot\frac{2\pi\cos k\pi}{k^2} = \frac{4(-1)^k}{k^2}. Thus

x2=π23+4∑k≥1(−1)kcos⁡kxk2.x^2 = \frac{\pi^2}{3} + 4\sum_{k\ge1}\frac{(-1)^k\cos kx}{k^2}.

At x=πx = \pi every cos⁡kπ=(−1)k\cos k\pi = (-1)^k, so π2=π23+4∑1/k2\pi^2 = \frac{\pi^2}{3} + 4\sum 1/k^2 and ∑k≥11/k2=π2/6\sum_{k\ge1} 1/k^2 = \pi^2/6. (This uses that the series converges to ff at a point where ff is continuous and the periodic extension is continuous: from memory, justified in the next lessons.) Parseval: the mean square is 12π∫x4=π4/5\frac{1}{2\pi}\int x^4 = \pi^4/5, and (a0/2)2=π4/9(a_0/2)^2 = \pi^4/9, so 12⋅16∑k−4=π4(15−19)=4π445\tfrac12\cdot16\sum k^{-4} = \pi^4\bigl(\tfrac15 - \tfrac19\bigr) = \tfrac{4\pi^4}{45}, giving ∑k≥11/k4=π4/90\sum_{k\ge1}1/k^4 = \pi^4/90. A numerical quadrature of a1,a2,a3a_1, a_2, a_3 gives −4, 1, −0.4444-4,\ 1,\ -0.4444, matching 4(−1)k/k24(-1)^k/k^2.

✏️ Pen & paperA different period●●○

The function f(x)=xf(x) = x on (−1,1)(-1, 1) is repeated with period 22 (L=1L = 1). Find bkb_k and evaluate b1,b2,b3b_1, b_2, b_3.

Show worked solution

ff is odd, so ak=0a_k = 0 and bk=∫−11xsin⁡(kπx) dx=2∫01xsin⁡kπx dxb_k = \int_{-1}^{1} x\sin(k\pi x)\,dx = 2\int_0^1 x\sin k\pi x\,dx. With ∫xsin⁡(kπx) dx=−xcos⁡kπxkπ+sin⁡kπxk2π2\int x\sin(k\pi x)\,dx = -\frac{x\cos k\pi x}{k\pi} + \frac{\sin k\pi x}{k^2\pi^2}, this is 2⋅(−cos⁡kπkπ)=2(−1)k+1kπ2\cdot\bigl(-\frac{\cos k\pi}{k\pi}\bigr) = \frac{2(-1)^{k+1}}{k\pi}. So b1,b2,b3=0.6366, −0.3183, 0.2122b_1, b_2, b_3 = 0.6366,\ -0.3183,\ 0.2122 (numerical quadrature agrees). In general bk=2L(−1)k+1/(kπ)b_k = 2L(-1)^{k+1}/(k\pi): the sawtooth of the lesson stretched to length LL.

✏️ Pen & paperWhy the odd extension costs a jump●●○

Take f(x)=1f(x) = 1 on (0,π)(0, \pi). Extend it oddly and find the Fourier series. What do you recognise, and what would the even extension give?

Show worked solution

The odd extension is −1-1 on (−π,0)(-\pi, 0) and +1+1 on (0,π)(0, \pi): the square wave, with series 4π∑k oddsin⁡(kx)/k\frac4\pi\sum_{k\text{ odd}}\sin(kx)/k, and a jump at x=0x = 0 with 1/k1/k decay. The even extension is the constant 11 on all of (−π,π)(-\pi, \pi): its series is just a0/2=1a_0/2 = 1 with every other coefficient zero. So for the same data on [0,π][0, \pi], the cosine series is exact in one term while the sine series converges slowly.

Key Takeaways

  • Sines and cosines are orthogonal on a full period: ∫cos⁡2kx=∫sin⁡2kx=π\int\cos^2 kx = \int\sin^2 kx = \pi and every cross integral is 00. They are the eigenfunctions of −y′′=λy-y'' = \lambda y with periodic ends, with λ=k2\lambda = k^2 doubled.
  • The Euler–Fourier formulas ak=1π∫fcos⁡kxa_k = \frac1\pi\int f\cos kx, bk=1π∫fsin⁡kxb_k = \frac1\pi\int f\sin kx are projections; a0/2a_0/2 is the mean. For period 2L2L replace kxkx by kπx/Lk\pi x/L and 1/π1/\pi by 1/L1/L.
  • Square wave: bk=4/(kπ)b_k = 4/(k\pi) for odd kk (1.2732401.273240, 0.4244130.424413, 0.2546480.254648); sawtooth 2(−1)k+1/k2(-1)^{k+1}/k; triangle ak=−4/(πk2)a_k = -4/(\pi k^2) for odd kk, all checked by quadrature in the sim.
  • Smoothness sets the decay: a jump in ff gives 1/k1/k, a jump in f′f' gives 1/k21/k^2, a smooth periodic function decays faster than any power; at N=39N = 39 the rms errors are 0.1010.101, 0.001450.00145 and below 10−510^{-5}.
  • Complex form ck=(ak−ibk)/2c_k = (a_k - ib_k)/2; shifting ff rotates the phase of ckc_k but leaves ∣ck∣|c_k| alone.
  • Parseval: mean of f2f^2 =(a0/2)2+12∑(ak2+bk2)= (a_0/2)^2 + \tfrac12\sum(a_k^2 + b_k^2); the energy missed by SNS_N equals the mean square error, and the identity sums series (π2/8\pi^2/8, π4/96\pi^4/96).
  • Convergence is in the mean-square sense; next to a jump the partial sums overshoot by about 18%18\% (max 1.1791.179 at N=39N = 39). Convergence and Gibbs are the business of 8.5 Convergence & the Gibbs Phenomenon.
  • Which extension of ff from [0,L][0, L] to the full period we choose changes the convergence and must respect the boundary conditions: 8.4 Even & Odd Extensions: Sine and Cosine Series.
Finished this lesson?

Mark it complete to track your progress (saved in this browser). Reading every section does this for you.

Where this leads

These later lessons build directly on this one: