---
title: Boundary-value problems
heading: Shooting and finite differences for conditions at both ends
eyebrow: A13 · Lesson 44 · Boundary-value problems
xp: 40
---

::: hook
The ODE solvers so far start from known values at one point and march forward. Many engineering problems instead fix conditions at both ends: a cooling fin's temperature at its base and its insulated tip, the potential at both contacts of a diode, a beam supported at both ends. These **boundary-value problems** need different tools. **Shooting** guesses the missing starting slope, integrates, and corrects the guess until the far end comes out right. **Finite differences** replace the derivatives with differences at every grid point at once and solve the resulting banded system. Each has a regime where the other fails.
:::

::: play
- The curve is the exact solution of $y'' = k^2y$ with $y(0) = 1$ and $y(1) = 0$.
- Raise k: the solution hugs zero except near x = 0. The first readout shows how much a slope error at x = 0 grows by x = 1 when you shoot; it passes a thousand at k = 10.
- The second readout is the diagonal of the finite-difference system with h = ¼. It stays above 2, so the system stays diagonally dominant and easy to solve, however large k gets.
:::

```sim plot
view: graph
x: x = 0..1
y: y = -0.1..1.1
slider: k = 0.5..10 step 0.5 = 2
plot: sinh(k*(1 - x))/sinh(k) | label: exact | samples: 300
readout: shooting's error growth across the interval = sinh(k)/k | digits: 4
readout: finite-difference diagonal for a step of a quarter = 2 + k^2*0.0625 | digits: 4
```

::: predict
## Shooting solves $y'' = 100y$ on [0, 1] by integrating from x = 0 with a guessed slope. A slope error of $10^{-8}$ at the start becomes how large an error at x = 1?

- [ ] About $10^{-8}$
- [ ] About $10^{-6}$
- [x] About $10^{-5}$
- [ ] About 1

::: reveal
About $1.1 \times 10^{-5}$: a slope error δ grows to $\delta\frac{\sinh 10}{10} = 1101\delta$. The equation has a growing solution $e^{10x}$, and integrating from one end amplifies every error along it. With stiffer equations (larger k), shooting loses all accuracy, while finite differences, which treat both ends at once, stay well behaved.
:::
:::

::: nameit
## Shooting

