4.8.27 · D3Numerical Methods

Worked examples — Systems of ODEs — RK4 for systems

2,738 words12 min readBack to topic

Before any example, one reminder of the machinery we reuse every time.

Here "vector" just means an ordered list of numbers, one per unknown. If we track two quantities , then is a point in a plane, and hands back another such pair — the direction and speed each quantity is currently moving. That is all the notation means.


The scenario matrix

Every problem this topic can throw at you falls into one of these cells. The worked examples below are each tagged with the cell(s) they cover, so that together they fill the whole grid.

Cell What makes it different Example
A. Coupled, mixed signs each depend on the other variable, opposite signs (rotation) Ex 1
B. Decoupled system equations don't reference each other — a sanity check Ex 2
C. Reduced 2nd-order a single higher-order ODE turned into a system Ex 3
D. Nonlinear word problem products of unknowns, real units (predator–prey) Ex 4
E. Degenerate / steady state initial state where (nothing should move) Ex 5
F. All-positive growth both variables grow, positive slopes throughout Ex 6
G. Limiting / stiff twist a component decays so fast that step size matters Ex 7
H. Non-autonomous depends on explicitly, not just on Ex 8

The columns of "sign behaviour" (both slopes negative, both positive, mixed, zero) and the rows of "structure" (coupled / decoupled / reduced / nonlinear / stiff / time-dependent) are all touched. The figure below plots four of these cells as motions in state space, so you can see how different the scenarios are before we grind through the arithmetic.

Figure — Systems of ODEs — RK4 for systems

Read the figure like a map: the blue arc (Cell A, Ex 1) is a point curving on a circle — mixed signs make it rotate; the orange arrow (Cell B, Ex 2) shows one coordinate shrinking while the other grows — a decoupled system; the green curve (Cell F, Ex 6) is both coordinates racing outward together — pure growth; the red dot (Cell E, Ex 5) sits frozen at a fixed point — nothing moves. Keep this picture in mind as each example below traces one of these paths.


Ex 1 — Cell A: coupled rotation, mixed signs

Forecast: these are the equations of a point spinning on a circle (position and velocity feeding each other with opposite signs). After a small step should dip slightly below and should go slightly negative. Since RK4 is order 4, expect ~4 correct digits.

Here . This is the blue arc in the figure above.

  1. . Why this step? The slope at the current state — this is where is heading right now.
  2. Midpoint state , so . Why this step? Re-measure the slope at the predicted middle of the step — both components use the advanced state.
  3. , so . Why this step? A refined midpoint slope, correcting .
  4. , so . Why this step? The end-of-interval slope.
  5. Combine with weights over : Why this step? We now blend the four slope vectors into one best estimate of the step's average velocity (middle slopes doubled — Simpson weighting) and move by that average times . This is the actual advance of the state.

Verify: (matches to ~5 decimals), (matches to ~4 decimals). Agreement in one step. Forecast confirmed.


Ex 2 — Cell B: decoupled sanity check

Forecast: the two equations never mention each other. RK4 on a decoupled system must reproduce, component-by-component, exactly what scalar RK4 gives on each equation separately. shrinks (negative slope), grows fast (slope ). This is the orange arrow in the figure above.

Here .

  1. . Why? Slope at .
  2. State , . Why? Midpoint slope.
  3. State , . Why? Refined midpoint.
  4. State , . Why? End slope.
  5. Combine: Why this step? Each component gets its own blend — since the equations are decoupled, this is literally scalar RK4 done twice in parallel.

Verify: (error ), (error ). The larger growth rate () makes a bigger step per unit , so its error is larger — expected. Both are the same numbers you'd get running scalar RK4 on each line alone.


Ex 3 — Cell C: reduce a 2nd-order ODE

Forecast: this is the reduction trick. With the exact solution is , so should track .

Set . Then so , starting at .

  1. . Why? Slope at : , .
  2. State , . Why? Midpoint slope — swap the components.
  3. State , . Why? Refined midpoint.
  4. State , . Why? End slope.
  5. Combine (both components identical here since throughout): Why this step? Blend the four slopes with the fixed weights to advance (and ) by one step; the reduction changed nothing about this averaging.

Verify: ; error . The 2nd-order ODE was solved by the same systems recipe — the reduction cost us nothing.


Ex 4 — Cell D: nonlinear predator–prey word problem

Forecast: the products make this nonlinear — the coupling is multiplicative. At the start and : rabbits momentarily steady, foxes rising. So expect (nearly flat) and up to about .

