4.8.24Numerical Methods

Runge-Kutta 4th order (RK4) — derivation

1,692 words8 min readdifficulty · medium2 backlinks

Solving dydx=f(x,y)\dfrac{dy}{dx}=f(x,y), y(x0)=y0y(x_0)=y_0, without knowing the closed-form solution.

The Big Picture

WHAT are we trying to do?

HOW: deriving the general RK4 skeleton

A 4-stage explicit RK method has the form yn+1=yn+(a1k1+a2k2+a3k3+a4k4)y_{n+1} = y_n + (a_1 k_1 + a_2 k_2 + a_3 k_3 + a_4 k_4) where each stage uses the previous ones:

k1=hf(xn,  yn)k2=hf(xn+c2h,  yn+b21k1)k3=hf(xn+c3h,  yn+b31k1+b32k2)k4=hf(xn+c4h,  yn+b41k1+b42k2+b43k3)\begin{aligned} k_1 &= h\,f(x_n,\; y_n)\\ k_2 &= h\,f(x_n + c_2 h,\; y_n + b_{21}k_1)\\ k_3 &= h\,f(x_n + c_3 h,\; y_n + b_{31}k_1 + b_{32}k_2)\\ k_4 &= h\,f(x_n + c_4 h,\; y_n + b_{41}k_1 + b_{42}k_2 + b_{43}k_3) \end{aligned}

Why this shape? Each kik_i is an estimate of h(slope)h\cdot(\text{slope}) at a sample point. We tune the constants ai,ci,bija_i, c_i, b_{ij} so the whole thing agrees with the Taylor series of the true solution up to h4h^4.

Step 1 — Taylor expansion of the true solution (the target)

