TExcelTAD
Home › Differential Equations › The Heat Equation

The Heat Equation

The temperature u(x,t)u(x, t) in a thin rod with insulated sides satisfies the heat equation

ut=k uxx,u_t = k\,u_{xx},

where k>0k > 0 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 0<x<L0 < x < L by separation of variables and Fourier series.

The problem

ut=k uxx,u(0,t)=u(L,t)=0,u(x,0)=f(x).u_t = k\,u_{xx}, \qquad u(0, t) = u(L, t) = 0, \qquad u(x, 0) = f(x).

The ends are held at temperature 0 (Dirichlet conditions), and ff is the initial temperature profile.

Separation of variables

Look for solutions u=X(x)T(t)u = X(x)T(t). Substituting: XT′=kX′′TXT' = kX''T, so

T′kT=X′′X=−λ.\frac{T'}{kT} = \frac{X''}{X} = -\lambda.

The left side depends only on tt, the middle only on xx, so both equal a constant −λ-\lambda. That gives two ODEs:

  • X′′+λX=0X'' + \lambda X = 0, X(0)=X(L)=0X(0) = X(L) = 0: the eigenvalue problem, with λn=(nπL)2\lambda_n = \left(\frac{n\pi}{L}\right)^2 and Xn=sin⁡nπxLX_n = \sin\frac{n\pi x}{L}.
  • T′=−λnkTT' = -\lambda_nkT: Tn=e−λnktT_n = e^{-\lambda_nkt}.

By superposition,

u(x,t)=∑n=1∞bn e−k(nπ/L)2tsin⁡nπxL,u(x, t) = \sum_{n=1}^\infty b_n\,e^{-k(n\pi/L)^2 t}\sin\frac{n\pi x}{L},

and t=0t = 0 requires ∑bnsin⁡nπxL=f(x)\sum b_n\sin\frac{n\pi x}{L} = f(x): the bnb_n are the sine series coefficients of ff.

What the solution says

  • Every mode decays, and mode nn decays at rate kn2π2L2k\frac{n^2\pi^2}{L^2}: high frequencies die fastest. Sharp features smooth out almost immediately; after a while only the first mode b1e−kπ2t/L2sin⁡πxLb_1e^{-k\pi^2t/L^2}\sin\frac{\pi x}{L} is visible.
  • The solution is smooth for every t>0t > 0, even if ff has jumps.
python · try it yourself
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 ux(0,t)=ux(L,t)=0u_x(0, t) = u_x(L, t) = 0 (no heat flows out), the eigenfunctions are cosines and n=0n = 0 is allowed:

u(x,t)=a02+∑n=1∞an e−k(nπ/L)2tcos⁡nπxL.u(x, t) = \frac{a_0}{2} + \sum_{n=1}^\infty a_n\,e^{-k(n\pi/L)^2 t}\cos\frac{n\pi x}{L}.

As t→∞t \to \infty, u→a02u \to \frac{a_0}{2}, the average initial temperature: the heat is trapped and evens out.

Nonzero end temperatures

If the ends are held at u(0,t)=T0u(0, t) = T_0 and u(L,t)=T1u(L, t) = T_1, subtract the steady state v(x)=T0+(T1−T0)xLv(x) = T_0 + (T_1 - T_0)\frac{x}{L} (which solves v′′=0v'' = 0 with these boundary values). Then w=u−vw = u - v solves the problem with zero boundary values and initial value f−vf - v.

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:

python · try it yourself
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 ut=2uxxu_t = 2u_{xx} on (0,π)(0, \pi) with u=0u = 0 at both ends and u(x,0)=5sin⁡3xu(x, 0) = 5\sin 3x.

Answer

u=5e−18tsin⁡3xu = 5e^{-18t}\sin 3x.

Solution

The initial value is already one eigenfunction (n=3n = 3, L=πL = \pi), which decays at rate kn2=2⋅9kn^2 = 2 \cdot 9.

Exercise 2: Long-run temperature

A rod of length 2 with insulated ends starts at f(x)=xf(x) = x. What temperature does it approach?

Answer

1, the average of ff.

Solution

a02=12⋅22∫02x dx=1\frac{a_0}{2} = \frac{1}{2}\cdot\frac{2}{2}\int_0^2 x\,dx = 1.

Exercise 3: How long until it is cool? (code)

A rod of length 1 with k=0.1k = 0.1, 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 t≈2.578t \approx 2.578.

Solution

One mode: 4πe−0.1π2t=0.1\frac{4}{\pi}e^{-0.1\pi^2t} = 0.1 gives t=ln⁡(40/π)0.1π2t = \frac{\ln(40/\pi)}{0.1\pi^2}.

python · try it yourself
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 nn decays like…

  1. e−knte^{-knt}
  2. e−k(nπ/L)2te^{-k(n\pi/L)^2t}
  3. 1/n1/n
  4. it does not decay

Separation of variables assumes $u(x, t) = $…

  1. X(x)+T(t)X(x) + T(t)
  2. X(x)T(t)X(x)T(t)
  3. x tx\,t
  4. ex+te^{x+t}

With insulated ends, the temperature approaches…

  1. 0
  2. the average initial temperature
  3. the maximum initial temperature
  4. infinity