Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 39 ยท Numerical Methods

Boundary Value Problems and Shooting

Solve ODE boundary value problems numerically: the shooting method with root finding, finite-difference matrices, SciPy's solve_bvp, and checks against exact solutions.

So far every problem has been an initial value problem: all conditions are given at the starting time, and you march forward. Many real problems have conditions at two different places. A beam is clamped at both ends. The temperature of a rod is held fixed at each end. A rope hangs between two posts. These are boundary value problems (BVPs), and they are not just IVPs with extra steps: a BVP may have no solution, exactly one, or several.

This lesson shows the two classic ways to solve them numerically. The first reuses everything you know about IVP solvers. The second turns the whole problem into a system of algebraic equations. They are also the gateway to the PDE methods of the next chapter.

The shooting method

Take the second-order problem y'' = f(x, y, y') with y(a) = ฮฑ and y(b) = ฮฒ. You know y(a) but not y'(a). The idea is to guess the missing slope s, solve the resulting IVP, and see where it lands at x = b.

  1. Pick a slope s, solve the IVP y(a) = ฮฑ, y'(a) = s up to x = b.
  2. Compute the miss F(s) = y(b; s) - ฮฒ.
  3. Find the slope with F(s) = 0 using a root finder (bisection, secant, Newton, or brentq).

Like aiming a cannon: adjust the angle until the shot hits the target. The method is simple and uses any IVP solver. Its weakness is sensitivity: for some problems tiny changes in s cause huge changes at b, and the shot may even blow up before reaching b.

Python Shooting on a nonlinear BVP

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For this problem the misses change sign between the guesses, so brentq pins the slope at -8, matching the exact solution 4/(1+x)ยฒ.

Finite differences

The second approach discretizes. Choose grid points x_i = a + i h with h = (b - a)/N, and replace derivatives with difference quotients:

TEXT
y''(x_i) โ‰ˆ (y_{i-1} - 2 y_i + y_{i+1}) / hยฒ
y'(x_i)  โ‰ˆ (y_{i+1} - y_{i-1}) / (2h)

Both are second-order accurate: the error is O(hยฒ). Write the ODE at each interior point and put the boundary values in. For a linear ODE you get a linear system for the unknowns y_1, ..., y_{N-1}, and the matrix is tridiagonal, so even huge grids are solved quickly. For a nonlinear ODE the system is nonlinear and you use Newton's method.

Example: y'' = y - x with y(0) = 0, y(1) = 2. Exact solution: y = x + sinh(x)/sinh(1).

Python Finite differences and second-order convergence

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The error shrinks by a factor of about 4 each time N doubles: second-order convergence. The matrix here is dense for simplicity; real code stores only the three diagonals (see scipy.linalg.solve_banded or scipy.sparse).

Two ways to solve a BVP

Shooting Guess the slope, integrate, correct.
Finite differences Replace derivatives, solve a matrix.
Collocation What solve_bvp uses internally.

Shooting is easy to code; finite differences are more robust.

1 / 2

SciPy's solve_bvp

scipy.integrate.solve_bvp(fun, bc, x, y_guess) solves BVPs with a collocation method and adaptively refines the mesh. You provide

  • fun(x, y): the first-order system right-hand side (array shape (n, m) for m points),
  • bc(ya, yb): the residuals of boundary conditions, which should be zero when satisfied,
  • an initial mesh x and an initial guess y for the solution.

A good guess matters. Nonlinear BVPs can have several solutions, and the solver finds the one nearest to your guess.

Python solve_bvp finds both solutions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first guess converges to the exact solution with y'(0) = -8. The second guess, a parabola that dips below zero, converges to a completely different solution with y'(0) near -35.86: the solution plunges to negative values and then curves back up to meet y(1) = 1. Both satisfy the equation and both boundary conditions, which is exactly the lesson: BVPs can have several solutions and the answer reflects where you start. Always check sol.status (0 means success).

Existence and uniqueness

Unlike IVPs, BVPs offer no automatic guarantee. The linear problem y'' + y = 0, y(0) = 0, y(ฯ€) = 0 has infinitely many solutions (c sin x), while y'' + y = 0, y(0) = 0, y(ฯ€) = 1 has none. This is the same phenomenon as eigenvalue problems like the vibrating string, which you will meet in separation of variables: solutions exist only for special parameter values.

Common mistakes

  • Using a poor initial slope range for shooting, so the IVP blows up before reaching b. Bracket the root carefully.
  • Forgetting to move known boundary values to the right-hand side of the finite-difference system.
  • Giving solve_bvp a guess with the wrong shape: it must be (n, len(x)).
  • Ignoring sol.status and trusting garbage output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the shooting method, what do you adjust?

    You vary the missing initial condition until the solution hits the target boundary value.
  2. 2. The central difference (y[i-1] - 2y[i] + y[i+1]) / h^2 approximates:

    It is the standard second-order accurate approximation to the second derivative.
  3. 3. If you halve h for a second-order finite-difference method, the error drops by about:

    Error is proportional to h^2, so halving gives a factor of 4.
  4. 4. Which statement is true for BVPs?

    Existence and uniqueness are not guaranteed, unlike initial value problems.

Summary

  • Boundary value problems specify conditions at two points.
  • Shooting turns a BVP into root finding on the missing initial condition.
  • Finite differences turn it into a (tridiagonal) linear or nonlinear system with O(hยฒ) error.
  • solve_bvp uses adaptive collocation; check status and give a sensible initial guess.
  • BVPs can have zero, one or several solutions.

Next: Introduction to PDEs