SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 6.4.1 Diffusion and the heat equationConcept · 30 min
      2. 6.4.2 The fundamental solutionConcept · 30 min
      3. 6.4.3 Practice set: the heat equationPractice set · 20 min
      4. 6.4.4 Case study: from Black–Scholes to the heat equationCase study · 35 min

Level 6, Differential equations 6.4 The heat equation Lesson 1 of 4

ConceptLesson 6.4.1 in Level 6, Differential equations. About 30 minutes.

Diffusion and the heat equation

The heat equation

Picture a thin metal rod along the xx-axis, wrapped so that heat can only flow along it. Write u(x,t)u(x, t) for its temperature at position xx and time tt. The heat equation says how that temperature changes.

Rule: The heat equation

∂u∂t=∂2u∂x2,orut=uxx\frac{\partial u}{\partial t} = \frac{\partial^2 u}{\partial x^2}, \quad \text{or} \quad u_t = u_{xx}

The left side is how fast the temperature at a fixed point changes in time. The right side is how the profile bends at that point at that moment: the second partial in xx, with tt held fixed (lesson 4.1.2).

Lesson 6.3.1 met this equation and checked solutions such as x2+2tx^2 + 2t and e−tsin⁡xe^{-t}\sin x by computing their partials. This lesson explains what the equation does to a profile, and why.

Units, and the diffusivity

For a real rod the equation reads ut=D uxxu_t = D\,u_{xx}. The diffusivity DD says how fast heat spreads in the material. It is in units of length² per unit of time, such as m² per second, so both sides are in degrees per unit of time.

A change of clock removes DD. Measure time as s=Dts = Dt; by the chain rule (lesson 4.2.2), ut=D usu_t = D\,u_s, so the equation becomes us=uxxu_s = u_{xx}. With D=0.5D = 0.5, two seconds make one unit of ss.

From here on D=1D = 1. Case study 6.4.4 removes a DD in exactly this way.

Hot to cold: the bend and the neighbours

The second central difference (lessons 2.4.2 and 2.8.1) estimates the bend from three values a step hh apart. Write m=u(x+h)+u(x−h)2m = \frac{u(x + h) + u(x - h)}{2} for the average of the two neighbours. Then

uxx≈u(x+h)−2u(x)+u(x−h)h2=2h2(m−u(x))\begin{aligned} u_{xx} &\approx \frac{u(x + h) - 2u(x) + u(x - h)}{h^2} \\ &= \frac{2}{h^2}\big(m - u(x)\big) \end{aligned}

So uxx>0u_{xx} > 0 means the point is colder than its neighbours on average. Heat flows in from them, and the heat equation says the point warms. A point hotter than its neighbours cools.

Rule: Which way a point moves

uxx>0: u rises (a dip fills in)uxx<0: u falls (a peak sinks)uxx=0: u is still, for now\begin{gathered} u_{xx} > 0: \ u \text{ rises (a dip fills in)} \\[4pt] u_{xx} < 0: \ u \text{ falls (a peak sinks)} \\[4pt] u_{xx} = 0: \ u \text{ is still, for now} \end{gathered}

The speed is the size of the bend. Only the bend counts, not the height or the slope: a straight profile such as 3x+13x + 1 never changes, however steep it is.

A worked example: a dip between two peaks

Take a rod of length π\pi whose ends are held at temperature 0, a boundary condition as in lesson 6.3.2. Start it at u(x,0)=sin⁡x+0.5sin⁡3xu(x, 0) = \sin x + 0.5\sin 3x: two peaks with a dip between them. The solution is

u(x,t)=e−tsin⁡x+0.5 e−9tsin⁡3xu(x, t) = e^{-t}\sin x + 0.5\,e^{-9t}\sin 3x

Each term solves the equation, and the equation is linear, so their sum does too (lesson 6.3.1). For the second term, ut=−4.5 e−9tsin⁡3xu_t = -4.5\,e^{-9t}\sin 3x, and differentiating twice in xx gives 0.5 e−9t(−9sin⁡3x)0.5\,e^{-9t}(-9\sin 3x), the same.

At t=0t = 0 the bend is uxx=−sin⁡x−4.5sin⁡3xu_{xx} = -\sin x - 4.5\sin 3x. The table checks it against the neighbours with h=0.1h = 0.1. It looks at the dip, the left peak, and the point between them where the bend changes sign.

pointxxuuneighbours' average mmsecond differenceuxxu_{xx}
dip1.57080.50.51733.46723.5
left peak0.70171.07581.0534−4.4888-4.4888−4.5185-4.5185
between1.11410.79780.7978−0.0063-0.00630

The dip is colder than its neighbours, so it warms at 3.5 degrees per unit of time. The peaks cool at about 4.5. Between them the point is, for a moment, still.

