5.4.23 · D1Scientific Computing (Python)

Foundations — Implementing ODE solvers from scratch — Euler, RK4

1,753 words8 min readBack to topic

Before you can read a single line of Euler or RK4, you must own every symbol they use. This page builds each one from nothing — plain words, then a picture, then why the topic needs it. Read top to bottom; each block leans on the one above.


1. A function of two variables:

Picture. Imagine standing on a hillside. Your position is described by where along the ground you are () and how high up you are (). The rule is a tiny arrow pinned to that spot telling you how steeply to climb next.

Figure — Implementing ODE solvers from scratch — Euler, RK4

Why the topic needs it. We usually cannot find with algebra, but is always something we can plug numbers into and evaluate. The whole method is: read , take a step, read again. Without a computable slope there is nothing to walk along.

  • If depends only on (the arrow ignores where you are horizontally), we call the ODE autonomous.
  • If also depends on (arrows change as time moves), it is non-autonomous — and you must remember to plug in the shifted time. That is exactly the third [!mistake] on the parent page.

2. The derivative and the prime: and

Picture. Draw the solution curve. At one point, lay a ruler just touching the curve — the tangent line. Its steepness (rise over run) is at that point.

Figure — Implementing ODE solvers from scratch — Euler, RK4

Why two notations? is short and clean; visibly reminds you which variable we differentiate against (). The parent writes the ODE as — this literally says "the slope of the curve equals whatever the slope machine reports." That single equation is the whole problem.

See Finite Difference Approximation of Derivatives for how we turn this idealised slope into something a computer can compute.


3. The limit:

Picture. Pick two points on the curve a horizontal distance apart, and draw the straight line between them (a secant). As you slide the second point closer — getting tinier — that secant line pivots until it becomes the tangent. The limit is that final tangent slope.

Figure — Implementing ODE solvers from scratch — Euler, RK4

Why the topic needs it. The exact derivative is defined by the limit The computer cannot take all the way to zero — that would be dividing by nothing. So Euler's trick is to stop the limit early: pick a small-but-real and accept the secant slope as a stand-in for the tangent. The gap between "stopped early" and "true limit" is exactly the error we spend the rest of the topic chasing.


4. The step size and the index : , ,

Picture. A row of evenly spaced fence-posts along the -axis, gap between each. Post number sits at ; on top of it sits our height guess .

Figure — Implementing ODE solvers from scratch — Euler, RK4

Why the topic needs it. The whole algorithm is a loop: from produce , then repeat. The subscript notation is how we write "the next post from the current post." That is the meaning of .


5. Taylor expansion — where the update formula is born

Picture. The straight tangent is a first draft of the curve. The term is the first correction that bends the draft toward the real curve; the more terms you keep, the tighter the fit.

Why the topic needs it. Cut the series after the slope term → you get Euler, and the first thrown-away piece () is Euler's per-step error. Keep matching more terms → you get RK4, tuned so errors cancel out to the level. Full detail lives in Taylor Series Expansion.


6. Big-O notation: , ,

Order Halve → error × Reader's takeaway
(Euler global) half the error — slow
(RK4 global) sixteenth the error — fast

Why the topic needs it. The single reason RK4 beats Euler is a comparison of exponents. Without you cannot state "16× better per halving." The parent's error table is nothing but -notation made concrete.


7. The weighted average — the heart of RK4

Picture. Four compass readings taken along the hop (start, middle, middle again, end). The two middle readings are trusted more, so they sit twice as heavy on the scale.

Why the topic needs it. RK4's step is exactly height + step × (weighted-average slope). The denominator is why we divide by . This mirrors Simpson's Rule, which also weights the midpoint heavily — analogous, not identical (Simpson uses ).


Prerequisite map

function f of t and y

derivative y prime equals slope

limit as h goes to zero

step size h and index n

Taylor expansion

Euler update

RK4 update

Big-O error order

weighted average

Solving the IVP numerically

Each box is a symbol or idea from this page; the arrows show what feeds what. Notice both update rules sit on Taylor expansion, which itself sits on the derivative, which sits on the limit — the whole tower rests on Section 3.


Equipment checklist

Test yourself: can you answer each before revealing?

What does physically return?
A single number — the slope the solution should have at the point .
Autonomous vs non-autonomous ?
Autonomous depends only on ; non-autonomous also depends on , so you must plug in shifted times.
What does or mean geometrically?
The steepness of the tangent line to the solution curve at that point.
What does ask?
What value an expression approaches as shrinks toward (but never reaches) zero.
Difference between and ?
is our marching estimate; is the true (usually unknown) value — their gap is the error.
What is ?
The fixed horizontal step size — the width of one hop, with .
What does Taylor expansion give us here?
The solution rebuilt as start + slope· + curvature· + …; truncating it produces Euler and RK4.
What does mean when we halve ?
Error drops by times.
Local vs global error order?
Local is one-step error; global is the accumulated error after all steps — one power of weaker.
Why do we divide the RK4 slopes by 6?
Because the weights sum to , making it a true weighted average.