A1236 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

A12 · Lesson 36 · Lab: separation of variables in code

Summing series solutions, finding eigenvalues and checking orthogonality

Lessons 28 to 35 solved the heat, wave and Laplace equations as series of eigenfunctions. On paper the series are exact; in practice you sum a finite number of terms, and the eigenvalues often come from equations like tan⁡k=−k/h that have no closed form. This lab turns the series into working code. You'll compute Fourier coefficients numerically, sum a heat series and solve it for a time, evaluate a Laplace series without overflowing, find Robin eigenvalues by bisection, and check that their eigenfunctions are orthogonal as Sturm–Liouville theory promises.

  • The curve is the partial sum of the sine series of a uniform starting temperature, 4π∑sin⁡nπxn over odd n, after heat has flowed for a time t (in units of L2/α): each term is multiplied by e−n2π2t.
  • At t = 0 the partial sum overshoots near the ends, by about 9% of the jump, however many terms you keep: that's the Gibbs phenomenon.
  • Raise t a little. The high terms die first, the overshoot vanishes, and three or four terms describe the profile well.

Predict

A rod with ends at 0 starts at a uniform temperature 1. At what time, in units of L2/α, has its midpoint fallen to ½?

Name it

Numerical coefficients

A coefficient like bn=2L∫0Lf(x)sin⁡nπxLdx can be computed with the trapezoid rule on a fine grid. Check the code on a case with a known answer (for f=1, bn=4nπ for odd n) before trusting it on data.

How many terms

For heat problems, term n carries a factor e−n2t/τ1, so after even a modest fraction of τ1 a handful of terms suffice. At t = 0 the series converges slowly (terms fall as 1/n for a jump), and partial sums overshoot by 8.9% of the jump near it.

Avoiding overflow

Laplace series contain ratios like sinh⁡(nπy/a)sinh⁡(nπb/a). For n of a few hundred, each sinh overflows a double, though the ratio is fine. Rewrite before computing, for example sinh⁡(x/2)sinh⁡x=12cosh⁡(x/2), or use e−nπ(b−y)/a for large n.

Eigenvalues without closed forms

With a Robin end, X(0)=0 and X′(1)+hX(1)=0, the eigenfunctions are sin⁡kx with kcos⁡k+hsin⁡k=0. Each root lies in a known interval, ((n−12)π,nπ), where the function changes sign, so bisection finds it reliably.

Worked example

How many terms of the midpoint heat series are needed for 1% accuracy at t = 0.01 (units of L2/α)?

Show the solution
  1. The terms. At the midpoint the terms are ±4nπe−n2π2t for odd n. At t = 0.01 they are 1.154, 0.175, 0.0216, 0.0014 for n = 1, 3, 5, 7.
  2. Bound the error. The series alternates and its terms shrink, so the error after stopping is less than the first term left out.
  3. Where to stop. Stopping after n = 5 leaves an error below 0.0014, well within 1% of the midpoint value of about 1.0.
  4. Compare with t = 0. At t = 0, you'd need hundreds of terms for the same accuracy near the ends.

Answer three terms (n = 1, 3 and 5) are enough.

Worked example

Find the lowest eigenvalue for X′′+λX=0 with X(0)=0 and X′(1)+X(1)=0.

Show the solution
  1. The equation for k. With X=sin⁡kx the right end gives g(k)=0, where g(k)=kcos⁡k+sin⁡k
  2. Bracket a root. At k=π/2, g=1>0, and at k=π, g=−π<0, so a root lies between.
  3. Bisect. Sixty bisection steps narrow the bracket to rounding: k1=2.02876λ1=k12=4.1159
  4. Check. That lies between the insulated-end value (π/2)2=2.47 and the fixed-end value π2=9.87, as it should: a convective end lets some heat out but not as freely as a fixed temperature.
  5. The next root. It lies between 3π/2 and 2π: k2=4.91318.

Answer λ1=4.1159, with k1=2.02876.

Check

A Fourier series of a signal with a jump of 2 is cut off after many terms. By how much does the partial sum overshoot the jump's high side?

Check

In the heat series of a uniformly started rod, how large is the n = 3 term next to the n = 1 term at time t = 0.01 (units of L2/α)?

Check

Write sine_coeff(f, n) computing bn=2∫01f(x)sin⁡nπxdx with the trapezoid rule on 20 001 points. Set b to the list of b1 to b5 for f=1.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write u_mid(t, N=50), the midpoint temperature of a rod (L=α=1) that starts at 1 with ends at 0, summed over odd n below N. Find by bisection the time t_half when it reaches ½, and set t_one to the one-term answer ln⁡(8/π)π2.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

A square box has its lid at 1 and its other sides at 0. Set center to the series value at its center, ∑n odd4nπsin⁡nπ2⋅sinh⁡(nπ/2)sinh⁡nπ, using the first 200 odd terms. Write the ratio of sinhs so that it can't overflow.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write robin_roots(h) returning the first two positive roots of kcos⁡k+hsin⁡k=0, found by bisection on (π/2,π) and (3π/2,2π). Set k1, k2 = robin_roots(1.0).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Sturm–Liouville theory says the Robin eigenfunctions sin⁡k1x and sin⁡k2x are orthogonal on (0, 1). Set overlap to ∫01sin⁡k1xsin⁡k2xdx (trapezoid rule, 20 001 points) with k1=2.028758 and k2=4.913180, and norm1 to ∫01sin2⁡k1xdx.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why do partial sums of a Fourier series overshoot near a jump, and why does the heat equation remove the overshoot?

Practice

Keep practicing

4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.