For $y'' = f(x, y, y')$ with $y(a) = \alpha$ and $y(b) = \beta$: guess the slope s at a, integrate the initial-value problem to b, and call the miss $F(s) = y(b; s) - \beta$. Then solve $F(s) = 0$ with the secant method or Newton's method. For a **linear** ODE, $y(b; s)$ is linear in s, so two shots determine s exactly.

Shooting reuses good IVP solvers, but if the ODE has rapidly growing solutions the miss is extremely sensitive to s, and errors explode. Multiple shooting (restarting from several points) tames that.

## Finite differences

Put N interior points with spacing h and replace $y''$ by $\frac{y_{i+1} - 2y_i + y_{i-1}}{h^2}$ at each. For $y'' = k^2y$ that gives $y_{i-1} - (2 + k^2h^2)y_i + y_{i+1} = 0$, a tridiagonal system solved in O(N) operations (lesson 24). The error is $O(h^2)$, and Richardson extrapolation lifts it to $O(h^4)$. Nonlinear problems give nonlinear algebraic systems, solved by Newton's method with a tridiagonal Jacobian.

## Boundary conditions

Fixed values go straight into the equations' right sides. A slope condition like $y'(L) = 0$ (an insulated fin tip) uses a ghost point $y_{N+1} = y_{N-1}$ to keep second-order accuracy.

## Where they come from

Fins and heat sinks ($T'' = m^2(T - T_\text{air})$), cables and beams, carrier diffusion in a semiconductor ($n'' = n/L^2$), the potential across a junction (Poisson's equation, nonlinear), and steady temperature in a wire with resistive heating.
:::

::: example
## Solve $y'' = y$ with $y(0) = 0$ and $y(1) = 1$ by linear shooting.

::: solution
1. **One shot.** Starting with $y(0) = 0$ and slope s, the solution is $y = s\sinh x$, so
   $$y(1) = s\sinh 1$$
   One shot with s = 1 gives $y(1) = 1.1752$.
2. **Scale.** The problem is linear, so
   $$\begin{aligned} s &= \frac{1}{1.1752} \\ &= 0.8509 \end{aligned}$$
3. **The solution.**
   $$y = \frac{\sinh x}{\sinh 1}$$
4. **Nonlinear problems.** For a nonlinear equation, the secant method on the miss does the same job in a few iterations, each one a full IVP solve.

**Answer:** the starting slope is 0.8509, and the solution is $y = \frac{\sinh x}{\sinh 1}$.
:::
:::

::: example
## Solve $y'' = 4y$ with $y(0) = 1$ and $y(1) = 0$ by finite differences with h = 0.25.

::: solution
1. **The difference equations.** At the three interior points
   $$\begin{aligned} &y_{i-1} - (2 + 4 \times 0.0625)y_i \\ &\quad + y_{i+1} = 0 \end{aligned}$$
   so the diagonal is 2.25.
2. **Back-substitute.** With $y_0 = 1$ and $y_4 = 0$:
   $$\begin{aligned} y_2 &= 2.25y_3 \\ y_1 &= 4.0625y_3 \\ 1 &= 6.8906y_3 \end{aligned}$$
3. **Solve.**
   $$\begin{aligned} y_3 &= 0.1451 \\ y_2 &= 0.3265 \\ y_1 &= 0.5896 \end{aligned}$$
4. **Compare with the exact values.** $\frac{\sinh 2(1 - x)}{\sinh 2}$ gives 0.5871, 0.3240 and 0.1437: errors of about 0.0025, from a grid of only three points.
5. **A finer grid.** Halving h would cut them to about 0.0006.

**Answer:** $y_1 = 0.5896$, $y_2 = 0.3265$ and $y_3 = 0.1451$, within about 0.0025 of the exact values.
:::
:::

::: example
## Why does shooting fail on $y'' = 100y$ while finite differences succeed?

::: solution
1. **The general solution.** It is $Ae^{10x} + Be^{-10x}$.
2. **What a slope error does.** Shooting from x = 0 with a slope error δ adds
   $$\delta\frac{\sinh 10x}{10}$$
   which reaches $1101\delta$ at x = 1.
3. **Worse still.** For $y'' = 10^4y$ the factor is $10^{41}$, far beyond double precision, so no slope can be found.
4. **Finite differences.** The system
   $$y_{i-1} - (2 + 100h^2)y_i + y_{i+1} = 0$$
   is diagonally dominant, so elimination is stable and every unknown is computed together, with both boundary values in view.
5. **Why that works.** The growing and decaying solutions are each pinned at the end where they're small.

**Answer:** shooting multiplies any slope error by the growing exponential (1101 times here, $10^{41}$ times for $y'' = 10^4y$), while the finite-difference system is diagonally dominant and pins both ends at once.
:::
:::

::: check
given: k = 2 | 3 | 4; B = 1 | 5
answer: B*k/sinh(k)
tolerance: 0.5%

## Shooting solves $y'' = \given{k}^2y$ with $y(0) = 0$ and $y(1) = \given{B}$. What starting slope hits the target?

::: right
Right. $y = s\frac{\sinh kx}{k}$, so $s = \frac{Bk}{\sinh k}$.
:::

::: misses
- [B/sinh(k)] The slope of $\sinh kx$ at 0 is k.
- [any] Solve $s\frac{\sinh k}{k} = B$.
:::
:::

::: check
given: k = 2 | 5; h = 0.1 | 0.25
answer: 2 + k^2*h^2
tolerance: 0.5%

## Finite differences on $y'' = \given{k}^2y$ with h = {h}: what is the diagonal of the system $y_{i-1} - dy_i + y_{i+1} = 0$?

::: right
Right. $2 + k^2h^2$: always above 2, so the system is diagonally dominant.
:::

::: misses
- [2 - k^2*h^2] Move $k^2h^2y_i$ to the left with the −2.
- [any] Multiply $\frac{y_{i+1} - 2y_i + y_{i-1}}{h^2} = k^2y_i$ by $h^2$.
:::
:::

::: check
given: k = 5 | 10 | 20
answer: sinh(k)/k
tolerance: 0.5%

## Shooting on $y'' = \given{k}^2y$ over [0, 1]: by what factor does a starting slope error grow by x = 1?

::: right
Right. $\frac{\sinh k}{k}$: about $\frac{e^k}{2k}$ for large k.
:::

::: misses
- [exp(k)] Close for large k; exactly it's $\frac{\sinh k}{k}$.
- [any] The perturbation solves the same equation with zero start and unit slope.
:::
:::

::: check
kind: parts
given: e = 0.004 | 0.01; r = 2 | 4

## A second-order finite-difference solution has error {e} at a given point.

::: part
answer: e/r^2
tolerance: 0.5%

### What error do you expect with h divided by {r}?

::: misses
- [e/r] The error goes as $h^2$.
- [any] $e/r^2$.
:::
:::

::: part
answer: r^2
tolerance: 0.5%

### And the number of points grows by about what factor?

::: misses
- [any] r: the cost is linear in the number of points, so error falls as the square of the cost.
:::
:::
:::

::: check
given: h = 0.5 | 0.25; y = 1 | 2
answer: -2/h^2 - exp(y)
tolerance: 0.5%

## Newton's method on the finite-difference form of $y'' = e^y$. What is a diagonal entry of the Jacobian at a point where $y_i$ = {y}, with h = {h}?

::: right
Right. Differentiate $\frac{y_{i+1} - 2y_i + y_{i-1}}{h^2} - e^{y_i}$ with respect to $y_i$.
:::

::: misses
- [-2/h^2] Include the derivative of $e^{y_i}$.
- [any] $-\frac{2}{h^2} - e^{y_i}$.
:::
:::

::: check
given: m = 10 | 30; L = 0.02 | 0.05
answer: 1/cosh(m*L)
tolerance: 0.5%

## A fin with $T'' = m^2T$ (T measured from the air temperature), m = {m} m⁻¹, length {L} m, base at T = 1 and an insulated tip. What is the tip temperature?

::: right
Right. $T = \frac{\cosh m(L - x)}{\cosh mL}$, so the tip sits at $\frac{1}{\cosh mL}$.
:::

::: misses
- [exp(-m*L)] That's an infinitely long fin; the insulated tip keeps it warmer.
- [any] Solve with $T(0) = 1$ and $T'(L) = 0$.
:::
:::

::: check
kind: choice

## When is shooting a poor choice for a two-point boundary-value problem?

- [ ] When the ODE is linear
- [x] When the ODE has solutions that grow rapidly across the interval
- [ ] When the boundary conditions are fixed values
- [ ] When an accurate IVP solver is available

::: right
Right. Integrating from one end amplifies every error along the growing solutions; finite differences or multiple shooting avoid that.
:::

::: misses
- [1] Linear problems are where shooting is easiest: two shots solve them.
- [3] Fixed values are the simplest conditions for both methods.
- [4] Shooting relies on a good IVP solver; that's a reason to use it.
:::
:::

::: check
kind: derivation
vars: k = 0.5..5, x = 0..1

## Solve $y'' = k^2y$ with $y(0) = 0$ and $y(1) = 1$.

::: step
answer: sinh(k*x)/sinh(k)

### What is y(x)?
:::

::: step
answer: k/sinh(k)

### What slope does it start with, the one shooting must find?
:::

::: right
Right. For large k the starting slope is tiny, about $2ke^{-k}$: a small number that shooting must find to many digits.
:::
:::

::: explain
## Why do finite differences handle boundary-value problems with growing and decaying solutions better than shooting?

::: model
Shooting turns the problem into an initial-value problem integrated from one end, so any error at the start is carried across the whole interval by the ODE, and growing solutions magnify it enormously. Finite differences write one equation per grid point and solve them all together, with the conditions at both ends included from the start. Each solution component is effectively determined from the end where it is small: a growing exponential is pinned by its value at the far end, a decaying one by the near end. The resulting matrix is banded and diagonally dominant, so elimination is stable, and no error has to travel across the interval unchecked.
:::
:::

::: practice

::: check
given: k = 0.5 | 4; B = 2 | 3
answer: B*k/sinh(k)
tolerance: 0.5%

## Shooting slope for $y'' = \given{k}^2y$, $y(1) = \given{B}$?

::: misses
- [any] $\frac{Bk}{\sinh k}$.
:::
:::

::: check
given: m = 20 | 50; L = 0.01 | 0.04
answer: 1/cosh(m*L)
tolerance: 0.5%

## Insulated fin tip temperature, m = {m}, L = {L}?

::: misses
- [any] $\frac{1}{\cosh mL}$.
:::
:::
:::
