5.4.24 · D5 · HinglishScientific Computing (Python)
Question bank — Implementing numerical integration from scratch — trapezoidal, Simpson's
5.4.24 · D5· Coding › Scientific Computing (Python) › Implementing numerical integration from scratch — trapezoida
Shuru karne se pehle, ek shared vocabulary reminder taaki koi bhi symbol unexplained na rahe:
True or false — justify karo
Trapezoidal rule hamesha overestimate hoti hai.
False — ye overestimate sirf tab karta hai jab curve convex ho (upar ki taraf bend kare, jaise ), kyunki straight chord curve ke upar baith jaati hai; concave curve ke liye chord neeche baith jaati hai aur ye underestimate karta hai.
Simpson's rule mein zyada slices hamesha kam slices se better hoti hain.
Zyada tar true hai lekin guaranteed nahi — chhota term ko shrink karta hai, phir bhi bahut rough ya non-smooth ke liye error constant misbehave kar sakta hai, aur zyada evaluations se rounding error badhta hai.
Agar koi rule degree-2 polynomials ke liye exact hai toh degree-3 ke liye bhi exact hona chahiye.
True Simpson ke liye specifically — uska symmetric 1-4-1 stencil odd (cubic) error term ko cancellation se khatam kar deta hai, ek free bonus, toh ye degree 3 tak exact hai jabki sirf ek parabola fit karta hai.
Trapezoidal, Simpson's ka ek special case hai.
False — ye alag Newton-Cotes members hain (Newton-Cotes formulas): trapezoid degree-1 line fit karta hai, Simpson degree-2 parabola. Koi bhi doosre ko contain nahi karta.
double karne se trapezoidal error roughly aadhi ho jaati hai.
False — trapezoid error hai, aur double karne se aadha ho jaata hai, toh error roughly one quarter ho jaata hai, na ki aadha.
Straight-line function ke liye, trapezoid aur Simpson identical, exact answers dete hain.
True — dono degree-1 polynomials ke liye exact hain, toh dono sahi area return karte hain zero error ke saath, chahe kuch bhi ho.
Simpson's rule ko even chahiye kyunki odd se negative ho jaata hai.
False — kisi bhi positive ke liye positive hai; asli wajah ye hai ki har parabola ek pair of slices span karta hai, toh odd count mein ek dangling slice bina partner ke reh jaati hai.
Composite trapezoid mein weights hamesha tak sum hote hain.
True — nodes pe pattern sum karke deta hai, aur , jo bilkul wahi width hai jise average height se multiply karna hoga.
par har method true integral tak converge karta hai.
True exact arithmetic mein — dono Riemann-type sums hain (Riemann sums) jinki limit definite integral hoti hai; floating point mein, rounding eventually improvement rokta hai.
Spot the error
Ek student Simpson weights set karta hai. Kya galat hai?
Sirf odd-index (pair-centre) nodes ko weight 4 milta hai; even interior nodes ko weight 2 milta hai kyunki wo do adjacent parabolas ke beech shared hote hain. Blanket-4 shared ones ko double-count karta hai.
Ek student interior trapezoid nodes sum karne ke liye for i in range(1, n+1) likhta hai.
Isse node (right endpoint) interior point ki tarah include ho jaata hai, use twice add karta hai aur galat weight deta hai — interior nodes sirf
range(1, n) hain, kyunki points hain lekin interior points hain.Koi claim karta hai ki Simpson ke liye exact nahi ho sakta kyunki ye sirf parabolas fit karta hai.
Galat — fitted curve parabola hai, lekin Simpson ke integration weights cubic error term ko symmetry se cancel kar dete hain, toh answer cubics ke liye bhi exact hai.
Ek program trapezoid compute karta hai sum(f(a+i*h) for i in range(n+1)) * h se.
Ye har node ko weight 1 deta hai, lekin endpoints ko milna chahiye. Fix ye hai ki ka aadha subtract karo, ya equivalently sum se shuru karo phir interior nodes add karo.
Koi pe integrate karta hai jahan aur positive area report karta hai.
ke saath, negative hai, toh sum naturally negative aata hai — sahi signed result; ise positive force karna ke against hoga.
Ek student Simpson ke saath use karta hai aur "bas leftover slice ko average kar leta hai."
odd hai; saaf 1-4-1 stencil teen slices tile nahi kar sakta. Ya toh even choose karo ya odd trailing slice pe alag rule lagao (jaise Simpson ya trapezoid) — sirf average nahi.
Why questions
Endpoints ko interior nodes se alag weight kyun milta hai?
Har endpoint sirf ek trapezoid/parabola se belong karta hai, jabki har interior node do adjacent shapes ke beech shared hota hai, isliye interior nodes do baar count hote hain (trapezoid) ya shared weight 2 lete hain (Simpson).
Simpson's rule ko pairs of slices mein points kyun chahiye, single slices mein nahi?
Ek parabola teen points se determine hota hai, aur teen consecutive nodes do slices span karte hain; ek single slice ke do points pe ek parabola fit nahi ho sakti.
Simpson's ka error kyun hai jabki trapezoid sirf hai?
Error ki Taylor series expansion dikhati hai ki trapezoid ka leading term aur involve karta hai, jabki Simpson ka symmetric stencil aur terms cancel kar deta hai, aur bachta hai.
Trapezoid ek convex (upar-bending) curve ko overestimate kyun karta hai?
Convex curve pe do points ko jodne wali straight chord curve ke upar hoti hai unke beech har jagah, isliye trapezoid area curve ke neeche sahi area se zyada hota hai.
Simpson ke liye exact kyun hai lekin ke liye nahi?
Simpson degree 3 tak exact hai; degree 4 hai, toh uska -driven error term nonzero hai aur rule ek residual chhod deta hai.
Hum trapezoid results combine karke Simpson build kyun kar sakte hain?
Richardson extrapolation term ko aur ko us ratio mein combine karke cancel karta hai jo use eliminate karta hai; result algebraically Simpson ke identical hai, isliye aadha karke extrapolate karna kaam karta hai.
Dono rules ko "weighted sums " kyun phrase karte hain?
Ye reveal karta hai ki saare Newton-Cotes rules (Newton-Cotes formulas) ek skeleton share karte hain aur sirf weights mein differ karte hain, toh inhe code karne ka matlab hai weight table badalna, loop structure nahi.
Edge cases
ke liye trapezoid kya deta hai?
Single-trapezoid estimate — poore interval ke across ek straight chord, usually crude lekin exact agar linear ho.
Simpson ke liye sabse chhota valid kya hai, aur kyun?
— bilkul ek pair of slices, teen nodes, ek parabola; impossible hai kyunki sirf do points se parabola fit nahi ho sakti.
Kya hota hai agar ke andar mein ek sharp corner (not smooth) ho?
Dono error estimates smoothness assume karte hain ( ya exist kare); corner pe wo derivatives blow up karte hain, toh promised orders fail ho jaate hain aur convergence slow ho jaati hai — accuracy recover karne ke liye corner pe node rakh do.
Agar (zero-width interval) ho toh kya?
, har slice collapse ho jaati hai, aur sum hai — correct, kyunki kisi bhi ke liye.
Agar kahin -axis ke neeche chali jaaye toh?
Rules signed area return karte hain: axis ke neeche ke regions negative height contribute karte hain, toh result true signed integral se match karta hai, geometric area se nahi.
Agar code mein Simpson ko odd diya jaaye toh kya hoga?
Ek sahi implementation error raise karta hai (jaise parent ke
raise ValueError mein hai); ek careless implementation silently last node ko galat weight deta hai, ek wrong answer produce karta hai jo sahi lagta hai — sabse khatarnak bug.Dono rules ka par limiting behaviour kya hai?
Dono exact arithmetic mein exact tak converge karte hain; floating point mein, rounding error eventually dominate karta hai aur aur badhane se result kharab ho jaata hai.
Recall Aage badhne se pehle one-line self-test
Agar tum "edges alag kyun count hote hain?" aur "Simpson ke liye even kyun matter karta hai?" apne words mein bina dekhke answer kar sako, toh tum is topic ke malik ho. Warna Newton-Cotes formulas aur Polynomial interpolation links dobara padho.
Connections
- Riemann sums — woh limiting sum jise dono rules approximate karte hain.
- Newton-Cotes formulas — wo family jise ye do rules open karte hain.
- Polynomial interpolation — kyun Simpson = fitted parabola ko integrate karna hai.
- Taylor series — jahan se / orders aate hain.
- Richardson extrapolation — trapezoids combine karke Simpson / Romberg build karna.
- scipy.integrate.quad — adaptive production tool jo ye sab chhupata hai.