Lesson 7 of 15

Heat Equation

Heat Equation

The 1D heat equation describes how temperature u(x,t)u(x, t) diffuses through a material:

ut=α2ux2\frac{\partial u}{\partial t} = \alpha\, \frac{\partial^2 u}{\partial x^2}

where α>0\alpha > 0 is the thermal diffusivity.

Separation of Variables

For the rod x[0,L]x \in [0, L] with Dirichlet boundary conditions u(0,t)=u(L,t)=0u(0,t) = u(L,t) = 0 and initial condition u(x,0)=f(x)u(x, 0) = f(x), the solution is:

u(x,t)=n=1Bnsin ⁣(nπxL)exp ⁣(α(nπL)2t)u(x, t) = \sum_{n=1}^{\infty} B_n \sin\!\left(\frac{n\pi x}{L}\right) \exp\!\left(-\alpha\left(\frac{n\pi}{L}\right)^2 t\right)

Fourier Coefficients

The Fourier sine coefficients BnB_n are determined by projecting the initial condition onto the sine basis:

Bn=2L0Lf(x)sin ⁣(nπxL)dxB_n = \frac{2}{L} \int_0^L f(x) \sin\!\left(\frac{n\pi x}{L}\right) dx

Each mode nn decays exponentially in time with rate α(nπ/L)2\alpha(n\pi/L)^2. High-frequency modes decay fastest — this is why heat smooths out sharp features rapidly.

Numerical Integration

Given ff sampled on a uniform grid of NgridN_{\text{grid}} interior points xk=kL/Ngridx_k = k \cdot L / N_{\text{grid}} for k=1,,Ngrid1k = 1, \ldots, N_{\text{grid}} - 1, approximate the integral with the rectangle rule:

Bn2Lk=1Ngrid1f(xk)sin ⁣(nπxkL)ΔxB_n \approx \frac{2}{L} \sum_{k=1}^{N_{\text{grid}}-1} f(x_k) \sin\!\left(\frac{n\pi x_k}{L}\right) \Delta x

where Δx=L/Ngrid\Delta x = L / N_{\text{grid}}.

Example

For f(x)=sin(πx/L)f(x) = \sin(\pi x / L), only B1=1B_1 = 1 is nonzero (all other Bn=0B_n = 0), giving:

u(x,t)=sin ⁣(πxL)eα(π/L)2tu(x, t) = \sin\!\left(\frac{\pi x}{L}\right) e^{-\alpha(\pi/L)^2 t}

The temperature profile maintains its sinusoidal shape but decays uniformly.

Your Task

Implement heat_coefficient_Bn(f_values, L, n, N_grid=100) where f_values is a list of Ngrid1N_{\text{grid}}-1 interior sample values. Implement heat_solution(x, t, alpha, L, Bn_list) that evaluates the series given a precomputed list [B1,B2,][B_1, B_2, \ldots]. Implement heat_series(x, t, alpha, L, f_values, N_terms=10) that computes coefficients and evaluates the solution in one call.

Python runtime loading...
Loading...
Click "Run" to execute your code.