4.8.25 · D3 · HinglishNumerical Methods

Worked examplesAdaptive step-size — RK45, error control

4,789 words22 min read↑ Read in English

4.8.25 · D3 · Maths › Numerical Methods › Adaptive step-size — RK45, error control

Yeh page parent note Adaptive step-size — RK45, error control ka "koi surprise nahi" drill hai. Step controller mein har knob alag-alag direction mein point kar sakta hai — error bahut bada ho sakta hai, bahut chota, ya bilkul budget ke andar; raw step factor grow, shrink, ya safety clamps ke bahar ja sakta hai; tolerance khud ke badhne ke saath change hoti hai. Neeche pehle hum har possible case lay out karte hain, phir har ek ke liye ek example karte hain taaki aap kabhi bhi aisa scenario na dekhen jo pehle na dekha ho.

Numbers chuney se pehle, ek reminder ki har symbol ka kya matlab hai, simple shabdon mein:


Scenario matrix

Har step mein controller do independent sawaalon ka combination leta hai. Ise ek grid ki tarah socho:

Cell Error vs tol ( vs ) Raw factor Outcome jo hume dikhana hai
C1 (spare room) aur ke beech accept, moderately grow
C2 (bahut zyada) se neeche reject, shrink but pe clamp
C3 (bahut chota error) se upar accept, but growth pe clamp
C4 (thoda zyada) aur ke beech reject, moderately shrink, pe raho
C5 exactly (boundary) accept, thoda sa hi badlta hai
C6 (degenerate: perfect step) division danger → clamp karna zaroor
C7 tolerance relative-dominated hai ( large, incl. negative ) koi bhi dikhao badhta hua
C8 ODEs ka ek system (vector ) koi bhi error ek norm hai, scalar nahi
C9 word problem / real integration to koi bhi last step capped taaki pe exactly land ho
C10 non-finite ya (NaN / Inf from overflow) undefined reject + shrink force karo, number pe kabhi trust mat karo

Neeche ke examples har cell ko cover karte hain. Har ek pe label dhyaan se dekho.


Example 1 — C1: accept aur moderately grow

Steps.

  1. Accept ya reject? Compare karo ko se. Kyunki , step theek hai → accept karo aur ko pe advance karo. Yeh step kyun? ka poora point ek gatekeeper hona hai; budget ke andar matlab answer trustworthy hai.
  2. Ratio. . Yeh step kyun? Hum form karte hain kyunki woh ratio exactly woh factor hai jisse hum error budget enlarge kar sakte hain; ise law se paas karna (agle step mein) "spare error" ko "allowed step change" mein convert karta hai. Yahi ek quantity hai jo unknown constant ko cancel karti hai, toh yahi sab kuch drive karta hai.
  3. Raw factor. . Yeh step kyun? Error ki tarah badhti hai, toh zyada error spend karne ke liye hum sirf se stretch kar sakte hain — bahut gentle factor.
  4. Clamp check. , aur ke beech hai, toh koi clamp fire nahi hota.
  5. New step. .

Verify: predicted new error , error constant use karke jo symbol list mein define hai. Kyunki , hame milta hai , toh cancel ho jata hai: predicted . Yeh se just under hai — exactly wahi jo safety factor aim kar raha tha. ✔ (Yahan scalar hai, toh .)


Example 2 — C4: reject aur moderately shrink

Steps.

  1. Accept ya reject? reject. Hum aage nahi badhate; pe ruke rehte hain. Yeh step kyun? Ek rejected step matlab trustworthy nahi hai — ise use karna ek bahut bada error hamesha ke liye aage carry kar dega (dekho Local vs Global Truncation Error).
  2. Ratio. . Yeh step kyun? Wahi reason jaise hamesha — yeh ratio, ek baar law se paas hone ke baad, batata hai ki ko kitna resize karna hai; se neeche ki value pehle se hi "shrink" signal de rahi hai. Yeh unknown ko bhi cancel karta hai.
  3. Raw factor. . Yeh step kyun? se neeche ka factor matlab shrink karna — sahi hai, kyunki hum budget overshoot kar gaye.
  4. Clamp check. , aur ke beech hai; koi clamp nahi.
  5. New step. . Hum usi se dobara try karte hain iske saath.

