Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 39 ยท PDEs and Applications

The Heat Equation

Solve the 1D heat equation exactly with Fourier series and numerically with explicit and implicit finite differences, including stability limits, in Python.

Drop a hot spot into a cold metal bar and watch it spread. The heat equation, u_t = k u_xx, captures that spreading. It is also the model for diffusion of ink in water, of pollutants in the air, of prices in finance (the Black-Scholes equation transforms into it) and of the smoothing done by a Gaussian blur in image processing.

The constant k > 0 is the diffusivity: it measures how fast things even out. This lesson gives you the exact solution with Fourier series and then the numerical method that works when no formula exists.

What the equation says

The second derivative u_xx measures curvature. Where the temperature profile is convex (curving up, like a valley), u_xx > 0 and the temperature rises; where it is concave (a hill), the temperature falls. So peaks are lowered and dips are filled: the profile smooths out. High-frequency wiggles die much faster than slow ones, because a wave sin(nฯ€x/L) decays like exp(-k (nฯ€/L)ยฒ t), and the rate grows with nยฒ.

Typical features:

  • Smoothing: even a jagged initial profile becomes infinitely smooth for t > 0.
  • Maximum principle: the temperature inside never exceeds the largest initial or boundary value.
  • Arrow of time: it is irreversible. Running it backward is unstable.
  • Steady state: as t -> โˆž with fixed boundary values the solution tends to the solution of the Laplace equation u_xx = 0, a straight line in 1D.

Exact solution for a rod with cold ends

For a rod of length L with u(0, t) = u(L, t) = 0 and u(x, 0) = f(x), separation of variables gives

TEXT
u(x, t) = ฮฃ_{n>=1} b_n exp(-k (nฯ€/L)ยฒ t) sin(nฯ€x/L),     b_n = (2/L) โˆซ f(x) sin(nฯ€x/L) dx
Python Fourier solution of the heat equation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The tent's sharp peak at x = 0.5 is gone almost immediately, and by t = 0.2 the profile is a single smooth hump decaying like exp(-ฯ€ยฒ k t): the slowest mode survives longest.

The explicit finite-difference scheme

When boundary conditions or coefficients are complicated, discretize. Use grid spacing ฮ”x and time step ฮ”t, and write u_i^n โ‰ˆ u(x_i, t_n). Replace u_t by a forward difference and u_xx by the central difference:

TEXT
u_i^{n+1} = u_i^n + r (u_{i+1}^n - 2 u_i^n + u_{i-1}^n),     r = k ฮ”t / ฮ”xยฒ

This is the FTCS (forward time, centered space) scheme. It is cheap and simple, and it has a famous stability restriction: r <= 1/2. Larger values make high-frequency errors grow, because the amplification factor of the highest mode is 1 - 4r, whose magnitude exceeds 1 once r > 1/2. Since r contains ฮ”xยฒ in the denominator, halving ฮ”x forces ฮ”t to shrink by four: that is expensive.

Python FTCS stability: r = 0.4 versus r = 0.6

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

At r = 0.4 and 0.5 the numerical solution matches the exact one to several digits. At r = 0.6 rounding errors in the highest-frequency mode multiply by 1.4 each step and swamp the answer: the scheme is unconditionally unstable beyond the limit.

Implicit schemes: backward Euler and Crank-Nicolson

Evaluate the spatial difference at the new time level instead:

TEXT
u_i^{n+1} - r (u_{i+1}^{n+1} - 2 u_i^{n+1} + u_{i-1}^{n+1}) = u_i^n

Now each step solves a tridiagonal linear system. The scheme (backward Euler in time) is stable for every r, as discussed in stiff equations, and costs only O(N) per step with a tridiagonal solver. It is still only first-order accurate in time. The Crank-Nicolson scheme averages the explicit and implicit versions, giving second-order accuracy in time and stability for all r:

TEXT
(I - (r/2) D) u^{n+1} = (I + (r/2) D) u^n,     D = second-difference matrix
Python Crank-Nicolson with large time steps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Even with r far above 0.5 (the largest step is 50 times what FTCS would allow) the method is stable and the error is small. It hardly changes with dt because the remaining error of about 1e-4 comes from the spatial grid (ฮ”x = 0.02), not from the time stepping: refine N to reduce it.

Solving the heat equation

Fourier Exact, for simple geometries.
FTCS Easy, but needs r at most 1/2.
Backward Euler Stable for any step, first order.
Crank-Nicolson Stable and second order.

The stability limit comes from the amplification factor of the highest frequency.

1 / 2

Other boundary conditions

With a fixed temperature u = a at an end, put a in the boundary node. With an insulated end (u_x = 0) use a ghost point: replace u_{-1} by u_1 in the formula at the boundary node. In the Fourier solution the sine series is replaced by a cosine series for insulated ends. If the equation has a source term, u_t = k u_xx + q(x), subtract the steady-state solution and solve for the deviation.

Common mistakes

  • Choosing ฮ”t by feel for FTCS and being surprised by blow-up. Always compute r.
  • Using a sine series with insulated ends.
  • Applying the maximum principle with sources present: it only holds without them.
  • Confusing k (diffusivity) with conductivity: k = conductivity / (density ร— specific heat).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. The FTCS scheme for the heat equation is stable when:

    The highest-frequency mode is multiplied by 1 - 4r each step, which stays within [-1, 1] only for r <= 1/2.
  2. 2. Which Fourier mode of the heat equation decays fastest?

    Decay rates are k (nฯ€/L)ยฒ, so higher n decays faster.
  3. 3. If you halve dx in FTCS and keep r fixed, dt is multiplied by:

    dt = r dxยฒ/k, so halving dx makes dt four times smaller.
  4. 4. Which scheme is second-order in time and stable for all r?

    Crank-Nicolson averages explicit and implicit steps, giving order 2 and unconditional stability.

Summary

  • The heat equation u_t = k u_xx smooths profiles; high frequencies vanish first.
  • Fourier series give the exact solution: u = ฮฃ b_n e^{-k (nฯ€/L)ยฒ t} sin(nฯ€x/L).
  • FTCS is explicit and requires r = kฮ”t/ฮ”xยฒ <= 1/2.
  • Backward Euler and Crank-Nicolson solve a tridiagonal system per step and are unconditionally stable.
  • Boundary conditions enter through boundary nodes or ghost points.

Next: The Wave Equation