Skip to content
Tutorials Center
My learning

Topics

Lesson 62 of 68 ยท Projects

Project: Heat Flow in a Rod

Solve the 1D heat equation with finite differences in numpy, find the stability limit r = 0.5, then check the result against the Fourier-series solution.

Heat flow in a thin rod is the classic first partial differential equation. It is simple enough that you can solve it two completely different ways: by marching a grid forward in time, and by an exact series of sine waves. In this project you build both and make them check each other.

You will also meet the most famous trap in numerical PDEs. A time step that is slightly too large does not give a slightly wrong answer; it gives an answer that explodes. Finding that limit by experiment, and then understanding why it exists, teaches more than any formula. The background is in Partial Differential Equations: Heat and Wave and Fourier Series.

The problem

A rod of length 1 has its two ends held at temperature 0, for example by pressing them against ice. At time t = 0 the middle half, 0.25 <= x <= 0.75, is at temperature 1 and the outer quarters are at 0. How does the temperature u(x, t) evolve?

The heat equation says that the temperature at a point changes at a rate proportional to the curvature of the temperature profile:

du/dt = k * d2u/dx2

where k is the thermal diffusivity. It makes physical sense. Where the profile bends upward (positive second derivative), the point is colder than the average of its neighbours and warms up. Where it bends downward, the point is hotter than its neighbours and cools. We use k = 0.05 in units chosen to keep the demo readable.

flowchart TD
    P["Heat equation<br/>u_t = k u_xx, ends fixed at 0"] --> G["Grid method<br/>replace derivatives by differences"]
    P --> F["Series method<br/>separation of variables"]
    G --> S["Stability limit<br/>r = k dt / dx^2 <= 0.5"]
    G --> N["Numeric solution"]
    F --> X["Exact sine series"]
    N --> C["Compare: errors"]
    X --> C

Two independent routes to the same temperature profile, which makes each one a check on the other.

Step 1: finite differences

Replace the derivatives by differences on a grid with spacing dx and time step dt. A forward difference in time and a centred second difference in space give

u_new[j] = u[j] + r * (u[j+1] - 2*u[j] + u[j-1]), with r = k*dt/dx**2.

Each new value is the old value plus a fraction r of how far it is from the average of its neighbours. That is the same intuition as the equation, in one line of numpy. This is the explicit (forward-time, centred-space) scheme. We use 101 points, so dx = 0.01, and choose r = 0.4, which fixes dt = 0.0008 and takes 1,250 steps to reach t = 1.

Python Explicit finite-difference solver

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 sharp edges of the pulse round off almost immediately, then the whole profile sinks and widens into a single smooth hump, which is the shape of sin(pi*x). The centre cools from 1 to 0.56 by t = 1. Total heat, the integral of u, drops from about 0.51 to 0.35 because the cold ends keep draining it; with insulated ends it would stay constant, which is a good extra test to try.

The line u[1:-1] = u[1:-1] + r * (u[2:] - 2*u[1:-1] + u[:-2]) updates all interior points at once. Slicing means numpy does the loop in compiled code, and the right-hand side is fully evaluated from the old values before anything is stored, which is exactly what the scheme requires.

Step 2: the stability limit

Why r = 0.4? Try a larger time step and the answer explodes. To see why, feed the scheme the roughest possible profile, a zig-zag +1, -1, +1, .... For that profile u[j+1] - 2*u[j] + u[j-1] = -4*u[j], so one step multiplies the zig-zag by 1 - 4r. If |1 - 4r| is larger than 1 the zig-zag grows with every step, and round-off error always contains a little of it. The condition |1 - 4r| <= 1 gives

r = k*dt/dx**2 <= 1/2.

The consequence is severe: halving dx to get a more accurate answer forces dt to shrink by a factor of four, so the work grows by a factor of eight.

Python The stability limit r = 0.5

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.6 the maximum is 2.48 after only 10 steps (it began at 1, and heat can never exceed the starting maximum), and 1.1e20 after 150. At r = 0.51 the blow-up is slow, so the maximum is only 3.88 after 150 steps, but it is still growing; run more steps and it will explode too. At exactly r = 0.5 the zig-zag factor is -1: that mode neither grows nor decays, which is why it sits on the edge.

An unstable run is not "less accurate". Heat conduction can never create a temperature higher than the starting maximum, so any value above 1 here tells you immediately that something is wrong. Always check your numerical answer against a physical bound.

Step 3: compare with the Fourier-series solution

Separation of variables gives the exact answer as a sum of sine waves. Each wave sin(n*pi*x) fits the fixed ends, and decays on its own schedule:

