4.8.27Numerical Methods

Systems of ODEs — RK4 for systems

1,573 words7 min readdifficulty · medium1 backlinks

What we are solving (WHAT)


Deriving RK4-for-systems from scratch (HOW)

Start from the exact integral over one step: y(t+h)=y(t)+tt+hf(s,y(s))ds.\mathbf{y}(t+h) = \mathbf{y}(t) + \int_t^{t+h}\mathbf{f}(s,\mathbf{y}(s))\,ds.

RK4 approximates this integral with Simpson-like sampling, evaluating f\mathbf f at the start, twice at the midpoint, and at the end — but using estimated states, not the unknown true ones.

Figure — Systems of ODEs — RK4 for systems

Worked Example 1 — a 2×2 linear system

Solve y1=y2,  y2=y1y_1' = y_2,\ \ y_2' = -y_1 with y(0)=(1,0)\mathbf y(0)=(1,0). (True solution: y1=cost, y2=sinty_1=\cos t,\ y_2=-\sin t.) Take h=0.1h=0.1, one step.

So f(t,y)=(y2, y1)\mathbf f(t,\mathbf y) = (y_2,\ -y_1).

Step k1. k1=(y2, y1)=(0, 1)\mathbf k_1 = (y_2,\ -y_1) = (0,\ -1). Why this step? Slope at the start using the current state (1,0)(1,0).

Step k2. Intermediate state y+h2k1=(1,0)+0.05(0,1)=(1, 0.05)\mathbf y + \tfrac h2\mathbf k_1 = (1,0)+0.05(0,-1)=(1,\ -0.05). k2=(0.05, 1)\mathbf k_2 = (-0.05,\ -1). Why this step? We re-evaluate the slope at the predicted midpoint — note both components used the advanced state, not just one.

Step k3. y+h2k2=(1,0)+0.05(0.05,1)=(0.9975, 0.05)\mathbf y + \tfrac h2\mathbf k_2 = (1,0)+0.05(-0.05,-1)=(0.9975,\ -0.05). k3=(0.05, 0.9975)\mathbf k_3 = (-0.05,\ -0.9975). Why this step? A refined midpoint slope using k2\mathbf k_2.

Step k4. y+hk3=(1,0)+0.1(0.05,0.9975)=(0.995, 0.09975)\mathbf y + h\,\mathbf k_3 = (1,0)+0.1(-0.05,-0.9975)=(0.995,\ -0.09975). k4=(0.09975, 0.995)\mathbf k_4 = (-0.09975,\ -0.995). Why this step? Slope at the end of the interval.

Combine. y1,1=1+0.16(0+2(0.05)+2(0.05)+(0.09975))=1+0.16(0.29975)0.99500.y_{1,1}=1+\tfrac{0.1}{6}\big(0+2(-0.05)+2(-0.05)+(-0.09975)\big)=1+\tfrac{0.1}{6}(-0.29975)\approx 0.99500. y2,1=0+0.16(1+2(1)+2(0.9975)+(0.995))=0.16(5.99)0.099833.y_{2,1}=0+\tfrac{0.1}{6}\big(-1+2(-1)+2(-0.9975)+(-0.995)\big)=\tfrac{0.1}{6}(-5.99)\approx -0.099833. True: cos0.1=0.995004\cos 0.1=0.995004, sin0.1=0.099833-\sin 0.1=-0.099833. The y1y_1 value matches to ~4 decimals; the y2y_2 value matches to ~3 decimals — with one step.


Worked Example 2 — a 2nd-order ODE via reduction

Solve y+y=0y'' + y = 0, y(0)=1, y(0)=0y(0)=1,\ y'(0)=0. Reduce: u1=y, u2=yu_1=y,\ u_2=y': u1=u2,u2=u1.u_1' = u_2,\qquad u_2' = -u_1. Why this step? Same physics as Example 1 — the pendulum/oscillator. We converted a 2nd-order ODE into a system so RK4-for-systems applies directly. The result is identical: y1y_1 tracks cost\cos t. This proves the reduction trick lets one method handle all orders.


Forecast-then-Verify


80/20 — the few things that carry everything

Recall Feynman: explain to a 12-year-old

