Lab 06 — 1D Heat Equation (Explicit FTCS)#
Solve u_t = alpha * u_xx on [0,1] with Dirichlet BCs.
import numpy as np
import matplotlib.pyplot as plt
alpha = 0.01
nx = 51
L = 1.0
x = np.linspace(0, L, nx)
dx = x[1]-x[0]
# stability: dt <= dx^2/(2*alpha)
dt = 0.4 * dx**2/alpha
nt = 200
u = np.exp(-100*(x-0.5)**2) # initial condition (Gaussian)
u_all = [u.copy()]
for n in range(nt):
un = u.copy()
# interior update
u[1:-1] = un[1:-1] + alpha*dt/dx**2 * (un[2:] - 2*un[1:-1] + un[:-2])
# Dirichlet BCs
u[0] = 0.0
u[-1] = 0.0
if n % 20 == 0:
u_all.append(u.copy())
plt.figure()
for snap in u_all:
plt.plot(x, snap)
plt.xlabel('x'); plt.ylabel('u'); plt.title('1D Heat (explicit FTCS)'); plt.tight_layout()
plt.gca().figure