Heat Equation // Live Solver

FTCS & Crank-Nicolson  ·  Fourier Analytical  ·  Real-Time
// Parameters
Diffusivity α0.50
Fourier Modes N20
Speed
Initial Condition
Solver
// Live Stats
Max Error
L² Norm
Max u(x,t)
Energy
Decay: e−α(nπ/L)²t
n=1 half-life:
n=2 decays 4× faster
Space Play/Pause   R Reset
Numerical FTCS
Analytical (Fourier)
Mode n=1
t = 0.0000 / t_max = 0.5000
// 2D Heat Diffusion Demo
u(x,y,t) — same PDE, same α, square domain
Colour = temperature. Hot spots spread outward.
Coarse features persist; fine features vanish first.
Responds to α slider — try high values!
// L² Error vs Time
Difference between numerical solution and analytical. Log scale reveals exponential decay rate.

How to Use This App

Quick Reference Guide

The Three Curves

Sliders & Selectors

Stats Panel

Right Panel

Keyboard Shortcuts

Export

Click Export PNG in the header to download the current main plot as a PNG image.

Theoretical Background

The 1D Heat Equation — Derivation, Exact Solution & Numerical Methods

1. The PDE

The 1D heat (diffusion) equation governs temperature \(u(x,t)\) along a rod of length \(L\) with thermal diffusivity \(\alpha > 0\):

\[\frac{\partial u}{\partial t} = \alpha\,\frac{\partial^2 u}{\partial x^2}, \qquad x \in [0,L],\; t > 0\]

Physically: the local rate of temperature change equals the Laplacian (curvature) of \(u\) scaled by \(\alpha\). Peaks lose heat; valleys gain it — the equation is a smoothing operator.

2. Boundary & Initial Conditions

\[u(0,t) = 0,\quad u(L,t) = 0\quad\text{(Dirichlet — rod ends held at zero)}\] \[u(x,0) = f(x)\quad\text{(arbitrary initial profile)}\]

3. Separation of Variables

Writing \(u(x,t) = X(x)\,T(t)\) and substituting yields the separated equations with constant \(-\lambda\):

\[\frac{T'(t)}{\alpha\,T(t)} = \frac{X''(x)}{X(x)} = -\lambda\]

The spatial ODE \(X'' + \lambda X = 0\) with homogeneous BCs yields eigenvalues \(\lambda_n = (n\pi/L)^2\) and eigenfunctions \(X_n(x) = \sin(n\pi x/L)\) for \(n = 1,2,3,\ldots\) The temporal factor \(T_n(t) = e^{-\alpha(n\pi/L)^2 t}\); higher modes decay exponentially faster.

4. Exact Analytical Solution

\[u(x,t) = \sum_{n=1}^{\infty} B_n \sin\!\left(\tfrac{n\pi x}{L}\right) e^{-\alpha(n\pi/L)^2\,t}\] \[B_n = \frac{2}{L}\int_0^L f(x)\sin\!\left(\tfrac{n\pi x}{L}\right)dx\quad\text{(Fourier sine coefficients)}\]

This app computes \(B_n\) via a trapezoidal quadrature rule \(\bigl(B_n \approx \frac{2\,\Delta x}{L}\sum_i f(x_i)\sin(n\pi x_i/L)\bigr)\), which is exact for the eigenfunction basis on the grid.

5a. FTCS Numerical Scheme (explicit)

Discretise with \(\Delta x = L/(N-1)\) and timestep \(\Delta t\). The Forward-Time Centered-Space update is:

\[u_i^{n+1} = u_i^n + r\bigl(u_{i+1}^n - 2u_i^n + u_{i-1}^n\bigr), \qquad r = \frac{\alpha\,\Delta t}{(\Delta x)^2}\]

Stability requires the CFL condition \(r \leq 0.5\). This app enforces \(r = 0.49\) automatically, giving first-order accuracy in time, second-order in space.

5b. Crank-Nicolson Scheme (implicit)

Averaging the spatial operator at time levels \(n\) and \(n+1\) gives the unconditionally stable implicit scheme:

\[-\tfrac{r}{2}\,u_{i-1}^{n+1} + (1+r)\,u_i^{n+1} - \tfrac{r}{2}\,u_{i+1}^{n+1} = \tfrac{r}{2}\,u_{i-1}^n + (1-r)\,u_i^n + \tfrac{r}{2}\,u_{i+1}^n\]

Each step solves a tridiagonal linear system via the Thomas algorithm — \(O(N)\) cost. C-N is second-order in both time and space and remains stable for any \(r\), allowing timesteps 4× larger than FTCS here. The stability region covers the entire left half-plane, unlike FTCS which has a disk of radius 1.

6. Key Mathematical Properties

7. References & Further Reading