2.1 Derivation of the Heat Conduction Equation
We derive the diffusion equation from Fourier's law of heat conduction.
📐 Theory
heat conduction equation(diffusion equation):
$$\frac{\partial u}{\partial t} = \alpha \frac{\partial^2 u}{\partial x^2}$$where $u(x,t)$ is temperature and $\alpha = k/(\rho c_p)$ is thermal diffusivity
$k$: thermal conductivity, $\rho$: density, $c_p$: specific heat
💻 Code Example 1: Numerical solution of heat conduction equation (explicit scheme)
# Requirements:
# - Python 3.9+
# - matplotlib>=3.7.0
# - numpy>=1.24.0, <2.0.0
"""
Example: $k$: thermal conductivity, $\rho$: density, $c_p$: specific
Purpose: Demonstrate data visualization techniques
Target: Beginner to Intermediate
Execution time: 2-5 seconds
Dependencies: None
"""
import numpy as np
import matplotlib.pyplot as plt
# Parameters
L = 10.0 # rod length
alpha = 0.1 # thermal diffusivity
T_total = 10.0
Nx = 100
Nt = 2000
x = np.linspace(0, L, Nx)
t = np.linspace(0, T_total, Nt)
dx = x[1] - x[0]
dt = t[1] - t[0]
# Stability condition verification
r = alpha * dt / dx**2
print(f"Stability parameter r = {r:.4f} (stability condition: r ≤ 0.5)")
# Initial condition: step function
u = np.zeros((Nt, Nx))
u[0, Nx//4:3*Nx//4] = 100.0
# Boundary conditions: u(0,t) = u(L,t) = 0
# explicit scheme(FTCS: Forward Time Central Space)
for n in range(Nt-1):
for i in range(1, Nx-1):
u[n+1, i] = u[n, i] + r * (u[n, i+1] - 2*u[n, i] + u[n, i-1])
u[n+1, 0] = 0
u[n+1, -1] = 0
# Visualization
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
axes = axes.flatten()
times_idx = [0, Nt//10, Nt//3, Nt-1]
for idx, n in enumerate(times_idx):
ax = axes[idx]
ax.plot(x, u[n], 'b-', linewidth=2)
ax.axhline(0, color='gray', linewidth=0.5)
ax.grid(True, alpha=0.3)
ax.set_xlabel('Position x', fontsize=12)
ax.set_ylabel('Temperature u(x,t)', fontsize=12)
ax.set_title(f't = {t[n]:.2f}', fontsize=12)
ax.set_ylim(-10, 110)
plt.suptitle('Numerical solution of heat conduction equation (explicit scheme)', fontsize=14)
plt.tight_layout()
plt.show()
# Spacetime plot
plt.figure(figsize=(12, 6))
plt.contourf(x, t, u, levels=50, cmap='hot')
plt.colorbar(label='Temperature u(x,t)')
plt.xlabel('Position x', fontsize=12)
plt.ylabel('Time t', fontsize=12)
plt.title('Heat diffusion spacetime diagram', fontsize=14)
plt.tight_layout()
plt.show()
print("\nProperties of heat conduction equation:")
print("- Heat diffuses from high temperature to low temperature")
print("- Temperature distribution becomes smoother over time")
print("- Entropy increases (irreversible process)")