4.8.25 · HinglishNumerical Methods

Adaptive step-size — RK45, error control

1,622 words7 min readRead in English

4.8.25 · Maths › Numerical Methods


WHAT we are trying to do

WHY do orders? Ek akela method aapko ek number deta hai, lekin yeh jaanne ka koi tarika nahi ki woh kitna accha hai. Agar aapke paas ek 4th-order estimate aur ek 5th-order estimate same stages se hain, toh essentially 4th-order result ka local error hai — almost koi extra cost nahi. Isko embedded pair kehte hain.


HOW the stages work (the "embedded" idea)

Ek Runge–Kutta method slope samples se banata hai:

y_{n+1} = y_n + h\sum_{i=1}^{s} b_i k_i$$ Ek **embedded pair** same $k_i$ share karta hai lekin do weight rows $b_i$ (order 5) aur $b_i^{*}$ (order 4) use karta hai: $$y^{(5)} = y_n + h\sum_i b_i k_i,\qquad y^{(4)} = y_n + h\sum_i b_i^{*} k_i$$ $$\boxed{\;\varepsilon = y^{(5)} - y^{(4)} = h\sum_i (b_i - b_i^{*})\,k_i\;}$$ > [!intuition] Yeh "free" kyun hai > Kisi bhi RK method ka expensive part function evaluations $k_i$ hain. Dono answers inhe **sab** reuse karte hain, isliye error estimate sirf ek sasta dot product cost karta hai, extra $f$ calls nahi. --- ## DERIVING the step-update formula from scratch Hum assume karte hain ki lower-order method ki error leading term ki tarah scale hoti hai: $$\varepsilon(h) \approx C\,h^{p+1}, \qquad p = 4 \text{ for RK45.}$$ **WHY:** order-$p$ method ki local truncation error apne pehle uncancelled Taylor term se dominate hoti hai, jo $\propto h^{p+1}$ hai. Hum ek naya step $h_{\text{new}}$ chahte hain jiska error tolerance ke barabar ho: $$C\,h_{\text{new}}^{\,p+1} = \text{tol}.$$ Unknown constant $C$ ko eliminate karne ke liye dono equations ko divide karo: $$\frac{\text{tol}}{\varepsilon} = \frac{C h_{\text{new}}^{p+1}}{C h^{p+1}} = \left(\frac{h_{\text{new}}}{h}\right)^{p+1}.$$ $h_{\text{new}}$ ke liye solve karo: $$\boxed{\,h_{\text{new}} = h \left(\frac{\text{tol}}{\varepsilon}\right)^{1/(p+1)}\,}$$ > [!formula] Practical step controller > $$h_{\text{new}} = h \cdot \min\!\Big(f_{\max},\;\max\big(f_{\min},\; S\,(\text{tol}/\varepsilon)^{1/(p+1)}\big)\Big)$$ > - $S \approx 0.8$–$0.9$ : **safety factor** (tol hit karne par gamble mat karo). > - $f_{\min}\approx 0.2,\; f_{\max}\approx 5$ : clamp taaki $h$ kabhi wildly jump na kare. > - $p+1 = 5$ for RK45. > **Safety factor kyun?** $\varepsilon$ khud ek estimate hai; thoda *tol ke neeche* aim karo taaki retried step pehli baar hi accept ho jaye. ![[4.8.25-Adaptive-step-size-—-RK45,-error-control.png]] --- ## Worked Example 1 — accept and grow > [!example] Ek adaptive step > Suppose ek step par aapne $h = 0.10$ use kiya, $\varepsilon = 1\times10^{-7}$ mila, tol $= 1\times10^{-6}$, $S=0.9$, $p=4$. > > **Step 1 — accept?** $\varepsilon < \text{tol}$ ✔ → is step ko accept karo. > *Yeh step kyun?* Error budget ke andar hai, toh answer kaafi accha hai. > > **Step 2 — new $h$:** ratio $= \text{tol}/\varepsilon = 10$. > $$h_{\text{new}} = 0.9 \times 0.10 \times 10^{1/5} = 0.09 \times 1.585 = 0.143.$$ > *Yeh step kyun?* Hamare paas spare error thi, isliye hum $h$ **grow** karte hain tezi se kaam karne ke liye — lekin sirf $10^{1/5}$ se, 10 se nahi, kyunki error $h^5$ ki tarah scale hoti hai. ## Worked Example 2 — reject and shrink > [!example] Ek rejected step > Ab $h = 0.10$, $\varepsilon = 4\times10^{-6}$, tol $= 1\times10^{-6}$. > > **Step 1 — accept?** $\varepsilon > \text{tol}$ ✗ → **reject** karo, step redo karo. > *Yeh step kyun?* Step bahut inaccurate hai; hum $t$ ko aage nahi badha sakte. > > **Step 2 — new $h$:** ratio $= 0.25$. > $$h_{\text{new}} = 0.9\times0.10\times0.25^{0.2} = 0.09\times0.758 = 0.068.$$ > *Yeh step kyun?* $h$ shrink karo taaki **retried** step ki predicted error $\approx C h_{\text{new}}^5 = $ tol ho. Hum same $t_n$ se $0.068$ ke saath retry karte hain. ## Worked Example 3 — clamp in action > [!example] Forecast-then-verify > $h = 0.05$, $\varepsilon = 1\times10^{-12}$, tol $=1\times10^{-6}$, $f_{\max}=5$. > Raw factor $= 0.9\,(10^{6})^{1/5} = 0.9\times 15.8 = 14.2$. > **Forecast:** yeh $h$ ko 14× enlarge kar deta — dangerous! > **Verify:** controller $f_{\max}=5$ par clamp karta hai, toh $h_{\text{new}} = 0.25$. > *Yeh step kyun?* $h^5$ model sirf locally trustworthy hai; ek 14× jump aise region mein land kar sakta hai jo model ne kabhi nahi dekha. Clamping humein safe rakhta hai. --- > [!mistake] Steel-manned errors > **Mistake A:** "Error $h^4$ ki tarah scale hoti hai toh exponent $1/4$ use karo." > *Yeh sahi kyun lagta hai:* RK45 ka *result* order 4 (ya 5) ka hai. **Fix:** **local** error per step $\sim h^{p+1}=h^5$ hai, isliye exponent $1/(p+1)=1/5$ hai. $1/4$ use karne se controller over-react karta hai. > > **Mistake B:** "Reject karne ke baad, chhote $h$ ke saath $t$ aage badha do." > *Yeh sahi kyun lagta hai:* Aapne "kaam kiya," toh ise throw away karna wasteful lagta hai. **Fix:** ek rejected step matlab $y_{n+1}$ untrustworthy hai — aapko **$t_n$ par hi rehna hai** aur naye $h$ ke saath recompute karna hai. > > **Mistake C:** Sirf absolute tolerance use karna. > *Yeh sahi kyun lagta hai:* simpler hai. **Fix:** agar $|y|$ bada ho jaaye, ek fixed atol impossibly strict ho jaata hai; $\text{rtol}\,|y|$ mix karo taaki tolerance solution ke saath scale kare. > > **Mistake D:** "Error estimate $y^{(5)}$ ki error hai." > *Yeh sahi kyun lagta hai:* aap $y^{(5)}$ ke saath aage badhte ho (local extrapolation). **Fix:** $\varepsilon$ *4th-order* error estimate karta hai; yeh dono mein se worse ko bound karta hai, jo conservative hai — woh theek hai. --- > [!recall]- Feynman: ek 12-saal ke bachche ko explain karo > Sochiye aap ek car drive kar rahe ho aur aap winding road par bina crash ke rehna chahte ho. Lambi seedhi jagah par aap speed up karte ho; sharp turns ke paas slow down karte ho. Yeh jaanne ke liye ki turn kitna sharp hai, aap **do cameras** se dekhte ho — ek rough, ek sharp — aur dekhte ho ki unki pictures kitni alag hain. Agar woh barely alag hain, road seedhi hai: floor it (bada step). Agar woh bahut disagree karte hain, turn aa raha hai: brake lagao (chhota step). RK45 bilkul yahi hai: do answers, unhe compare karo, aur apni speed (step size) choose karo taaki aap kabhi badi galti na karo. > [!mnemonic] Controller yaad rakho > **"Tol Over Error, to the Fifth-root"** → $h_{\text{new}} = h\,(\text{tol}/\varepsilon)^{1/5}$. > Aur **"Reject → Repeat, Accept → Advance."** --- ## #flashcards/maths RK45 har step par do solutions kyun compute karta hai? ::: *Same* stage evaluations $k_i$ se ek free local-error estimate $\varepsilon = y^{(5)}-y^{(4)}$ paane ke liye. "Embedded" Runge–Kutta pair kya hota hai? ::: Do methods alag order ke jo same $k_i$ share karte hain lekin alag weight rows $b_i,\,b_i^*$ rakhte hain. Order-$p$ method ki local error kaise scale hoti hai? ::: $C\,h^{p+1}$. Naya step size derive karo. ::: $\varepsilon\approx Ch^{p+1}$ aur $\text{tol}=Ch_{\text{new}}^{p+1}$ se, $C$ cancel karne ke liye divide karo: $h_{\text{new}}=h(\text{tol}/\varepsilon)^{1/(p+1)}$. RK45 (order 4 controlled) ke liye $h$ update karne ka exponent kya hai? ::: $1/(p+1)=1/5$. Rejected step ke baad kya karte hain? ::: $t_n$ par ruko, chhote $h_{\text{new}}$ ke saath recompute karo; $t$ aage mat badha. Safety factor $S\approx0.9$ ka purpose kya hai? ::: Thoda tol ke neeche aim karo taaki retried/next step usually pehli baar pass ho jaye. $h_{\text{new}}$ ko $f_{\min},f_{\max}$ se kyun clamp karte hain? ::: $h^{p+1}$ model sirf locally valid hai; clamps wild jumps rokti hain. Standard tolerance formula kya hai? ::: $\text{tol}=\text{atol}+\text{rtol}\,|y_n|$. Error estimate essentially "free" kyun hai? ::: Yeh saare expensive $f$-evaluations reuse karta hai; sirf ek sasta weighted sum add hota hai. --- ## Connections - [[Runge–Kutta Methods (RK4)]] — RK45 ka fixed-step parent. - [[Local vs Global Truncation Error]] — kyun $h^{p+1}$ step controller govern karta hai. - [[Order of a Numerical Method]] — exponent mein $p$ define karta hai. - [[Stiff ODEs and Stability]] — jab adaptive explicit methods phir bhi struggle karte hain. - [[Taylor Series Expansion]] — leading error term ki origin. - [[Dormand–Prince (RK45 used by scipy solve_ivp)]] — standard coefficient table. ## 🖼️ Concept Map ```mermaid flowchart TD FIXED[Fixed step h wasteful] -->|motivates| ADAPT[Adaptive step-size] ADAPT -->|needs| ESTERR[Free error estimate] RK45[RK45 embedded pair] -->|provides| ESTERR RK45 -->|shares| STAGES[Same slopes k_i] STAGES -->|weighted by| Y4[Order-4 estimate] STAGES -->|weighted by| Y5[Order-5 estimate] Y5 -->|difference| EPS[Error epsilon] Y4 -->|difference| EPS TOL[tol = atol + rtol times y] -->|compare with| DECIDE{epsilon <= tol} EPS -->|compare with| DECIDE DECIDE -->|yes| ACCEPT[Accept step] DECIDE -->|no| REJECT[Reject and shrink h] EPS -->|assume C h^p+1| UPDATE[h_new formula] TOL -->|eliminate C| UPDATE UPDATE -->|with safety S| CTRL[Step controller] ```