The Shooting Method
A boundary value problem (BVP) prescribes conditions at two ends of an interval: the deflection of a beam fixed at both ends, the temperature in a rod held at two temperatures, the trajectory that hits a target. IVP solvers cannot be applied directly, because the initial slope is unknown. The shooting method guesses it, integrates, measures the miss at the far end, and corrects the guess with a root finder.
The idea
For , , : let solve the IVP with , . We need the slope with
a scalar root-finding problem (Rootfinding in Practice). Each evaluation of is one IVP solve.
import numpy as np
from scipy.integrate import solve_ivp
from scipy.optimize import brentq
# a nonlinear BVP: u'' = 1.5 u^2, u(0) = 4, u(1) = 1 (one solution is u = 4/(1+x)^2)
def rhs(x, y):
return [y[1], 1.5 * y[0] ** 2]
def miss(s):
sol = solve_ivp(rhs, (0, 1), [4.0, s], rtol=1e-11, atol=1e-11)
return sol.y[0, -1] - 1.0
for s in (-10, -9, -8, -7):
print(f"slope {s:4d}: u(1) misses by {miss(s):+.4f}")
s_star = brentq(miss, -9, -7)
print(f"shooting slope s* = {s_star:.10f} (exact -8)")slope -10: u(1) misses by -3.4008
slope -9: u(1) misses by -1.9581
slope -8: u(1) misses by +0.0000
slope -7: u(1) misses by +2.6442
shooting slope s* = -8.0000000000 (exact -8)
The miss changes sign between and , so brentq finds the slope; the exact solution has .
Linear problems need only two shots
For a linear ODE, depends linearly on , so two IVP solves determine the right slope exactly: .
Multiple solutions
A nonlinear BVP can have several solutions, one for each root of . The same problem has a second solution with a much steeper initial slope:
import numpy as np
from scipy.integrate import solve_ivp
from scipy.optimize import brentq
def miss(s):
sol = solve_ivp(lambda x, y: [y[1], 1.5 * y[0] ** 2], (0, 1), [4.0, s], rtol=1e-11, atol=1e-11)
return sol.y[0, -1] - 1.0 if sol.success and sol.t[-1] == 1 else np.nan
s_grid = np.linspace(-45, -30, 61)
F = np.array([miss(s) for s in s_grid])
i = np.where(np.sign(F[:-1]) * np.sign(F[1:]) < 0)[0]
roots = [brentq(miss, s_grid[k], s_grid[k + 1]) for k in i]
print("slopes giving u(1) = 1 in [-45, -30]:", np.round(roots, 6))
sol = solve_ivp(lambda x, y: [y[1], 1.5 * y[0] ** 2], (0, 1), [4.0, roots[0]], dense_output=True, rtol=1e-11)
print(f"this solution dips to u = {sol.sol(np.linspace(0, 1, 201))[0].min():.4f}")slopes giving u(1) = 1 in [-45, -30]: [-35.858549]
this solution dips to u = -10.6901
Weaknesses
- Instability: if the IVP has rapidly growing solutions, is extremely sensitive to (it may even overflow), and shooting fails. For on , an error of in grows by .
- Multiple shooting splits the interval into pieces, shoots on each, and matches the pieces, which limits the growth. It leads to a larger nonlinear system solved by Newton's method.
- Finite differences discretize the whole interval at once and avoid the issue (Finite Differences for BVPs).
In software
scipy.integrate.solve_bvpuses collocation (a global method related to finite differences) with Newton iterations and mesh refinement; it needs an initial guess for the whole solution, not only the slope.- The theory of BVPs and eigenvalue problems such as is in Boundary Value Problems.
Exercises
Exercise 1: Set up the shooting function
For , , , find the shooting function exactly and its root.
Answer
; .
Solution
The IVP solution is , so and .
Exercise 2: Sensitivity
For on , by roughly what factor does an error in the initial slope get multiplied at ?
Answer
About .
Solution
The slope's contribution is , and .
Exercise 3: Linear shooting with two shots (code)
Solve , , by two shots with and and the linear combination formula; compare with the exact at .
Answer
Both give .
Solution
import numpy as np
from scipy.integrate import solve_ivp
rhs = lambda x, y: [y[1], -y[0] + x]
shot = lambda s: solve_ivp(rhs, (0, 1), [0.0, s], dense_output=True, rtol=1e-11, atol=1e-12)
v0, v1 = shot(0.0), shot(1.0)
s = 0 + (2 - v0.y[0, -1]) / (v1.y[0, -1] - v0.y[0, -1])
print(f"{shot(s).sol(0.5)[0]:.6f} {0.5 + np.sin(0.5) / np.sin(1):.6f}")1.069747 1.069747
Test yourself
The shooting method turns a BVP into…
- an eigenvalue problem
- a root-finding problem for the unknown initial data
- a linear system only
- an integral
For a linear BVP, how many IVP solves does shooting need?
- one
- two
- about ten
- it depends on the tolerance
Shooting fails when…
- the BVP is linear
- the IVP has rapidly growing solutions that amplify errors in
- the interval is short
- the solution is smooth