Sine and Cosine Series
PDE problems live on a finite interval (a rod, a string), not on a periodic line. To use Fourier series there, we extend the function to 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 defined on :
| Sine series | Cosine series | |
|---|---|---|
| extension | odd | even |
| value at and | 0 | free |
| matches boundary conditions | (fixed ends, fixed temperatures 0) | (insulated ends) |
Example: on
The coefficients decay like because the odd extension is smooth except for a jump in the second derivative. Three terms already give an excellent approximation:
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 and because it does not know that is an integer; with and its answer is our formula.
The same function, two series
Each series represents on but extends it differently outside. Here is on :
- sine series: odd extension, a sawtooth with jumps at ; coefficients , decaying like .
- cosine series: even extension , a continuous triangle wave; , , decaying like .
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 pixel blocks precisely because the even extension avoids artificial jumps at the block edges, so few coefficients are needed.
scipy.fft.dctcomputes 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 on .
Answer
.
Solution
. The odd extension is the square wave.
Exercise 2: Cosine series of a constant
What is the cosine series of on ?
Answer
Just (, all other ).
Solution
The even extension is the constant 1, which is already a cosine series. , and .
Exercise 3: Choose the series (code)
A rod on has insulated ends and initial temperature . Compute the first four coefficients of the appropriate series.
Answer
Cosine series: , , , .
Solution
Insulated ends mean : cosine series with .
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 corresponds to which extension of ?
- even
- odd
- periodic with period
- zero outside
Which series fits insulated ends ?
- sine series
- cosine series
- neither
- complex series
Why does JPEG use a cosine transform rather than a plain Fourier transform?
- Cosines are faster to compute
- The even extension has no jumps at block edges, so coefficients decay fast
- Sines don't exist for images
- It is a coincidence