time tt00.050.10.20.51
dip, x=π2x = \frac{\pi}{2}0.50.63240.70160.73610.6010.3678
peak, x=0.7017x = 0.70171.07580.88840.7590.59960.39630.2375

The dip fills in until t=0.188t = 0.188, when it reaches 0.7365. After that the whole profile sinks, because heat leaks out through the cold ends.

The profile bends down at the peaks, so they sink; it bends up at the dip, so the dip rises. By t=0.2t = 0.2 the dip has gone, and the single hump that is left sinks as heat leaves through the ends.

Try it

SymPy confirms that ut−uxxu_t - u_{xx} is 0. The loop prints the first table: uu, the neighbours' average, and the second difference with h=0.1h = 0.1.

The last loop prints the second table, the dip and the peak over time.

⌘/Ctrl + Enter runs the cell

Fine wiggles fade fastest

The example has two waves, and they fade at different rates. For any number kk, the wave e−k2tsin⁡kxe^{-k^2 t}\sin kx solves the heat equation, because utu_t and uxxu_{xx} are both −k2u-k^2 u.

Rule: Decaying waves

u(x,t)=e−k2tsin⁡kxu(x, t) = e^{-k^2 t}\sin kx

A wave with kk times as many wiggles fades k2k^2 times as fast: its height halves after time ln⁡2k2\frac{\ln 2}{k^2}. The same holds with cos⁡kx\cos kx. Fine detail dies first, so the heat equation smooths a profile.

wiggles kk12310
height left at t=0.1t = 0.10.90480.67030.40664.54×10−54.54 \times 10^{-5}
time to halve0.69310.17330.0770.0069

In the example the sin⁡3x\sin 3x wave fades 9 times as fast as sin⁡x\sin x. By t=0.5t = 0.5 it is down to e−4.5≈0.0111e^{-4.5} \approx 0.0111 of its start, which is why one hump is left.

Lesson 6.3.2 found that running the heat equation backwards makes small ripples explode. The waves show why: backwards in time each one grows like ek2te^{k^2 t}, and the finest grow fastest.

Corners and jumps

A corner is smoothed at once. Take the kink max⁡(x,0)\max(x, 0): its slope jumps from 0 to 1 at x=0x = 0, so uxxu_{xx} does not exist there. Its second difference at 0 is h−0+0h2=1h\frac{h - 0 + 0}{h^2} = \frac{1}{h}, which is 10, 100 and 1000 for hh = 0.1, 0.01 and 0.001.

That grows without limit as hh shrinks, so the corner starts to rise infinitely fast and is rounded off immediately. A jump, such as a step from 0 to 1, is smoothed at once in the same way. Away from the corner, where the kink is straight, nothing happens at first.

Try it

The first loop prints the table: what is left of each wave at t=0.1t = 0.1, to 4 significant figures, and its time to halve. 4.54e-05 means 4.54×10−54.54 \times 10^{-5}.

Then the sin⁡3x\sin 3x wave at t=0.5t = 0.5, and the kink's second difference at 0 for three steps: 1h\frac{1}{h} each time. round hides float noise.

⌘/Ctrl + Enter runs the cell

The explorer shows the heat equation smoothing three profiles on a long rod. Choose the kink and set t=0.01t = 0.01: the corner is already round, while at x=0.5x = 0.5 the profile has not moved. Then choose the spike and move tt up to 1. The height at 0 falls from 3.3333 to about 0.28, while the spread 2t\sqrt{2t} grows to 1.4142.

Explore

Heat flows from hot to cold, so peaks sink and dips fill in. Move time forward and watch the profile spread out; a corner is smoothed at once.

−2020123

Dashed: the profile at time 0. Dark: the exact solution.

Height at x = 0
0.1262
Spread
the bell curve that smooths it has standard deviation √(2t) = 0.3162

Total heat stays the same

The total heat in the rod between aa and bb is the area under the profile, ∫abu dx\int_a^b u\,dx. Its rate of change is ∫abut dx=∫abuxx dx\int_a^b u_t\,dx = \int_a^b u_{xx}\,dx. By the fundamental theorem (lesson 2.5.3) that is the difference of the slopes uxu_x at the two ends.

Rule: Conservation of heat

ddt∫abu dx=ux(b,t)−ux(a,t)\frac{d}{dt}\int_a^b u\,dx = u_x(b, t) - u_x(a, t)

The slope at an end measures heat flowing in or out there. With no flow through the ends, the total stays the same: heat moves about, but none is made or lost.

That holds for an insulated rod, whose ends have ux=0u_x = 0, and on an endless line when the profile is flat and zero far out. It fails when heat can cross the ends.

