Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 37 ยท Ordinary Differential Equations

Systems of ODEs and Higher-Order Equations

Turn second-order equations into first-order systems and solve pendulums, springs and predator-prey models with RK4 and solve_ivp, including boundary value problems.

Real models have several unknowns that depend on each other: positions and velocities, prey and predators, concentrations of several chemicals. They are systems of first-order ODEs, y' = f(t, y) where y is now a vector. Higher-order equations such as Newton's m x'' = F fit the same mold after one trick.

Good news: every method from the last three lessons works unchanged, because Euler and Runge-Kutta only add vectors and multiply by scalars. If your code uses NumPy arrays for y, you already have a system solver.

Reducing higher-order equations

Rewrite an equation of order n as n first-order equations by introducing the derivatives as new variables. For a second-order equation x'' = g(t, x, x') set y1 = x, y2 = x':

y1' = y2 y2' = g(t, y1, y2)

Initial conditions become y1(0) = x(0) and y2(0) = x'(0). A pendulum theta'' = -(g/L) sin(theta) becomes [theta, omega]' = [omega, -(g/L) sin(theta)].

flowchart LR
  A["x'' = g(t, x, x')"] --> B["Let y1 = x<br/>and y2 = x'"]
  B --> C["y1' = y2<br/>y2' = g(t, y1, y2)"]
  C --> D["Solve with any<br/>first-order method"]

RK4 for vectors

Python RK4 on a system: the pendulum

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 energy (omega^2 / 2 - (g/L) cos(theta)) should stay constant; its tiny drift measures the numerical error. Conserved quantities are great diagnostics for any simulation.

SciPy's solve_ivp for systems

solve_ivp(fun, t_span, y0, ...) takes the same vector function. Add t_eval for evenly spaced output and dense_output=True to evaluate between steps. Events let you stop or record when a condition is met, such as a ball hitting the ground.

Python Predator-prey (Lotka-Volterra)

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 populations cycle: predators rise after prey, then crash after the prey does.

Python Events: when does the ball land?

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.

Boundary value problems

In an initial value problem all conditions are given at one time. In a boundary value problem (BVP) they are given at two places, for example the position of both ends of a beam, y(0) = 0 and y(1) = 1. Two classic approaches:

  • Shooting method: guess the missing initial slope, integrate as an IVP, see how far the end value misses, and adjust the guess with a root finder (secant or Newton, as in root finding).
  • Finite differences / collocation: discretize the whole interval into a big algebraic system and solve it at once. SciPy's solve_bvp does this.
Python A BVP with solve_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.

Some BVPs have no solution, or several. The initial guess matters a great deal, as it does for any nonlinear equation solver.

Common mistakes

  • Forgetting the initial value of the derivative variable (position and velocity are needed).
  • Returning a list of the wrong length or order from f; the order must match y0.
  • Using t_eval outside t_span.
  • Taking a fixed h for oscillating systems with Euler, which gains energy. Use RK4 or a symplectic method for long orbits.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you write x'' = -x as a first-order system?

    Let y1 = x and y2 = x'. Then y1' = y2 and y2' = x'' = -y1.
  2. 2. Which initial data does a second-order ODE need?

    Both the value and the first derivative at the start are required.
  3. 3. What does an event with terminal = True do in solve_ivp?

    Events detect sign changes of a function; terminal events end the integration there.
  4. 4. What is the shooting method?

    It turns a BVP into repeated IVPs wrapped in a root-finding loop.

Summary

  • Every higher-order ODE is a system of first-order equations by naming the derivatives as new variables.
  • Euler, RK4 and solve_ivp work unchanged on vectors.
  • Conserved quantities (energy, invariants, mass) are excellent error checks.
  • Events handle stopping conditions; BVPs are solved by shooting or by solve_bvp.

Next: One-dimensional optimization