Skip to main content

Shooting method algorithm

Short answer

Convert the boundary value problem to an initial value problem by guessing the missing initial slope s=y'(a). Integrate to x=b, compute the miss distance phi(s)=y(b)-beta, and improve s using the secant formula until |phi|<epsilon. The final IVP solution gives the boundary value problem solution.

Write an algorithm to solve two point boundary value problem using shooting method.

The shooting method converts a two-point boundary value problem

y'' = f(x, y, y'), y(a) = α, y(b) = β

into an initial value problem. The missing initial slope y'(a) = s is guessed ("the shot"), the IVP is integrated to x = b, and s is corrected until y(b) hits β.

Algorithm

  1. Write the ODE as a system: y' = z, z' = f(x, y, z).
  2. Choose two trial slopes s₀ and s₁ (for example s₀ = (β − α)/(b − a) and s₁ = s₀ + 1).
  3. For a trial slope s, solve the IVP y(a) = α, z(a) = s from a to b using RK-4 (or Euler/RK-2) with step h, and record the end value y(b; s).
  4. Form the miss-distance φ(s) = y(b; s) − β.
  5. Improve the slope by the secant formula s₂ = s₁ − φ(s₁)·(s₁ − s₀) / (φ(s₁) − φ(s₀)). (For a linear ODE φ is linear in s, so this gives the exact slope after two shots.)
  6. Set s₀ ← s₁, s₁ ← s₂ and repeat steps 3–5 until |φ(s)| < ε (tolerance, e.g. 10⁻⁴).
  7. Solve the IVP once more with the final slope; its solution values are the answer to the BVP.

Pseudo-code

READ a, b, alpha, beta, h, eps, s0, s1
phi0 ← Shoot(s0) − beta
REPEAT
    phi1 ← Shoot(s1) − beta
    IF |phi1| < eps THEN EXIT LOOP
    s2  ← s1 − phi1·(s1 − s0)/(phi1 − phi0)
    s0 ← s1;  phi0 ← phi1;  s1 ← s2
UNTIL iterations exceed limit
PRINT s1 and the table of (x, y) from Shoot(s1)

FUNCTION Shoot(s):
    x ← a;  y ← alpha;  z ← s
    WHILE x < b
        apply one RK-4 step to y' = z, z' = f(x, y, z)
        x ← x + h
    RETURN y            // value at x = b

Flow

 guess s0, s1
      |
 integrate IVP -> y(b)
      |
 |y(b) - beta| < eps ? --no--> new s (secant) --+
      |yes                                      |
   output solution   <--------------------------+

Bisection can be used instead of the secant step when two slopes giving φ of opposite signs are known.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Numerical Method chapter 6 questions →