u(x, t) = sum over n of b_n * exp(-k*(n*pi)**2 * t) * sin(n*pi*x)

The coefficients come from the Fourier sine series of the starting profile:

b_n = 2 * integral from 0 to 1 of u(x, 0) * sin(n*pi*x) dx = (2 / (n*pi)) * (cos(n*pi/4) - cos(3*n*pi/4))

Note the factor n**2 inside the exponential. High-frequency waves die much faster than low-frequency ones, which is why the sharp edges vanish almost at once and only a smooth hump remains.

Python Comparing with the Fourier-series solution

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.

By t = 1 one term leaves a difference of 1.2e-2, and with three terms the difference drops to 8.4e-3 and then stops improving. At that point the series is essentially exact (every further term is below 1e-5), so the remaining 8.4e-3 is the error of the grid solution, not of the series. That is a useful lesson in reading error tables: when adding more terms stops helping, you have found the error of the other method. The grid error comes mostly from the discontinuous starting profile, which a grid of 101 points can only sample roughly.

The last two lines show the price of the series at t = 0: with 60 terms the value at the centre is 1.015, not 1, and 0.9951 near the edge. This is the Gibbs ripple from Fourier Series. It is harmless at later times because the high-n terms vanish exponentially, which is one reason the series is so efficient for heat flow.

Step 4: an implicit scheme without the limit

The stability limit comes from evaluating the right-hand side at the old time. The implicit (backward Euler) scheme evaluates the second difference at the new time:

u_new[j] - r * (u_new[j+1] - 2*u_new[j] + u_new[j-1]) = u[j]

That is a tridiagonal linear system for the new values, solved by scipy.linalg.solve_banded in one call per step. It is stable for every r, so the time step can be chosen for accuracy alone. The price is a linear solve per step, and the error in time is only first order: halving dt halves the error. We check that on a smooth starting profile sin(pi*x) + 0.5*sin(3*pi*x), whose exact solution is known in closed form.

Python Implicit scheme and accuracy check

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.

With r = 25, 50 times the explicit limit, the implicit solution stays between 0 and 0.57: smooth, bounded and physically sensible. On the smooth problem the error halves each time dt halves (ratios 2.03, 2.01, 1.99), confirming first-order accuracy in time.

Extending the project

  • Insulated ends. Replace the fixed ends with u[0] = u[1] and u[-1] = u[-2] and check that total heat stays constant.
  • Crank-Nicolson. Average the explicit and implicit right-hand sides for second-order accuracy in time. Measure the error ratio when halving dt; you should see about 4.
  • A source term. Add + dt * q(x) to model a heater in the rod and watch the profile settle to a steady state.
  • Two materials. Make k depend on x (k_left, k_right) and notice that the Fourier solution no longer applies, while the grid method barely changes.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In the heat equation u_t = k u_xx, why does a point that is colder than the average of its neighbours warm up?

    A point below the average of its neighbours sits at a local dip, where the second derivative is positive. Then u_t = k * u_xx is positive and the temperature rises.
  2. 2. What is the stability condition for the explicit scheme with r = k dt / dx^2?

    A zig-zag error is multiplied by (1 - 4r) each step, so it grows unless |1 - 4r| <= 1, which means r <= 1/2.
  3. 3. You halve dx to improve accuracy and keep r = 0.4. What happens to dt?

    Since dt = r dx^2 / k, halving dx divides dt by four. Together with twice as many grid points, the total work grows by about a factor of eight.
  4. 4. Why do the sharp corners of the starting pulse disappear so quickly in the Fourier solution?

    Sharp features are made of high-frequency sine waves, and the exponent contains n squared, so those waves vanish almost immediately.
  5. 5. Why is the implicit scheme often used for stiff heat problems?

    Backward Euler damps every mode instead of amplifying the fast ones. Each step costs a tridiagonal solve, but there is no time-step limit from stability.

Summary

  • The heat equation u_t = k*u_xx says each point moves toward the average of its neighbours.
  • The explicit scheme u += r*(u[j+1] - 2*u[j] + u[j-1]) is one line of numpy but is stable only for r = k*dt/dx**2 <= 0.5.
  • A zig-zag mode is multiplied by 1 - 4r per step; for r = 0.6 the run reached 1e20 after 150 steps.
  • The exact solution is a sum of sine waves decaying like exp(-k*(n*pi)**2*t), so sharp features vanish first.
  • When adding series terms stops reducing the difference, you are looking at the grid method's own error.
  • Backward Euler removes the stability limit (tested at r = 25) at the cost of first-order accuracy in time, which we confirmed by halving dt.

Next: Project: Gradient Descent from Calculus.