Verify: predicted retried error . Toh retry pehli baar pass ho jaana chahiye — safety margin kaam aa raha hai. ✔


Example 3 — C2: reject, lekin shrink floor clamp se takra jaata hai

Steps.

  1. Reject. pe raho.
  2. Ratio. . Yeh step kyun? Jaise pehle, hum build karte hain taaki law "kitna over budget" ko "kitna shrink karna hai" mein convert kar sake; se bahut neeche ka ratio ek badi shrink ki warning hai, jise clamp baad mein tame karega.
  3. Raw factor. . Yeh step kyun? — fifth root ek bade overshoot ko bhi tame karta hai.
  4. Clamp check. → floor clamp fire karta hai. Iske bajaay use karo. Yeh step kyun? error model current ke aas paas fit kiya gaya tha; ek wild shrink aise region mein land kar sakti hai jo model ne kabhi nahi dekha, toh hum kitni tezi se peeche hatein ise cap karte hain aur simply dobara try karte hain agar phir bhi zyada ho.
  5. New step. .

Verify: unclamped prediction hoti — tol ke andar. Clamp ke saath hum sirf shrink karte hain, giving predicted , phir bhi tol ke upar. Toh yeh step dobara reject hone wala hai aur phir se shrink karega — yahi intended, safe, gradual behaviour hai. ✔

Figure — Adaptive step-size — RK45, error control

Example 4 — C3: accept, lekin growth ceiling clamp se takra jaata hai

Steps.

  1. Accept, advance karo.
  2. Ratio. . Yeh step kyun? Hum ratio hamesha usi wajah se banate hain — yeh dimensionless "error headroom" hai jo law step-change factor mein convert karta hai; yahan yeh enormous hai, ek aisi growth foreshadow karta hai jo ceiling clamp ko cap karni padegi.
  3. Raw factor. .
  4. Clamp check. → ceiling clamp fire karta hai. use karo. Yeh step kyun? ka jump solution ki aane wali sharp feature ke upar se skip kar sakta hai; hum growth cap karte hain aur next step ko phir se grow karne dete hain agar abhi bhi room ho.
  5. New step. .

Verify: clamped step ke saath predicted new error , abhi bhi safely se andar. Next step mein badhte rehne ki jagah — exactly "sprint gradually" behaviour. ✔

Figure — Adaptive step-size — RK45, error control

Example 5 — C5: tolerance boundary pe exactly baitha hua

Steps.

  1. Accept ya reject? Rule hai → accept. Kyunki , hum accept karte hain (boundary inclusive hai). Yeh step kyun? Exactly-on-budget step ko reject karna boundary ke paas forever loop kar dega; decision ko definite banata hai.
  2. Ratio. . Yeh step kyun? Hum phir bhi ratio form karte hain kyunki update formula iske liye demand karta hai; exactly ka ratio matlab "koi error headroom nahi," toh mein koi bhi change sirf safety factor se aata hai.
  3. Raw factor. . Yeh step kyun? Koi error headroom nahi hone par, sirf safety factor se change aata hai, jo ko thoda neeche nudge karta hai taaki future steps margin rakhein.
  4. Clamp check. , aur ke beech hai; koi clamp nahi.
  5. New step. .

Verify: safety factor ko hamesha dena chahiye jab ho. Indeed . ✔ Yahi wajah hai : on-budget hone par bhi, hum edge ki bajaye safety ki taraf drift karte hain.


Example 6 — C6: degenerate perfect step ()

Steps.

  1. Accept ka error certainly hai. Yeh step kyun? Zero estimated error matlab dono embedded answers perfectly agree karte hain, toh step jitna ho sake utna trustworthy hai.
  2. Division guard karo. Ratio , pe undefined hai. Yeh step kyun? Ratio form karna usual move hai, lekin yahan zero se divide ho jaata; law ise use karne se pehle hum ise protect karte hain. Code mein ko se replace karte hain, jahan machine precision se tied ek tiny positive number hai — ek common concrete choice hai (roughly double-precision round-off). Tab ratio finite aur huge hota hai, toh raw factor overshoot karta hai aur ceiling clamp le leta hai.
  3. New step. Floor in place hone par raw factor se exceed karta hai, toh . Yeh step kyun? Ceiling clamp pehle se hi "enormously grow karna chahta hai" ko " grow karo" mein turn karta hai, toh ke liye koi separate rule nahi chahiye.