Since y=fy'=f, differentiate repeatedly (chain rule, remember yy depends on xx): y=f,y=fx+fyf,y=fxx+2fxyf+fyyf2+fy(fx+fyf).y' = f,\qquad y'' = f_x + f_y f,\qquad y''' = f_{xx}+2f_{xy}f+f_{yy}f^2 + f_y(f_x+f_y f).

Why this step? These are the exact quantities the true solution carries. Our method must reproduce them.

So the exact step is

y(xn+h)=yn+hf+h22(fx+fyf)+h36y+h424y+O(h5).y(x_n+h)=y_n + h f + \tfrac{h^2}{2}(f_x+f_y f) + \tfrac{h^3}{6}y''' + \tfrac{h^4}{24}y'''' + O(h^5).

Step 2 — Taylor expand each kik_i and match

Expanding k2,k3,k4k_2,k_3,k_4 as 2-variable Taylor series and forcing the weighted sum to equal the exact expansion above produces a system of order conditions. The classic solution (there are infinitely many; this is the elegant symmetric one) is:

c2=c3=12,c4=1,a1=a4=16,a2=a3=13,c_2=c_3=\tfrac12,\quad c_4=1,\qquad a_1=a_4=\tfrac16,\quad a_2=a_3=\tfrac13, and b21=b32=12b_{21}=b_{32}=\tfrac12, b43=1b_{43}=1, all others zero.

Why these numbers? The weights (16,13,13,16)\left(\tfrac16,\tfrac13,\tfrac13,\tfrac16\right) are exactly Simpson's rule weights — because when ff depends only on xx, RK4 reduces to Simpson's h6(k1+2k2+2k3+k4)\tfrac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4)-style integration, which is O(h5)O(h^5) accurate. RK4 is Simpson's rule generalized to yy-dependence!

Step 3 — The classical RK4 formulas

Figure — Runge-Kutta 4th order (RK4) — derivation

Worked Example 1

Solve y=x+yy'=x+y, y(0)=1y(0)=1, find y(0.1)y(0.1) with h=0.1h=0.1.

  • k1=hf(0,1)=0.1(0+1)=0.1k_1 = h f(0,1) = 0.1(0+1)=0.1Why? slope at start.
  • k2=0.1f(0.05,1.05)=0.1(0.05+1.05)=0.11k_2 = 0.1\,f(0.05,\,1.05)=0.1(0.05+1.05)=0.11Why? midpoint using half of k1k_1.
  • k3=0.1f(0.05,1.055)=0.1(0.05+1.055)=0.1105k_3 = 0.1\,f(0.05,\,1.055)=0.1(0.05+1.055)=0.1105Why? refined midpoint using half of k2k_2.
  • k4=0.1f(0.1,1.1105)=0.1(0.1+1.1105)=0.12105k_4 = 0.1\,f(0.1,\,1.1105)=0.1(0.1+1.1105)=0.12105Why? endpoint using full k3k_3.

y1=1+16(0.1+2(0.11)+2(0.1105)+0.12105)=1.11034.y_1 = 1 + \tfrac16(0.1 + 2(0.11)+2(0.1105)+0.12105)=1.11034.

Exact solution y=2exx1y(0.1)=1.110342y=2e^x-x-1 \Rightarrow y(0.1)=1.110342. Error 106\approx 10^{-6}! Why so good? Fourth-order accuracy.

Worked Example 2 (Forecast-then-Verify)

Solve y=yy'=y, y(0)=1y(0)=1, one step h=1h=1. Forecast: true value is e=2.71828e=2.71828; Euler gives 22. Predict RK4 lands very close to ee.

  • k1=11=1k_1 = 1\cdot 1 = 1
  • k2=1(1+0.5)=1.5k_2 = 1\cdot(1+0.5)=1.5
  • k3=1(1+0.75)=1.75k_3 = 1\cdot(1+0.75)=1.75
  • k4=1(1+1.75)=2.75k_4 = 1\cdot(1+1.75)=2.75 y1=1+16(1+3+3.5+2.75)=1+10.256=2.70833.y_1 = 1 + \tfrac16(1+3+3.5+2.75)=1+\tfrac{10.25}{6}=2.70833.

Verify: 2.708332.70833 vs 2.718282.71828, error 0.01\approx 0.01 even with a huge step h=1h=1. Notice y1=1+1+12+16+124y_1 = 1+1+\tfrac12+\tfrac16+\tfrac1{24} = the first five terms of exe^x's series — direct proof RK4 matches Taylor to h4h^4.

Common Mistakes

Recall Feynman: explain to a 12-year-old

You want to walk across a hill in the fog and land at the right height. Euler checks the ground slope only where he starts and walks straight — he overshoots. RK4 is smarter: he peeks at the slope at the start, then guesses the middle, checks it, re-checks the middle, then checks the far end — and takes a cleverly weighted average of all four peeks (the two middle peeks count twice). That average almost perfectly matches the real curved path. So even with big steps, he lands almost exactly on target.

Active Recall

What IVP does RK4 solve?
y=f(x,y)y'=f(x,y) with y(x0)=y0y(x_0)=y_0, marching one step hh at a time.
Write the four RK4 slope formulas (with ki=hfk_i=hf).
k1=hf(xn,yn)k_1=hf(x_n,y_n); k2=hf(xn+h2,yn+k12)k_2=hf(x_n+\frac h2,y_n+\frac{k_1}2); k3=hf(xn+h2,yn+k22)k_3=hf(x_n+\frac h2,y_n+\frac{k_2}2); k4=hf(xn+h,yn+k3)k_4=hf(x_n+h,y_n+k_3).
What is the RK4 update formula?
yn+1=yn+16(k1+2k2+2k3+k4)y_{n+1}=y_n+\frac16(k_1+2k_2+2k_3+k_4).
What are the local and global error orders?
Local O(h5)O(h^5), global O(h4)O(h^4).
Why do k2,k3k_2,k_3 get weight 2?
They estimate the midpoint slope (best represents the interval); matches Simpson's 1,2,2,11,2,2,1 weighting.
Where is each kik_i sampled?
k1k_1 left edge, k2,k3k_2,k_3 midpoint, k4k_4 right edge.
What does RK4 reduce to when f=f(x)f=f(x) only?
Simpson's rule for the integral fdx\int f\,dx.
How does RK4 avoid computing derivatives of ff?
It replaces derivatives with extra function evaluations at interior points, matching the Taylor series.
Why is global error one order lower than local?
~1/h1/h steps accumulate the per-step O(h5)O(h^5) error into O(h4)O(h^4).

Connections

Concept Map

exact solution as

too inaccurate

approximate via

has

tune constants against

match up to h^4

solved by

give weights 1/6,1/3,1/3,1/6

produce

generalized to y-dependence

error per step

needs no

IVP y'=f x,y

Integral form of step

Euler uses one slope

RK4 samples many slopes

4-stage skeleton k1..k4

Taylor series of true solution

Order conditions

Symmetric constants c,a,b

Simpson's rule weights

Classical RK4 formulas

O h^5 accuracy

Derivatives of f

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, RK4 ka core idea bahut simple hai. Jab humein y=f(x,y)y'=f(x,y) solve karna hota hai lekin exact formula nahi milta, tab hum step-by-step aage badhte hain. Euler method sirf shuru ki slope dekh kar seedha chal deta hai — isliye galat direction mein overshoot ho jata hai, kyunki slope to raste mein badalti rehti hai. RK4 smart hai: wo char jagah slope check karta hai — start pe (k1k_1), do baar beech mein (k2,k3k_2, k_3), aur end pe (k4k_4).

Phir in char slopes ka ek weighted average leta hai: formula yn+1=yn+16(k1+2k2+2k3+k4)y_{n+1}=y_n+\frac16(k_1+2k_2+2k_3+k_4). Notice karo ki beech wali slopes (k2,k3k_2,k_3) ko do-do baar count kiya — kyunki midpoint interval ka sabse accha representative hota hai. Ye weights 1,2,2,11,2,2,1 actually Simpson's rule se aate hain! Isliye RK4 ko "Simpson's rule for ODEs" bhi kehte hain.

Kyun itna accurate? Kyunki ye weighted combination true solution ke Taylor series ko h4h^4 tak exactly match karta hai — bina koi derivative calculate kiye. Derivatives ki jagah bas extra function evaluations use hote hain. Result: local error O(h5)O(h^5), global error O(h4)O(h^4). Matlab step chhota karoge to error bahut tezi se girega. Exam mein RK4 favourite isliye hai kyunki bade step pe bhi answer exact ke kareeb aata hai — jaise humne dekha y=yy'=y mein h=1h=1 pe bhi ee ke bahut paas nikla.

Yaad rakho: k1k_1 left, k2k_2-k3k_3 middle, k4k_4 right, aur middles ko double weight — bas yehi RK4 ki poori kahani hai.

Go deeper — visual, from zero

Test yourself — Numerical Methods

Connections