HOW do we get from yn to yn+1? Use the definition of a derivative:
y′(tn)=limh→0hy(tn+h)−y(tn)≈hyn+1−yn
Drop the limit (use a finiteh) and solve for yn+1:
Equivalently from a Taylor expansion:
y(tn+h)=y(tn)+hy′(tn)+error we throw away2h2y′′(ξ)
The discarded term is O(h2)per step → local truncation errorO(h2). Over N=T/h steps the errors accumulate to global error==O(h)==. Halve h → halve the error. That's slow.
def euler(f, t0, y0, h, n): t, y = t0, y0 ts, ys = [t], [y] for _ in range(n): y = y + h * f(t, y) # one slope, whole step t = t + h ts.append(t); ys.append(y) return ts, ys
WHY do better? Euler uses the slope only at the left of the interval, so it consistently over/undershoots when the curve bends. RK4 samples the slope at the start, the middle (twice), and the end, then takes a weighted average — analogous to Simpson's rule for the integral ∫tntn+hfdt.
Why the weights 1,2,2,1? Sum =6, so 6h(…) is a true weighted mean slope. RK4 uses two separate midpoint slopes (k2,k3), each given weight 2, because the midpoint best represents the average behaviour over [tn,tn+h]. This is analogous to Simpson's 1/3 rule (which weights endpoint–midpoint–endpoint as 1,4,1): both put extra weight on the middle of the interval. RK4 splits Simpson's single "4" into two midpoint evaluations k2,k3 that together carry weight 2+2=4. Matching the Taylor series up to h4 forces exactly these coefficients.
Local truncation error O(h5), global error ==O(h4)==.
Halve h → error drops by 24=16×. That's why RK4 is the workhorse.
def rk4(f, t0, y0, h, n): t, y = t0, y0 ts, ys = [t], [y] for _ in range(n): k1 = f(t, y) k2 = f(t + h/2, y + h/2 * k1) k3 = f(t + h/2, y + h/2 * k2) k4 = f(t + h, y + h * k3) y = y + (h/6) * (k1 + 2*k2 + 2*k3 + k4) t = t + h ts.append(t); ys.append(y) return ts, ys
Why this matters: halving h helps Euler twice but helps RK4 sixteen times — the entire reason RK4 dominates practical computing.
Recall Feynman: explain to a 12-year-old
Imagine walking in fog where a tiny compass tells you which way to step right where you stand. Euler just trusts that one reading and walks a full step — but if the path curves, you drift off. RK4 is smarter: you peek ahead to the middle, peek again, peek at the far end, then take the average of all those compass readings before stepping. Averaging four peeks keeps you almost perfectly on the path.
Weighted mean (sum 6) of slopes; the two midpoint slopes count double each (total 4) — analogous to Simpson's rule's middle weight; forced by matching Taylor series to h4.
Are RK4 weights identical to Simpson's (1,4,1)?
No — analogous, not identical. RK4 uses (1,2,2,1) with two midpoint evaluations whose weights sum to 4.
RK4 global error order?
O(h4) — halving h cuts error ~16×.
Each RK4 stage builds on which previous stage?
k2 uses k1, k3 uses k2, k4 uses k3 — sequential, not all from k1.
Why is RK4 cheaper than Euler at equal accuracy?
Euler cost ~1/ε, RK4 ~1/ε1/4; four f-calls per step buy huge accuracy.
Dekho, ODE ka matlab hai ki tumhe har point pe sirf slope pata hai: y′=f(t,y). Tumhe poora curve nahi pata, bas yeh pata hai ki kis direction me badhna hai. To numerical solving ka funda simple hai — known point se start karo, slope dekho, chhota sa step (h) lo, aur repeat karte raho. Yahi fog me chalne jaisa hai jahan ek compass har jagah direction batata hai.
Euler sabse seedha tareeka hai: bas left point ka ek slope leke poora step maar do — yn+1=yn+hf(tn,yn). Problem yeh hai ki agar curve mud raha hai to tum drift kar jaoge, kyunki ek hi slope par bharosa kar liya. Iska error O(h) hai, yani h aadha karo to error sirf aadha hota hai — bahut slow.
RK4 smart hai: yeh ek nahi, chaar slopes leta hai — start (k1), do baar midpoint (k2,k3), aur end (k4) — phir weighted average 6h(k1+2k2+2k3+k4) leta hai. Weights 1,2,2,1 Simpson's rule jaise analogous hain (Simpson ke weights 1,4,1 hote hain), kyunki RK4 do midpoint evaluations karta hai jinka total weight 2+2=4 ho jaata hai — Simpson ke beech wale 4 ke barabar. Lekin yeh bilkul same nahi hai. Iska error O(h4) — h aadha karo to error 16 guna kam! Isiliye real-world computing me RK4 default choice hai.
Important baat: har RK4 stage pichle stage pe banta hai (k3 me k2 use hota hai, k1 nahi), aur agar f me t aata hai to t ko bhi shift karna mat bhoolna. Bas yeh do galtiyaan avoid karo, baaki RK4 magic hai.