Verify: floor ki bajaye limit lo aur same answer milta hai. Jaise , raw factor , aur . Toh limiting new step hai case sirf us limit ka endpoint hai, aur floor sirf computer ko crash kiye bina wahan pahunchne deta hai. ✔


Example 7 — C7: relative tolerance le leta hai jab badhta hai (aur negative ke liye positive rehta hai)

Steps.

  1. Pehle absolute value lo. Relative term use karta hai , nahi . Yeh step kyun? Tolerance allowed error ki size hai; yeh honi chahiye. mein exactly isi liye hai taaki negative solution apna magnitude contribute kare — warna ek negative ek negative "budget" deta, jo meaningless hai (aap zero se choti error allow nahi kar sakte).
  2. Tolerance build karo. . Yeh step kyun? Jab bada hota hai, error se neeche demand karna absurdly strict hai; relative part kehta hai "mujhe ki magnitude ka ki parwah hai", jo yahan lagbhag hai.
  3. Ratio. . Yeh step kyun? Hum ratio usi wajah se form karte hain jaise har example mein — yeh dimensionless headroom hai jo law step-change factor mein convert karta hai. se upar ki value signal karti hai ki step budget ke andar hai aur thoda grow kar sakta hai.
  4. Accept ya reject? accept. Yeh step kyun? Properly scaled budget ke against step comfortably inside hai; sirf naive absolute-only view ne ise bura kaha.
  5. Raw factor aur new step. , toh (thoda shrink kyunki hum barely budget ke andar hain).

Verify: sign fix aur relative dominance confirm karo. Kyunki , budget as required. Atol term , rtol term se times choti hai, toh 6 significant figures tak. Absolute-only se, hum ek perfectly fine step reject kar dete aur stall ho jaate — parent note se classic Mistake C. ✔


Example 8 — C8: ODEs ka ek system, error ek norm hai

Steps.

  1. Har component scale karo. Har error ko uski apni tolerance se divide karo: , . Yeh step kyun? Alag-alag components alag scales pe ho sakte hain; har error ko uske apne se divide karna har component ko ek dimensionless "apne budget ka fraction" mein turn karta hai, toh woh comparable ho jaate hain. se neeche ka scaled value matlab woh component budget ke andar hai.
  2. Root-mean-square norm se combine karo. . Yeh step kyun? Yahan actually woh kaam karta hai jo nahi kar sakta: accept/reject test ko ek single scalar chahiye, toh hum scaled errors ke vector ko ek number mein collapse karte hain. RMS norm (scipy ke Dormand–Prince solver mein use hota hai) squared scaled errors ko average karta hai taaki koi bhi single component ignore na ho aur koi component akele unfairly dominate na kare.
  3. Accept ya reject? Kyunki hum pehle se tolerances se scale kar chuke hain, budget ab simply hai: scaled matlab "har component average mein apne budget ke andar hai." Yahan accept. Yeh step kyun? Scaling ne tolerance ko error mein fold kar diya, toh comparison " vs " clean " vs " ban jaata hai — yeh wahi test hai, sirf budget ki units mein measure hota hai.
  4. Ratio aur raw factor. Scaled budget ke saath jo tol ka role play karta hai, ratio hai , aur raw factor hai . Yeh step kyun? Hum ratio exactly usi wajah se form karte hain jaise har scalar case mein — yeh error headroom hai jo law step-change factor mein convert karta hai; sirf fark yeh hai ki "budget" ab scaled value hai.
  5. New step. (grow, kyunki vector step comfortably budget ke andar tha). Yeh step kyun? se bahut kam scaled error matlab sab components mein spare room hai, toh hum speed up kar sakte hain — fifth root growth ko gentle rakhta hai.

Verify: RMS norm check: , , aur . ✔ Kyunki step grow karta hai, exactly jaise ek comfortable vector step ke liye expect kiya. Note yeh wahi controller hai jaise scalar case — sirf "" ka matlab (ab ek norm) aur "budget" (ab ) generalise hua.


Example 9 — C9: word problem, exactly pe land karo

