5.4.24 · D4 · HinglishScientific Computing (Python)

ExercisesImplementing numerical integration from scratch — trapezoidal, Simpson's

2,253 words10 min read↑ Read in English

5.4.24 · D4 · Coding › Scientific Computing (Python) › Implementing numerical integration from scratch — trapezoida

Do pictures hain jo hum baar baar use karenge — abhi inhe dekh lo taaki neeche ke words inhe point kar sakein.

Figure — Implementing numerical integration from scratch — trapezoidal, Simpson's
Figure — Implementing numerical integration from scratch — trapezoidal, Simpson's

Level 1 — Recognition

Recall Solution

Simpson's stencil har pair ke liye 1-4-1 hota hai. Teen pairs ko six slices ke across chain karo.

  • Endpoints () ek-ek parabola se belong karte hain → weight .
  • Odd-index nodes () pair-centres hain (woh peak jahan parabola bend karti hai) → weight .
  • Even interior nodes () do adjacent parabolas ke beech share hote hain → weight .

Toh pattern hai Check karo: weights ka sum hai, aur constant ke liye recover karta hai. ✓

Recall Solution

Points , isliye . Phir . Simpson ko even chahiye; even hai ✓ — allowed hai.


Level 2 — Application

Recall Solution

. Nodes aur heights: Exact value: . Trapezoid overshoot karta hai (curve convex hai, seedhi lids uske upar baith jaati hain) approximately se.

Recall Solution

Same nodes/heights jaise L2.1 mein hain. Weights : Yeh exact ke exactly barabar hai, kyunki degree ka hai aur Simpson saari parabolas ko exactly integrate karta hai.


Level 3 — Analysis

Recall Solution

Prediction: . half karne par milta hai. Toh error ke factor se shrink hona chahiye.

Check. ke saath:

  • (), nodes , : Error .
  • (), nodes , : Ruko — bracket carefully recompute karo: ; interior sum , doubled ; total ; . Error .

Ratio . ✓ prediction hold karti hai.

Recall Solution

Kyun: Ek cubic par parabola fit karne ki error leftover cubic term se aati hai. Symmetric interval par cubic error term ka odd function hota hai, aur symmetric limits par ek odd function ka integral hota hai. Simpson nodes symmetric hain, isliye odd leftover exactly zero integrate ho jaata hai — cubic term error mein kuch contribute nahi karta. Yahi woh "free" extra degree hai.

Confirm: true value (odd function). Simpson ke saath , : Exact, jaisa promise tha. ✓


Level 4 — Synthesis

Recall Solution

Pehle do trapezoid values nikalo ( phir ).

  • (, nodes , ):
  • () L2.1 se.

Richardson combine karo: Yeh se exactly match karta hai. Kyun kaam karta hai: trapezoid error hai. half karne par leading term se scale hota hai; weights aur over precisely isliye choose kiye gaye hain taaki terms cancel ho jaayein, ek aisi method chhod ke jo tak accurate hai — yahi Simpson hai. Yeh Richardson extrapolation ka ek step hai; ise iterate karne par Romberg integration milti hai.

Recall Solution

, nodes , heights Simpson on (nodes , weights ): Bracket ; Trapezoid on (nodes ): Total . Error — pure trapezoid se kaafi better, dangling slice ko honestly handle karta hai.


Level 5 — Mastery

Recall Solution

Trapezoid exact: koi bhi straight line, jaise on . Trapezoid construction se degree-1 polynomials ke liye exact hota hai (seedha lid function hi hota hai). Exact value ; ke saath, . ✓ Simpson bhi deta hai lekin tumhe kuch extra nahi milta — capture karne ke liye koi curvature hi nahi hai.

Error orders fail: lo par. Iska derivative par blow up karta hai, isliye / ke peeche wala Taylor-series argument (jisme bounded higher derivatives ki zaroorat hoti hai) break down ho jaata hai. Observed convergence se slower hoti hai. Lesson: advertised error orders assume karte hain ki kaafi smooth hai (trapezoid ke liye bounded 2nd derivative, Simpson ke liye 4th). Kinks, cusps, aur singularities guarantee void kar dete hain — wahan scipy.integrate.quad ki adaptive machinery use karo.

Recall Solution

Correct (weights ): . Exact ✓. Buggy (weights ): . Bug se error — bahut hi galat answer ( zyada bada). Sirf par extra weight (jahan ) ne akele add kar diya. Isliye endpoint weighting #1 silent bug hai: yeh crash nahi karta, bas quietly garbage return karta hai.

Recall Solution

(, nodes , ): Bracket ; Error — chhota lekin nonzero (degree , Simpson ke exact degree se zyada hai). (): weighted sum compute karne par milta hai, error . Ratio . ✓ order confirm ho gaya.


Recall Self-test summary (cloze)

Slice width hai ==. Simpson's per-pair stencil hai 1-4-1, scale hota hai == se. Trapezoid error order ::: Simpson error order ::: Highest exact degree — trapezoid / Simpson ::: 1 / 3 subintervals ke liye points ::: Richardson combo jo trapezoids se Simpson deta hai :::

Connections