The rod of the example has its ends held at 0, so heat leaks out. At the start ux=2.5u_x = 2.5 at x=0x = 0 and −2.5-2.5 at x=πx = \pi, so the total falls at −2.5−2.5=−5-2.5 - 2.5 = -5 per unit of time. Integrating the solution gives 2e−t+13e−9t2e^{-t} + \frac{1}{3}e^{-9t}: 2.3333 at the start, 1.9452 at t=0.1t = 0.1, 1.2168 at 0.5 and 0.7358 at 1.

Try it

quad integrates the profile at four times, next to the formula 2e−t+13e−9t2e^{-t} + \frac{1}{3}e^{-9t}.

The last line prints the slopes at the two ends at t=0t = 0 and their difference, the rate at which the total changes.

⌘/Ctrl + Enter runs the cell

Spreading like t\sqrt{t}

Now put one unit of heat in a narrow tent around x=0x = 0 on an endless rod. As time passes the heat spreads out, but its centre stays at 0 and its total stays 1. Read the profile as a density (lesson 5.4.1): its variance, ∫x2u dx\int x^2 u\,dx, grows by exactly 2 per unit of time.

Rule: Spreading

Var⁡(t)=Var⁡(0)+2twidth of heat from a point: 2t\begin{gathered} \operatorname{Var}(t) = \operatorname{Var}(0) + 2t \\[4pt] \text{width of heat from a point: } \sqrt{2t} \end{gathered}

For a total heat of 1 centred at 0; the width is the standard deviation. Stated here and checked below; lesson 6.4.2 meets it in an exact solution. Since the area stays 1 while the width grows, the peak falls like 1t\frac{1}{\sqrt{t}}.

time tt0.010.040.160.64
width 2t\sqrt{2t}0.14140.28280.56571.1314

Four times the time gives only twice the width. A fair random walk spreads in the same way: after nn steps its standard deviation is n\sqrt{n} (lesson 5.11.1). Heat is the same fact in continuous time, and lesson 6.4.2 makes the link exact.

Stepping a profile forward

A computer can follow the heat equation directly. Keep the profile's values on a grid of points Δx\Delta x apart, and estimate the bend at each by the second difference. Then take a short time step Δt\Delta t: each value moves by Δt\Delta t times its bend, towards its neighbours' average.

Lesson 6.5.1 studies this method, its error and how short Δt\Delta t must be. Here Δx=0.05\Delta x = 0.05 and Δt=0.0005\Delta t = 0.0005, a fifth of Δx2\Delta x^2, which is safely small.

Try it

The tent has half-width 0.2 and area 1, on a grid from −8-8 to 8 whose end values stay 0. Each pass of the loop is one time step for every inner point at once.

At tt = 0.25, 0.5 and 1 the cell prints the total heat, the growth of the variance and the peak. The total stays 1 and the variance grows by 2t2t. The peak, 0.5606, 0.3977 and 0.2816, is close to 14πt\frac{1}{\sqrt{4\pi t}}: lesson 6.4.2's height for heat that starts at a single point.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. The bend drives the change, not the height or the slope. A steep but straight profile does not change at all.
  2. Check the sign. uu rises where uxx>0u_{xx} > 0, where the profile bends up: a dip. It falls at a peak.
  3. Corners and jumps have no uxxu_{xx}. The second difference there grows like 1h\frac{1}{h}, and the corner is rounded at once.
  4. Heat is conserved only when none crosses the ends. Ends held at a fixed temperature let heat out or in, at the rate given by the slopes there.
  5. The width grows like t\sqrt{t}, not like tt. From a single point it is 2t\sqrt{2t}: four times the time, twice the width.
  6. Mind the clock. For ut=D uxxu_t = D\,u_{xx}, use the results of this lesson with s=Dts = Dt in place of tt.

Diffusion and the heat equation

0 of 13 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12.
  13. 13.
How to type answers
  • Powers with ^: x^7, 2^(1/3), a^-2.
  • Multiply with * or by writing side by side: 3x^2, 2(x+1).
  • ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
  • Your first answer to each question is saved: it counts towards your score and brings the question back for review.
  • Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.

Summary card

  • The heat equation ut=uxxu_t = u_{xx}: the temperature at a point changes at the rate the profile bends there. With a diffusivity, ut=D uxxu_t = D\,u_{xx} becomes us=uxxu_s = u_{xx} with s=Dts = Dt.
  • uxx≈2h2(m−u)u_{xx} \approx \frac{2}{h^2}(m - u), with mm the neighbours' average: a point colder than its neighbours warms. Peaks sink, dips fill in, straight stretches stay put.
  • e−k2tsin⁡kxe^{-k^2 t}\sin kx is a solution: fine wiggles fade k2k^2 times as fast, so corners and jumps are smoothed at once.
  • Total heat changes by ux(b,t)−ux(a,t)u_x(b, t) - u_x(a, t): it is conserved when no heat crosses the ends.
  • From a point, heat spreads to width 2t\sqrt{2t}; the variance grows by 2t2t and the peak falls like 1t\frac{1}{\sqrt{t}}.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.