Introduction
The unsteady heat conduction (diffusion) equation is the simplest model problem for studying how a partial differential equation is discretised in space and marched in time. In this study, the equation was discretised with the finite volume method (FVM) and solved in two ways: (i) an explicit scheme (forward Euler), in which each new temperature is computed directly from known values, and (ii) an implicit Crank–Nicolson scheme, in which a tri-diagonal system of equations must be solved at every time step using the tri-diagonal matrix algorithm (TDMA). The 2D problem was solved with the line-by-line TDMA. All numerical results were compared with the analytical Fourier-series solution.
Key equation
The governing equation is the 1D (or 2D) heat conduction equation with thermal diffusivity $\alpha$ (here $\alpha = 1$, i.e. non-dimensional form):
With the initial condition $T(x,0) = 1$ and the boundary conditions $T(0,t) = T(1,t) = 0$, the analytical solution on $0 \le x \le 1$ is the Fourier series below. In 2D, the exact solution is simply the product $T_{2D}(x,y,t) = T(x,t)\,T(y,t)$.
Integrating the equation over a control volume of width $\Delta x$ and over one time step $\Delta t$ gives the θ-scheme, where the diffusive fluxes are a weighted average of the old ($n$) and new ($n+1$) time levels:
$\theta = 0$ gives the explicit (forward Euler) scheme, $\theta = 1/2$ the Crank–Nicolson scheme and $\theta = 1$ the fully implicit scheme. The key non-dimensional parameter is the diffusion number (local Fourier number):
Explicit scheme. With $\theta = 0$, each new value is obtained directly from the old values:
This is simple but only conditionally stable: interior cells require $r \le 1/2$. With cell-centred volumes, the first and last cells lie only $\Delta x / 2$ from the wall, so the boundary flux has a factor 2 and the stricter limit $r \le 1/3$ applies there:
Implicit scheme and TDMA. With $\theta > 0$, the unknown $T_i^{n+1}$ is coupled to its neighbours $T_{i\pm1}^{n+1}$, so each time step requires solving a linear system of the form
where the source term $S_u$ contains only known values from the old time level. Because each equation involves only three unknowns, the matrix is tri-diagonal and can be solved in $O(N)$ operations by the TDMA (Thomas algorithm). In the forward elimination ($i = 1 \to N$), the lower diagonal is eliminated and each equation is rewritten as $T_i = P_i T_{i+1} + Q_i$:
Since $P_N = 0$, the last value is known directly ($T_N = Q_N$), and the back substitution ($i = N \to 1$) recovers all the other temperatures one by one:
Line-by-line TDMA (2D). In 2D each node has four neighbours (E, W, N, S), so the matrix is no longer tri-diagonal. The line-by-line method solves one grid line at a time: along a row, the values on the neighbouring rows ($j \pm 1$) are treated as known and moved to the source term, which leaves a 1D tri-diagonal system that the TDMA can solve. An x-sweep (all rows) followed by a y-sweep (all columns) forms one iteration, and the iterations are repeated within each time step until the residual falls below $10^{-6}$.
Setting
Both codes solve the same non-dimensional problem: a square plate initially at uniform temperature $T = 1$ whose boundaries are suddenly cooled to $T = 0$. The explicit solver was written in Fortran 90, and the implicit TDMA solver, including the step-by-step animation of the algorithm, in Python.
| Parameter | Value |
|---|---|
| Domain | 1D: $0 \le x \le 1$, 2D: $0 \le x, y \le 1$ |
| Thermal diffusivity | $\alpha = 1$ |
| Initial condition | $T = 1$ |
| Boundary condition | $T = 0$ on all boundaries (Dirichlet) |
| Parameter | Explicit (1D, Fortran) | Implicit TDMA (1D / 2D, Python) |
|---|---|---|
| Spatial discretisation | FVM, cell-centred | FVM, node-centred |
| Time integration | Forward Euler ($\theta = 0$) | Crank–Nicolson ($\theta = 0.5$) |
| Grid | $50$ cells, $\Delta x = 0.02$ | $21$ nodes ($21 \times 21$ in 2D), $\Delta x = 0.05$ |
| Diffusion number | $r = 0.25$ ($\le 1/3$, stable) | $r = 1$ |
| Time step | $\Delta t = 1.0 \times 10^{-4}$ (1000 steps) | $\Delta t = 2.5 \times 10^{-3}$ |
| End time | $t = 0.1$ | 1D: $t = 0.5$, 2D: $t = 0.25$ |
| Linear solver | None (direct update) | TDMA (1D), line-by-line TDMA (2D), tolerance $10^{-6}$ |
Result
Figure 1 shows the temperature profiles of the explicit solver every 200 steps ($t = 0, 0.02, \dots, 0.1$). Heat escapes through both walls, and the initially flat profile quickly relaxes into a sine-like shape whose amplitude decays exponentially, as expected from the dominant first Fourier mode $\sin(\pi x)\,e^{-\pi^2 t}$. At $t = 0.1$, the numerical solution lies on top of the analytical solution, with a maximum error of about $1.7 \times 10^{-6}$.

Figure 2 visualises how the TDMA solves one Crank–Nicolson time step. On the left, the forward elimination removes the lower diagonal row by row, turning the tri-diagonal matrix into a bi-diagonal one; at this stage no new temperature is known yet. The back substitution then determines the new temperatures one point at a time from the right end to the left. The animation then continues with time marching, and the centre temperature $T(0.5, t)$ follows the exact solution even at $r = 1$, a time step four times larger than the explicit scheme can stably use on the same grid.

Figure 3 shows the 2D case solved by the line-by-line TDMA. The cyan frame marks the grid line currently being solved as a 1D problem. During the first iteration, the x-sweep and the y-sweep progressively propagate the effect of the cold boundaries into the plate, and repeated sweeps drive the residual below the tolerance. The centre temperature $T(0.5, 0.5, t)$ agrees well with the exact solution $T(0.5,t)^2$, and decays faster than in 1D because heat is lost through four walls instead of two.