Imagine two friends running and holding hands — wherever one goes affects the other. To guess where they'll be in a moment, you don't peek once; you peek four times: at the start, twice in the middle, and at the end, each time guessing where both friends moved together. Then you take a clever weighted average (count the middle peeks double). Because they're holding hands, you must move both at each peek, never one alone. That careful four-peek average is RK4 for systems.


Connections

  • RK4 for a single ODE — the scalar parent method.
  • Reducing higher-order ODEs to first-order systems
  • Euler's method for systems — the cheaper, less accurate cousin.
  • Local vs Global Truncation Error — why O(h5)O(h^5) local → O(h4)O(h^4) global.
  • Stiff systems and stability — where explicit RK4 struggles.
  • Simpson's Rule — the quadrature RK4 mimics.

What change turns scalar RK4 into RK4 for a system?
Replace every scalar (yy, kik_i, ff) by its vector version; recipe and weights are unchanged.
Why must you finish the full vector k1\mathbf k_1 before starting k2\mathbf k_2?
Because each fif_i depends on all components; k2\mathbf k_2 needs the synchronized advanced state of every component.
How do you make a 2nd-order ODE y=g(t,y,y)y''=g(t,y,y') a first-order system?
Set u1=y, u2=yu_1=y,\ u_2=y'; then u1=u2, u2=g(t,u1,u2)u_1'=u_2,\ u_2'=g(t,u_1,u_2).
RK4 final combination formula (vector)?
yn+1=yn+h6(k1+2k2+2k3+k4)\mathbf y_{n+1}=\mathbf y_n+\frac h6(\mathbf k_1+2\mathbf k_2+2\mathbf k_3+\mathbf k_4).
Global error order of RK4?
O(h4)O(h^4) (local truncation O(h5)O(h^5)).
In k2\mathbf k_2, what argument do you pass for the state?
yn+h2k1\mathbf y_n+\tfrac h2\mathbf k_1 at time tn+h2t_n+\tfrac h2.
What integral does RK4 approximate over one step?
tt+hf(s,y(s))ds\int_t^{t+h}\mathbf f(s,\mathbf y(s))\,ds, sampled Simpson-style.

Concept Map

generalise via

gives

with initial data

via

becomes

Simpson sampling

works on vectors so

enables

uses

achieves

coupling breaks if

Scalar ODE y'=f

First-order system y'=f vector

IVP with y at t0

Treat unknowns as one vector

Higher-order ODE

Reduction: name derivatives

Exact integral over one step

Only algebra of f, h, add/mul

RK4 for systems vector form

Each k_i is length-n vector

Global error O h^4

Mistake: advance components separately

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ek single ODE mein sirf ek quantity ka rate hota hai. Lekin real life mein cheezein juddi hoti hain — jaise position aur velocity, ya predator aur prey. Inko alag-alag solve nahi kar sakte kyunki ek dusre par depend karte hain. Toh trick simple hai: saari unknowns ko ek vector y\mathbf{y} maan lo, aur jo RK4 scalar ke liye chalta tha, wahi recipe bilkul same — sirf har symbol ko bold (vector) bana do. Weights 1,2,2,11,2,2,1 over 66 same rehte hain.

Sabse important baat (yahi 80% marks dilata hai): stages ko synchronize karo. Pehle poora k1\mathbf k_1 nikalo — yaani saare components ka slope. Phir us advanced state se poora k2\mathbf k_2, phir k3\mathbf k_3, phir k4\mathbf k_4. Galti yeh hoti hai ki log ek equation ka pura RK4 finish kar dete hain phir doosre ka — yeh galat hai kyunki f1f_1 ko y2y_2 ki updated value chahiye. Dono haath pakad ke chal rahe hain, ek ko akele aage nahi badha sakte.

Aur ek bonus: koi bhi higher-order ODE ko first-order system mein todo. y=yy''=-y ko u1=y, u2=yu_1=y,\ u_2=y' rakh ke u1=u2, u2=u1u_1'=u_2,\ u_2'=-u_1 bana do. Ab same RK4-for-systems chala do. Isliye yeh ek hi method actually saari ODEs handle kar leta hai — bahut powerful aur exam mein guaranteed aata hai.

Go deeper — visual, from zero

Test yourself — Numerical Methods

Connections