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 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, over odd n, after heat has flowed for a time t (in units of ): each term is multiplied by .
- 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 , has its midpoint fallen to ½?
0.0947. The first term alone gives , so ; the full series moves that by only , because by then the n = 3 term has shrunk by relative to the first.
Name it
Numerical coefficients
A coefficient like can be computed with the trapezoid rule on a fine grid. Check the code on a case with a known answer (for , for odd n) before trusting it on data.
How many terms
For heat problems, term n carries a factor , so after even a modest fraction of a handful of terms suffice. At t = 0 the series converges slowly (terms fall as for a jump), and partial sums overshoot by 8.9% of the jump near it.
Avoiding overflow
Laplace series contain ratios like . For n of a few hundred, each sinh overflows a double, though the ratio is fine. Rewrite before computing, for example , or use for large n.
Eigenvalues without closed forms
With a Robin end, and , the eigenfunctions are with . Each root lies in a known interval, , 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 )?
Show the solution
- The terms. At the midpoint the terms are for odd n. At t = 0.01 they are 1.154, 0.175, 0.0216, 0.0014 for n = 1, 3, 5, 7.
- Bound the error. The series alternates and its terms shrink, so the error after stopping is less than the first term left out.
- Where to stop. Stopping after n = 5 leaves an error below 0.0014, well within 1% of the midpoint value of about 1.0.
- 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 with and .
Show the solution
- The equation for k. With the right end gives , where
- Bracket a root. At , , and at , , so a root lies between.
- Bisect. Sixty bisection steps narrow the bracket to rounding:
- Check. That lies between the insulated-end value and the fixed-end value , as it should: a convective end lets some heat out but not as freely as a fixed temperature.
- The next root. It lies between and : .
Answer , with .
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 )?
Check
Write sine_coeff(f, n) computing with the trapezoid rule on 20 001 points. Set b to the list of to for .
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 () 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 .
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, , 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 , found by bisection on and . 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 and are orthogonal on (0, 1). Set overlap to (trapezoid rule, 20 001 points) with and , and norm1 to .
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?
A partial sum with N terms is the true function smoothed by the Dirichlet kernel, a narrow oscillating bump whose width shrinks as and which has negative side lobes. Near a jump, sliding that kernel across the step gives a ripple whose largest peak overshoots by about 9% of the jump; adding terms squeezes the ripple closer to the jump but doesn't lower its peak. The heat equation multiplies term n by , which smooths the coefficients' sharp cutoff into a gentle roll-off. Smoothing a step with a positive kernel can't overshoot, so once the high terms have decayed the ripple is gone.
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.