The Heat Equation
The temperature in a thin rod with insulated sides satisfies the heat equation
where is the thermal diffusivity. It also describes diffusion of a chemical, the spread of probability in a random walk, and the blurring of an image. This lesson solves it on by separation of variables and Fourier series.
The problem
The ends are held at temperature 0 (Dirichlet conditions), and is the initial temperature profile.
Separation of variables
Look for solutions . Substituting: , so
The left side depends only on , the middle only on , so both equal a constant . That gives two ODEs:
- , : the eigenvalue problem, with and .
- : .
By superposition,
and requires : the are the sine series coefficients of .
What the solution says
- Every mode decays, and mode decays at rate : high frequencies die fastest. Sharp features smooth out almost immediately; after a while only the first mode is visible.
- The solution is smooth for every , even if has jumps.
import numpy as np
L, k = 1.0, 0.1
f_coeff = lambda n: 2 * (1 - (-1)**n) / (n * np.pi) # f = 1: sine series of a constant
def u(x, t, N=2001):
n = np.arange(1, N + 1)
return np.sum(f_coeff(n) * np.exp(-k * (n * np.pi / L)**2 * t) * np.sin(np.outer(x, n) * np.pi / L), axis=1)
x = np.array([0.1, 0.25, 0.5])
for t in (0.001, 0.05, 0.5, 2.0):
print(f"t = {t:5}: u =", np.round(u(x, t), 4))
print("one-mode prediction at t = 2:", np.round(4 / np.pi * np.exp(-k * np.pi**2 * 2) * np.sin(np.pi * x), 4))t = 0.001: u = [1. 1. 1.]
t = 0.05: u = [0.6827 0.9876 1. ]
t = 0.5: u = [0.2442 0.5532 0.7723]
t = 2.0: u = [0.0547 0.1251 0.1769]
one-mode prediction at t = 2: [0.0547 0.1251 0.1769]
Insulated ends
With (no heat flows out), the eigenfunctions are cosines and is allowed:
As , , the average initial temperature: the heat is trapped and evens out.
Nonzero end temperatures
If the ends are held at and , subtract the steady state (which solves with these boundary values). Then solves the problem with zero boundary values and initial value .
Checking with a numerical method
A finite-difference scheme (explicit method of lines, see The Heat Equation: Method of Lines) should agree with the series:
import numpy as np
L, k, N = 1.0, 0.1, 50
h = L / N
x = np.linspace(0, L, N + 1)
u = np.sin(np.pi * x) + 0.5 * np.sin(3 * np.pi * x) # initial profile with two modes
dt = 0.4 * h**2 / k # stable step (needs dt <= h^2 / (2k))
steps = int(round(0.2 / dt)); dt = 0.2 / steps
for _ in range(steps):
u[1:-1] += dt * k * (u[2:] - 2 * u[1:-1] + u[:-2]) / h**2
exact = np.exp(-k * np.pi**2 * 0.2) * np.sin(np.pi * x) + 0.5 * np.exp(-9 * k * np.pi**2 * 0.2) * np.sin(3 * np.pi * x)
print(f"{steps} steps, max difference from the series solution at t = 0.2: {np.max(np.abs(u - exact)):.2e}")125 steps, max difference from the series solution at t = 0.2: 6.60e-04
In software
- Gaussian blur in image editors is the heat equation run for a short time (its solution on the whole line is convolution with a Gaussian).
- Diffusion models in generative AI add noise by a heat-like (Fokker-Planck) process and learn to reverse it.
- Thermal simulation of chips, buildings and batteries solves 3D heat equations with finite elements.
Exercises
Exercise 1: Single mode
Solve on with at both ends and .
Answer
.
Solution
The initial value is already one eigenfunction (, ), which decays at rate .
Exercise 2: Long-run temperature
A rod of length 2 with insulated ends starts at . What temperature does it approach?
Answer
1, the average of .
Solution
.
Exercise 3: How long until it is cool? (code)
A rod of length 1 with , ends at 0 and initial temperature 1 everywhere. When does the centre temperature fall below 0.1? Use the one-mode approximation, then check with 200 terms.
Answer
About .
Solution
One mode: gives .
import numpy as np
from scipy.optimize import brentq
t1 = np.log(40 / np.pi) / (0.1 * np.pi**2)
n = np.arange(1, 400, 2)
centre = lambda t: np.sum(4 / (n * np.pi) * np.exp(-0.1 * (n * np.pi)**2 * t) * np.sin(n * np.pi / 2))
print(round(t1, 4), round(brentq(lambda t: centre(t) - 0.1, 0.1, 10), 4))2.5778 2.5778
Test yourself
In the series solution of the heat equation, mode decays like…
- it does not decay
Separation of variables assumes $u(x, t) = $…
With insulated ends, the temperature approaches…
- 0
- the average initial temperature
- the maximum initial temperature
- infinity