TExcelTAD
Home › Numerical Methods › The Shooting Method

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 u′′=f(x,u,u′)u'' = f(x, u, u'), u(a)=αu(a) = \alpha, u(b)=βu(b) = \beta: let v(x;s)v(x; s) solve the IVP with u(a)=αu(a) = \alpha, u′(a)=su'(a) = s. We need the slope ss with

F(s)=v(b;s)−β=0,F(s) = v(b; s) - \beta = 0,

a scalar root-finding problem (Rootfinding in Practice). Each evaluation of FF is one IVP solve.

python · try it yourself
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 −9-9 and −7-7, so brentq finds the slope; the exact solution u=4(1+x)2u = \frac{4}{(1 + x)^2} has u′(0)=−8u'(0) = -8.

Linear problems need only two shots

For a linear ODE, v(b;s)v(b; s) depends linearly on ss, so two IVP solves determine the right slope exactly: s∗=s0+(s1−s0)β−v(b;s0)v(b;s1)−v(b;s0)s^* = s_0 + (s_1 - s_0)\frac{\beta - v(b; s_0)}{v(b; s_1) - v(b; s_0)}.

Multiple solutions

A nonlinear BVP can have several solutions, one for each root of FF. The same problem has a second solution with a much steeper initial slope:

python · try it yourself
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, v(b;s)v(b; s) is extremely sensitive to ss (it may even overflow), and shooting fails. For u′′=400uu'' = 400u on [0,1][0, 1], an error of 10−1610^{-16} in ss grows by e20≈5×108e^{20} \approx 5 \times 10^8.
  • 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_bvp uses 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 u′′+λu=0u'' + \lambda u = 0 is in Boundary Value Problems.

Exercises

Exercise 1: Set up the shooting function

For u′′=−uu'' = -u, u(0)=0u(0) = 0, u(π/2)=2u(\pi/2) = 2, find the shooting function F(s)F(s) exactly and its root.

Answer

F(s)=s−2F(s) = s - 2; s∗=2s^* = 2.

Solution

The IVP solution is v=ssin⁡xv = s\sin x, so v(π/2)=sv(\pi/2) = s and F(s)=s−2F(s) = s - 2.

Exercise 2: Sensitivity

For u′′=100uu'' = 100u on [0,1][0, 1], by roughly what factor does an error in the initial slope get multiplied at x=1x = 1?

Answer

About e1020≈1100\frac{e^{10}}{20} \approx 1100.

Solution

The slope's contribution is s10sinh⁡(10x)\frac{s}{10}\sinh(10x), and sinh⁡1010≈1101\frac{\sinh 10}{10} \approx 1101.

Exercise 3: Linear shooting with two shots (code)

Solve u′′=−u+xu'' = -u + x, u(0)=0u(0) = 0, u(1)=2u(1) = 2 by two shots with s=0s = 0 and s=1s = 1 and the linear combination formula; compare with the exact u(x)=x+sin⁡xsin⁡1u(x) = x + \frac{\sin x}{\sin 1} at x=0.5x = 0.5.

Answer

Both give u(0.5)≈1.069747u(0.5) \approx 1.069747.

Solution
python · try it yourself
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…

  1. an eigenvalue problem
  2. a root-finding problem for the unknown initial data
  3. a linear system only
  4. an integral

For a linear BVP, how many IVP solves does shooting need?

  1. one
  2. two
  3. about ten
  4. it depends on the tolerance

Shooting fails when…

  1. the BVP is linear
  2. the IVP has rapidly growing solutions that amplify errors in ss
  3. the interval is short
  4. the solution is smooth