TExcelTAD
Home › Differential Equations › Sine and Cosine Series

Sine and Cosine Series

PDE problems live on a finite interval [0,L][0, L] (a rod, a string), not on a periodic line. To use Fourier series there, we extend the function to [−L,L][-L, L] either as an odd or as an even function. The odd extension gives a sine series, the even extension a cosine series, and the boundary conditions of the problem decide which one we need.

The two half-range series

For ff defined on (0,L)(0, L):

sine series: f(x)∼∑n=1∞bnsin⁡nπxL,bn=2L∫0Lf(x)sin⁡nπxL dx,\text{sine series: } f(x) \sim \sum_{n=1}^\infty b_n\sin\frac{n\pi x}{L}, \qquad b_n = \frac{2}{L}\int_0^L f(x)\sin\frac{n\pi x}{L}\,dx,
cosine series: f(x)∼a02+∑n=1∞ancos⁡nπxL,an=2L∫0Lf(x)cos⁡nπxL dx.\text{cosine series: } f(x) \sim \frac{a_0}{2} + \sum_{n=1}^\infty a_n\cos\frac{n\pi x}{L}, \qquad a_n = \frac{2}{L}\int_0^L f(x)\cos\frac{n\pi x}{L}\,dx.
Sine series Cosine series
extension odd even
value at 00 and LL 0 free
matches boundary conditions u(0)=u(L)=0u(0) = u(L) = 0 (fixed ends, fixed temperatures 0) ux(0)=ux(L)=0u_x(0) = u_x(L) = 0 (insulated ends)

Example: f(x)=x(π−x)f(x) = x(\pi - x) on (0,π)(0, \pi)

