5.4.23 · D5Scientific Computing (Python)
Question bank — Implementing ODE solvers from scratch — Euler, RK4
True or false — justify
Euler and RK4 both need a known starting point .
True — they are IVP solvers; without a starting value there is nothing to step forward from, since each step reads the slope at the current point. See scipy.integrate.solve_ivp which also demands
y0.Halving in Euler roughly halves the global error.
True — Euler's global error is , so error scales linearly with ; cut in two and error drops by about .
Halving in RK4 roughly halves the global error.
False — RK4 is , so halving cuts error by about , not .
RK4 always gives the exact answer.
False — it is fourth-order accurate, not exact; it discards the local terms and still accumulates global error unless the true solution is a polynomial of degree that RK4 can integrate exactly.
RK4 costs four times as much work per step as Euler.
True in function evaluations — RK4 calls four times per step vs Euler's one, but this is the wrong metric: at equal accuracy RK4 needs vastly fewer steps and wins overall.
For (no on the right), you may skip the -shifts in RK4's stages.
False — depends on here (non-autonomous), so must use and must use ; skipping them corrupts the answer.
RK4's weights are literally Simpson's weights.
False — Simpson uses over three points; RK4 uses over four stages, with two midpoint evaluations whose weights sum to — analogous, not identical. See Simpson's Rule.
Euler's method is the same as a forward finite difference.
True — rearranging the forward difference gives exactly the Euler update. See Finite Difference Approximation of Derivatives.
A larger step size always makes any explicit method faster with only a small accuracy cost.
False — beyond a stability limit, large makes explicit Euler and RK4 blow up (oscillate to infinity), especially on stiff problems. See Numerical Stability and Stiff ODEs.
Spot the error
"I'll reuse to build since I already have it: ."
Wrong — must use the previous stage : . The stages are a chain; reusing collapses RK4 to a lower order and silently destroys the accuracy.
"The final RK4 update is — four slopes, so divide by 4."
Wrong — the weights are (sum ), so you divide by : . A plain average by 4 gives the wrong weighted mean and loses accuracy.
"Euler samples the slope at the midpoint of each interval."
Wrong — Euler samples only at the left endpoint and assumes it holds all the way across; sampling at the midpoint would be a different (better) method.
" — I add half a step to ."
Wrong — you add , not ; the nudge to must be scaled by the slope , since changes by (slope × step), not by the step alone.
"For , , one Euler step of gives ."
Wrong — Euler gives , an error of ; only the true solution equals , and Euler's straight-line hop undershoots the curving exponential.
"RK4 has local error ."
Wrong — RK4's local truncation error is ; the global error (accumulated over steps) is . Don't confuse the two.
Why questions
Why does Euler consistently drift off when the true curve bends?
Because it trusts a single left-endpoint slope for the whole step; if the curve bends upward the straight line lies below it (and vice versa), so errors accumulate in one direction rather than cancelling.
Why does RK4 sample the midpoint twice ( and )?
The midpoint best represents the average slope over the interval; a first midpoint estimate (built from ) is refined by a second (built from ), and giving each weight matches the Taylor series through .
Why are the RK4 weights forced to be exactly ?
Because expanding the RK4 update as a Taylor series and demanding it agree with the true up to the term produces these coefficients uniquely — they are not a free choice. See Taylor Series Expansion.
Why is RK4 the practical "workhorse" despite four -calls per step?
At a target error , Euler cost scales like while RK4 scales like ; for small RK4 needs astronomically fewer total evaluations.
Why must we discretize at all instead of solving the ODE exactly?
Most real have no closed-form integral, but the slope itself is always computable, so we walk forward in small computable hops.
Why does dividing by (not the number of stages) matter?
Because is a genuine weighted mean slope (weights sum to ) times ; any other divisor would mis-scale the step and break the order.
Edge cases
What happens with Euler or RK4 when ?
No progress — and ; the solver stands still forever, so a positive step size is mandatory.
For an autonomous ODE (no explicit ), do the -shifts in RK4 matter?
No — since ignores , evaluating at , , or makes no difference; the -shifts only matter for non-autonomous .
If is a constant (so ), what does Euler do?
It becomes exact — the true solution is a straight line , and Euler's straight-line hop matches it perfectly with zero truncation error for any .
On a stiff ODE, why can a "small" RK4 step still explode?
Explicit methods have a bounded stability region; if times the (large negative) rate lies outside it, the numerical solution oscillates and grows without bound even though the true solution decays. Fix with implicit methods or adaptive stepping. See Numerical Stability and Stiff ODEs.
If the true solution is a cubic polynomial, how does RK4 behave?
It is exact — RK4 integrates polynomials up to degree with no error, since the discarded terms start at and a cubic has no such terms.
What if becomes undefined (e.g. blows up) partway through the interval?
Fixed-step Euler/RK4 will happily step over the singularity and return garbage; you need adaptive step control to detect the blow-up and refuse to cross it. See Adaptive Step Size (RK45 / Dormand–Prince).
Does taking more RK4 steps always reduce error toward zero?
Not indefinitely — past a point, floating-point round-off from summing many tiny increments dominates, so error bottoms out and then slowly worsens as shrinks further.