Steps.

  1. Remaining distance check karo. . Yeh step kyun? Hume exactly requested time pe finish karna hai; overshoot karna galat pe answer dega.
  2. Step cap karo. . Yeh step kyun? Capping (rejecting nahi) sahi hai kyunki ek chota step sirf error kam karta hai, toh yeh acceptable rehta hai.
  3. Final step lo se tak. Integration complete hai. Yeh step kyun? Ek baar , pe exactly pahunch jaata hai, requested integration done hai; aage badhane ke liye kuch bachi nahi.

Verify: exactly — deadline pe land karte hain. Aur kyunki , predicted error pehle se accepted estimate se choti hai, toh capped step guaranteed acceptable hai. ✔ Fixed-step RK4 method se compare karo, jisme yeh cap hand-coded karna padta.

Figure — Adaptive step-size — RK45, error control

Example 10 — C10: non-finite ya (NaN / Inf guard)

Steps.

  1. Non-finite value detect karo. Compare karne se pehle, isfinite(eps) aur isfinite(tol) test karo. Yahan test fail karta hai. Yeh step kyun? NaN har comparison tod deta hai: NaN <= tol, NaN > tol, aur NaN == tol sab false hain, toh plain accept/reject logic silently galat kaam karega (aksar accept). Hume comparison se pehle pakadna hai, baad mein nahi.
  2. Rejection force karo. Kisi bhi non-finite ko "infinitely over budget" treat karo: step reject karo aur pe raho. Yeh step kyun? Ek overflowed error certainly tolerance ke andar nahi hai; sirf safe reading hai "sabse bada possible error," jo reject hai.
  3. Floor clamp pe shrink karo. mein feed mat karo (woh ya doosra NaN deta hai). Iske bajaay directly floor factor use karo: . Yeh step kyun? Step-update formula ek finite assume karta hai; ise bypass karke maximum allowed amount se shrink karna ek step ki taraf sabse tezi se safe retreat hai jo itni chhoti ho ki arithmetic overflow na kare.
  4. (Agar khud non-finite ho — jaise overflow ho gaya — wahi rule apply hota hai: reject aur shrink karo, kyunki jo budget aap compute nahi kar sakte woh meet nahi kiya ja sakta.)

Verify: NaN trap logic confirm karo. IEEE arithmetic mein NaN <= x har x ke liye False hai, toh naive if eps <= tol: accept accept fail karega aur unpredictably fall through karega; explicit isfinite guard hi sirf sahi fix hai. Numerically forced shrink deta hai , same floor-clamped retreat jaise Example 3. ✔


Recall Saare cells ke across quick self-test

Kaunse cell ko division guard chahiye? ::: C6 () — ko round-off pe floor karke guard karo (jaise ) ya return karo. Rejected step ke baad, ka kya hota hai? ::: Kuch nahi — pe raho aur retry karo (Examples 2, 3, 10). Jab huge ho (ya negative), kaun sa tolerance term dominate karta hai aur yeh positive kyun hai? ::: Relative wala, ; budget ko positive rakhta hai tab bhi jab ho (Example 7). System ke liye error ek number mein kaise turn hoti hai? ::: Per-component errors ka scaled RMS norm , phir ke budget ke against compare hota hai (Example 8). ke liye vs kab use karte hain? ::: Scalar equation → ; ek system (vector ) → norm . Last step cap kyun karo reject karne ki bajaye? ::: Ek chota step sirf error kam karta hai, toh yeh acceptable rehta hai jabki exactly pe land karta hai (Example 9). Agar ya NaN/Inf ho toh kya karna chahiye? ::: isfinite check se detect karo, phir reject force karo aur se shrink karo — comparison pe kabhi trust mat karo (Example 10).


Connections

  • Parent: Adaptive step-size — RK45, error control — woh theory jiske yeh examples drill hain.
  • Runge–Kutta Methods (RK4) — embedded pair ke neeche wala fixed-step method.
  • Local vs Global Truncation Error — kyun upar ke har factor ko drive karta hai.
  • Order of a Numerical Method — woh jo exponent fix karta hai.
  • Taylor Series Expansion — leading error constant ka origin.
  • Stiff ODEs and Stability — jab ek perfect controller bhi tiny steps lene par majboor ho (aur overflow Example 10 trigger kar sakta hai).
  • Dormand–Prince (RK45 used by scipy solve_ivp) — Example 8 mein RMS-norm error test.