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

A8 · Lesson 51 · Lab: boundary value problems

Finite differences, shooting and eigenvalues

An initial-value problem gives everything at one end and steps forward. A boundary-value problem gives conditions at both ends, so stepping from one end means guessing what you don't know there. Lesson 50 solved these by hand when a formula exists. In this lab you'll solve them numerically in the two standard ways: finite differences, which turn the equation into a tridiagonal linear system, and shooting, which guesses the missing slope and corrects it. Then you'll find a string's vibration modes as the eigenvalues of a matrix.

  • The curves are mode n of a string or line fixed at both ends, sin⁡(nπx), and its finite-difference approximation's eigenvalue against the exact (nπ)2.
  • Low modes are resolved well by a 50-point grid. Raise n: as the wavelength shrinks toward the grid spacing, the finite-difference eigenvalue falls further below the true one.
  • That's the general rule for discretized waves: a grid needs several points per wavelength.

Predict

Second-order finite differences solve y′′=−5 with y(0)=y(1)=0 on a grid of only 10 intervals. How accurate is the answer at the grid points?

Name it

Finite differences

Divide [0,L] into n intervals of width h=L/n, and replace y′′ at each interior point by

y′′(xk)≈yk+1−2yk+yk−1h2

For y′′=f(x) with known end values, that's n−1 equations in the n−1 interior values, with a tridiagonal matrix: −2/h2 on the diagonal and 1/h2 beside it. Known end values move to the right side. The error is O(h2).

Shooting

Turn the boundary-value problem into an initial-value problem with a guessed slope s at the left end, integrate to the right end, and compare with the right-end condition. The mismatch is a function of s; find its zero by bisection or the secant method. For a linear equation the mismatch is linear in s, so two shots determine the answer exactly.

Eigenvalues

−y′′=λy with y(0)=y(1)=0 becomes K𝐲=λ𝐲 for the tridiagonal K with 2/h2 on the diagonal and −1/h2 beside it. Its eigenvalues are exactly

λn(h)=4h2sin2⁡nπh2

which approach (nπ)2 as h→0 and fall below them for modes with few points per wavelength.

Worked example

Set up the finite-difference system for y′′=−5, y(0)=y(1)=0, with h=0.25.

Show the solution
  1. Unknowns. The interior points are x=0.25, 0.5 and 0.75, with unknowns y1,y2,y3.
  2. One equation per point. With y0=y4=0, each equation is yk+1−2yk+yk−10.0625=−5
  3. The system. 16[−2101−2101−2][y1y2y3]=[−5−5−5]
  4. Solve and check. The solution is (0.469,0.625,0.469), exactly 2.5x(1−x) at those points: the middle value 58=0.625 is qL28.

Answer the three equations above, whose solution (0.469,0.625,0.469) matches 2.5x(1−x) exactly.

Worked example

Solve y′′=6x with y(0)=0, y(1)=2 by shooting.

Show the solution
  1. Integrate with a guessed slope. Integrating from y(0)=0, y′(0)=s gives y=x3+sx so y(1)=1+s.
  2. The mismatch is linear. The mismatch 1+s−2 is linear in s, as it must be for a linear equation.
  3. Two shots. s=0 misses by −1 and s=2 misses by +1; interpolating gives s=1.
  4. Nonlinear equations. For a nonlinear equation the mismatch is curved, and the secant method takes a few more shots.

Answer y=x3+x.

Check

A finite-difference grid of 20 intervals on [0,1] approximates y′′. What is the diagonal entry of its matrix?

Check

What is the finite-difference estimate of the k = 2 eigenvalue of −y′′=λy on [0,1] with 5 intervals?

Check

Solve y′′=−5 on [0,1] with zero ends by finite differences, n = 10 intervals. Set err to the largest difference from the exact 2.5x(1−x) at the interior points.

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

Check

Write fd_error(n) that solves y′′=−sin⁡πx with zero ends on n intervals and returns the largest error against the exact sin⁡πxπ2. Set ratio = fd_error(10) / fd_error(20).

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

Check

Solve y′′=6x with y(0)=0 and y(1)=2 by shooting: write shoot(s) returning y(1)−2 after integrating from y(0)=0, y′(0)=s with RK4 (1000 steps), then bisect on [0,3] (50 steps) and set s_star.

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

Check

Build the 49×49 matrix for −y′′=λy on [0,1] with 50 intervals. Set lam to its three smallest eigenvalues (with np.linalg.eigvalsh) and exact to (π,2π,3π)2.

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

Check

A cooling fin obeys y′′=m2y with y(0)=1 (the base) and y′(1)=0 (an insulated tip), with m = 2. Solve it by finite differences with 100 intervals, using a ghost point yn+1=yn−1 for the tip, and set tip to y(1).

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

Explain why

Why does a finite-difference grid underestimate the high modes' eigenvalues so much more than the low ones'?

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.