bn=2π∫0πx(π−x)sin⁡nx dx=4(1−(−1)n)πn3={8πn3n odd0n even.b_n = \frac{2}{\pi}\int_0^\pi x(\pi - x)\sin nx\,dx = \frac{4\big(1 - (-1)^n\big)}{\pi n^3} = \begin{cases} \frac{8}{\pi n^3} & n \text{ odd} \\ 0 & n \text{ even}. \end{cases}

The coefficients decay like 1n3\frac{1}{n^3} because the odd extension is smooth except for a jump in the second derivative. Three terms already give an excellent approximation:

python · try it yourself
import numpy as np
import sympy as sp

x, n = sp.symbols("x n", positive=True)
bn = sp.simplify(2 / sp.pi * sp.integrate(x * (sp.pi - x) * sp.sin(n * x), (x, 0, sp.pi)))
print("b_n =", bn)

xs = np.linspace(0, np.pi, 1001)
f = xs * (np.pi - xs)
S = sum(8 / (np.pi * k**3) * np.sin(k * xs) for k in (1, 3, 5))
print("max error with 3 terms:", round(np.max(np.abs(S - f)), 5))
b_n = 2*(-pi*n*sin(pi*n) - 2*cos(pi*n) + 2)/(pi*n**3)
max error with 3 terms: 0.01331

SymPy keeps sin⁡πn\sin\pi n and cos⁡πn\cos\pi n because it does not know that nn is an integer; with sin⁡nπ=0\sin n\pi = 0 and cos⁡nπ=(−1)n\cos n\pi = (-1)^n its answer is our formula.

The same function, two series

Each series represents ff on (0,L)(0, L) but extends it differently outside. Here is f(x)=xf(x) = x on (0,1)(0, 1):

  • sine series: odd extension, a sawtooth with jumps at ±1\pm 1; coefficients bn=2(−1)n+1nπb_n = \frac{2(-1)^{n+1}}{n\pi}, decaying like 1n\frac{1}{n}.
  • cosine series: even extension ∣x∣\lvert x\rvert, a continuous triangle wave; a0=1a_0 = 1, an=2((−1)n−1)n2π2a_n = \frac{2((-1)^n - 1)}{n^2\pi^2}, decaying like 1n2\frac{1}{n^2}.
python · try it yourself
import numpy as np
from scipy.integrate import quad

L = 1.0
for k in range(1, 6):
    b = 2 / L * quad(lambda x: x * np.sin(k * np.pi * x / L), 0, L)[0]
    a = 2 / L * quad(lambda x: x * np.cos(k * np.pi * x / L), 0, L)[0]
    print(f"n = {k}: b_n = {b: .5f}, a_n = {a: .5f}")
n = 1: b_n =  0.63662, a_n = -0.40528
n = 2: b_n = -0.31831, a_n =  0.00000
n = 3: b_n =  0.21221, a_n = -0.04503
n = 4: b_n = -0.15915, a_n = -0.00000
n = 5: b_n =  0.12732, a_n = -0.01621

The cosine series converges faster because its extension has no jumps.

In software

  • The discrete cosine transform (DCT) is the cosine series of sampled data. JPEG uses it on 8×88 \times 8 pixel blocks precisely because the even extension avoids artificial jumps at the block edges, so few coefficients are needed. scipy.fft.dct computes it.
  • The discrete sine transform (scipy.fft.dst) diagonalizes the finite-difference Laplacian with zero boundary values, giving fast Poisson solvers (Two-Dimensional Poisson Problems).

Exercises

Exercise 1: Sine series of a constant

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

Answer

4π(sin⁡x+sin⁡3x3+sin⁡5x5+⋯ )\frac{4}{\pi}\left(\sin x + \frac{\sin 3x}{3} + \frac{\sin 5x}{5} + \cdots\right).

Solution

bn=2π∫0πsin⁡nx dx=2(1−(−1)n)nπb_n = \frac{2}{\pi}\int_0^\pi \sin nx\,dx = \frac{2(1 - (-1)^n)}{n\pi}. The odd extension is the square wave.

Exercise 2: Cosine series of a constant

What is the cosine series of f(x)=1f(x) = 1 on (0,π)(0, \pi)?

Answer

Just 11 (a0=2a_0 = 2, all other an=0a_n = 0).

Solution

The even extension is the constant 1, which is already a cosine series. a0=2π∫0π1 dx=2a_0 = \frac{2}{\pi}\int_0^\pi 1\,dx = 2, and a02=1\frac{a_0}{2} = 1.

Exercise 3: Choose the series (code)

A rod on (0,2)(0, 2) has insulated ends and initial temperature f(x)=xf(x) = x. Compute the first four coefficients of the appropriate series.

Answer

Cosine series: a0=2a_0 = 2, a1=−8π2≈−0.8106a_1 = -\frac{8}{\pi^2} \approx -0.8106, a2=0a_2 = 0, a3=−89π2≈−0.0901a_3 = -\frac{8}{9\pi^2} \approx -0.0901.

Solution

Insulated ends mean ux=0u_x = 0: cosine series with an=22∫02xcos⁡nπx2 dx=4((−1)n−1)n2π2a_n = \frac{2}{2}\int_0^2 x\cos\frac{n\pi x}{2}\,dx = \frac{4((-1)^n - 1)}{n^2\pi^2}.

python · try it yourself
import numpy as np
from scipy.integrate import quad
print([round(quad(lambda x: x * np.cos(n * np.pi * x / 2), 0, 2)[0], 4) for n in range(4)])
[2.0, -0.8106, 0.0, -0.0901]

Test yourself

A sine series on (0,L)(0, L) corresponds to which extension of ff?

  1. even
  2. odd
  3. periodic with period LL
  4. zero outside

Which series fits insulated ends ux(0)=ux(L)=0u_x(0) = u_x(L) = 0?

  1. sine series
  2. cosine series
  3. neither
  4. complex series

Why does JPEG use a cosine transform rather than a plain Fourier transform?

  1. Cosines are faster to compute
  2. The even extension has no jumps at block edges, so coefficients decay fast
  3. Sines don't exist for images
  4. It is a coincidence