Here , state .

  1. . Why? Current slopes.
  2. State ; . Why? Midpoint slope, both populations advanced.
  3. State ; , so . Why? Refined midpoint.
  4. State ; , so . Why? End slope.
  5. Combine: Why this step? Even though is nonlinear, the averaging step is unchanged — we still weight the four slope vectors over . Nonlinearity only affects how each was computed, not how they are blended.

Verify: rabbits thousand (barely dropped, as forecast — they were near their peak), foxes thousand (rose, as forecast). Units are thousands of animals, consistent throughout; no negative populations appeared.


Ex 5 — Cell E: degenerate steady state

Forecast: at we get and — the field is exactly zero. A steady state means nothing should move: the answer must be again, to machine precision. This is the degenerate case that catches sloppy code — the red dot in the figure above.

  1. . Why? Slope at the fixed point is zero by definition.
  2. State ; . Why? We never left , so the slope is still zero.
  3. State ; . Why? Same reasoning.
  4. State ; . Why? Same.
  5. Combine: . Why this step? The blend of four zero vectors is zero, so the average velocity is zero and the state does not advance — exactly what an equilibrium demands.

Verify: the state is unchanged — RK4 respects equilibria exactly (since every ). If your program had "advanced one component alone," it might have nudged off the fixed point; the synchronized recipe does not.


Ex 6 — Cell F: all-positive growth

Forecast: with and the field pointing "each component grows at the other's value," both slopes are positive throughout — pure exponential growth. Expect both to reach . This is the green curve in the figure above.

Here , state .

  1. . Why? Slope at start.
  2. State , . Why? Midpoint.
  3. State , . Why? Refined midpoint.
  4. State , . Why? End slope.
  5. Combine (both components identical): Why this step? Weight the four positive slopes over and step forward; the doubled midpoints keep the exponential's accelerating curve accurate.

Verify: ; error . Both slopes stayed positive as forecast.


Ex 7 — Cell G: limiting / stiff twist

Forecast: the true solution is ; at that's . But the fast component decays with rate , and is large. RK4's stability limit for needs roughly . With we are outside it — expect the estimate to blow past reason (possibly wrong sign or magnitude), while (rate , ) stays fine.

Compute (a scalar RK4 with , , so ). For a linear , one RK4 step multiplies by

  1. . Why? The step's stability argument.
  2. . Why? Plug into the polynomial.
  3. So . Why? One step scales the state by — this factor is the combined effect of the four-slope blend for a linear field.
  4. For : , . Why? Small , safely stable.

Verify: true but RK4 gave — a catastrophic overshoot (wrong by a factor , and so it will grow each step). gave vs true — perfect. Fix: shrink so for the stiffest rate, i.e. , or use an implicit method. Explicit RK4 is limited by the fastest component.


Ex 8 — Cell H: non-autonomous (explicit )

Forecast: now mentions explicitly, so the and arguments actually matter — this is the case where forgetting to advance time breaks things. Start slope , : position rising, velocity flat. Because the exact answer is the straight line , RK4 should return and with essentially no error.

Here , state at .

  1. . Why? Slopes at , state .
  2. , state ; . Why? Midpoint — note we used , not .
  3. State ; . Why? Refined midpoint at .
  4. , state ; . Why? End, at .
  5. Combine: Why this step? The blend of the four slopes gives average velocity , so we step by — but only because we evaluated each at the correct time.

Verify: the exact solution is (check: , so ✓; , ✓), giving and . RK4 returned exactly and — zero error. This is because we advanced correctly in each stage; had we frozen , the term would be evaluated at the wrong time and the answer would be wrong.


Recall

Recall Quick self-test

Why did Ex 5 stay exactly put? ::: At the fixed point , so all four vanish and the weighted average adds nothing. Why did Ex 7 return instead of near ? ::: The RK4 amplification at equals , so the step amplifies instead of decaying — we were outside the stability region. In Ex 8, what would break if you kept in every stage? ::: The term would be evaluated at the wrong time, giving wrong 's and losing the exact answer.


Connections

  • Parent: RK4 for systems
  • RK4 for a single ODE — Ex 2 shows the decoupled case reduces to this.
  • Reducing higher-order ODEs to first-order systems — Ex 3, Ex 8.
  • Euler's method for systems — the cheaper cousin; try these examples with it to see the accuracy gap.
  • Local vs Global Truncation Error — why bigger rates in Ex 2 gave bigger error.
  • Stiff systems and stability — the failure in Ex 7.
  • Simpson's Rule — the quadrature RK4 imitates.