5.4.20 · D3 · Coding › Scientific Computing (Python) › SymPy — symbolic algebra, calculus, ODE solving
Intuition Pehle "scenario matrix" kyun?
Jab koi real problem samne aaye, sabse mushkil kaam hota hai yeh pehchanna ki yeh kis tarah ki problem hai. SymPy solve, diff, integrate, dsolve kar sakta hai... lekin kaun sa use karein, aur har ek mein kaunse traps chhuppe hain? Yeh page ek matrix — yaani ek grid — rakhta hai jo parent note ke har possible case-class ko cover karta hai — phir har cell ka ek example solve karta hai. Iske baad koi bhi aisa scenario nahi bachega jiska jawab tumne na dekha ho.
Yahan "case-class" ka matlab hai: inputs ki ek family jo same tarah behave karti hai (jaise "real roots wala quadratic" vs "complex roots wala quadratic"). Alag-alag classes ko alag handling chahiye, isliye hum har ek ko alag se dekhte hain.
Yeh page parent SymPy note ke tools assume karta hai. Agar koi symbol unfamiliar lage, toh use use karne se pehle neeche define kiya gaya hai.
Har row ko padhho "is topic se banti ek situation" ki tarah, aur cell ko padhho "woh twist jo ise apna alag case banati hai."
#
Category
Specific case (twist)
Worked in
A
Solving
Quadratic with two real roots
Ex 1
B
Solving
Quadratic with complex roots (discriminant < 0)
Ex 2
C
Solving
Degenerate equation: no solution / infinitely many
Ex 3
D
Calculus
Derivative limit se recover kiya (first principles)
Ex 4
E
Calculus
Definite integral jo exact rational deta hai vs decimal
Ex 5
F
Calculus
Ek obvious nahi wala limit (0/0 form)
Ex 6
G
ODE
Second-order, complex characteristic roots (oscillation)
Ex 7
H
ODE
First-order with initial condition (real-world growth word problem)
Ex 8
I
Traps
Exam twist: == vs true equality, aur solve ka output padhna
Ex 9
Har woh symbol jo hum use karte hain — solve, diff, limit, integrate, dsolve, discriminant, I — woh usi waqt explain hoga jab woh pehli baar aayega.
x 2 − 7 x + 12 = 0 solve karo.
Forecast: aage padhne se pehle guess karo: kaun se do nice whole numbers ka product 12 hai aur sum 7 ? Woh kaun hain?
Step 1 — Symbol aur expression set up karo.
x = symbols( 'x' )
solve(x ** 2 - 7 * x + 12 , x) # -> [3, 4]
Yeh step kyun? symbols('x') x ko ek unknown banata hai jo apni algebra retain karta hai (koi number nahi). solve(expr, x) expr ko expr = 0 ki tarah padhta hai — kyunki koi bhi equation a = b , a − b = 0 ban jaati hai, isliye "ek expression ke roots" universal form hai.
Step 2 — Answer samjho. SymPy andar se x 2 − 7 x + 12 = ( x − 3 ) ( x − 4 ) factor karta hai. Ek product tab hi zero hota hai jab koi factor zero ho, isliye x = 3 ya x = 4 .
Verify: dono ko wapas plug karo. 3 2 − 7 ⋅ 3 + 12 = 9 − 21 + 12 = 0 . ✓ Aur 4 2 − 7 ⋅ 4 + 12 = 16 − 28 + 12 = 0 . ✓
Yahan twist hai discriminant . a x 2 + b x + c ke liye, discriminant hai b 2 − 4 a c — quadratic formula mein square root ke andar wala number. Yeh ek sawaal ka jawab deta hai: "kya roots real hain?" Agar yeh negative hai, toh ek negative number ka square root aata hai, aur SymPy ise symbol I se likhta hai, jiska matlab hai − 1 (imaginary unit).
Definition Imaginary unit
I
I SymPy ka naam hai − 1 ke liye, yaani woh number jiska square − 1 hai. Yeh tab appear hota hai jab hum kisi negative number ka square root lete hain. Board par yeh humein 2 ± 3 I jaisi answers likhne deta hai jo ordinary number line par exist nahi karti.
x 2 − 4 x + 13 = 0 solve karo.
Forecast: discriminant = ( − 4 ) 2 − 4 ( 1 ) ( 13 ) = 16 − 52 = − 36 < 0 . Kyunki yeh negative hai, predict karo: koi real answer nahi , roots ek complex pair ki form mein aayenge.
Step 1 — Pehle discriminant check karo.
a, b, c = 1 , - 4 , 13
b ** 2 - 4 * a * c # -> -36
Yeh step kyun? Discriminant ka sign hume batata hai answer kis tarah ka hoga compute karne se pehle — isi se pata chalta hai ki hum kis cell mein hain.
Step 2 — Solve karo.
x = symbols( 'x' )
solve(x ** 2 - 4 * x + 13 , x) # -> [2 - 3*I, 2 + 3*I]
Yeh step kyun? − 36 = 6 I , aur quadratic formula deta hai x = 2 4 ± 6 I = 2 ± 3 I .
Verify: x = 2 + 3 I substitute karo:
( 2 + 3 I ) 2 − 4 ( 2 + 3 I ) + 13 . Kyunki ( 2 + 3 I ) 2 = 4 + 12 I + 9 I 2 = 4 + 12 I − 9 = − 5 + 12 I , toh milta hai ( − 5 + 12 I ) − ( 8 + 12 I ) + 13 = 0 . ✓
Twist yeh hai: kabhi kabhi koi equation sach mein quadratic nahi hoti , ya uske koi ya infinite solutions hote hain. Tumhe empty list [] aur "hamesha true" wala case pehchanna hoga.
Worked example Solve karo (a)
x + 1 = x + 2 aur (b) 0 ⋅ x = 0 .
Forecast: (a) kehta hai 1 = 2 — impossible. (b) har x ke liye true hai. Predict karo: (a) → no solutions, (b) → SymPy "saare numbers" list nahi kar sakta.
Step 1 — Impossible wala.
x = symbols( 'x' )
solve(Eq(x + 1 , x + 2 ), x) # -> []
Yeh step kyun? Eq(lhs, rhs) ek genuine equation banata hai. Subtract karo: x + 1 − ( x + 2 ) = − 1 , jo kabhi 0 nahi hota. SymPy empty list [] return karta hai — honest answer "koi bhi x kaam nahi karta."
Step 2 — Hamesha true wala.
solve(Eq( 0 * x, 0 ), x) # -> [] (SymPy warn bhi karta hai; relation trivially true hai)
Yeh step kyun? 0 = 0 sabhi x ke liye hold karta hai, isliye koi specific root return karne ko hai hi nahi. [] ko sahi se padhna (empty ≠ error) yahi seekhna hai.
[!mistake] Steel-man: "solve ne [] return kiya, mera code toot gaya."
Aisa lagta hai jaise failure hai. Lekin [] ek valid mathematical answer hai: "no solution." Fix: empty list ko information samjho, crash nahi.
Verify: x + 1 = x + 2 ⇒ 1 = 2 sabhi x ke liye false hai. ✓ (Koi root nahi, [] se match karta hai.)
Yahan tool hai limit. Ek derivative f ′ ( x ) f ki instantaneous steepness measure karta hai; yeh ek limit ke roop mein define hota hai:
f ′ ( x ) = lim h → 0 h f ( x + h ) − f ( x )
Yeh fraction do nearby points se guzarne wali line ki slope hai; hum gap h ko zero tak squeeze karte hain taaki us point par slope mile. Dekho Limits and the definition of the derivative .
f ( x ) = x 3 ka derivative scratch se recover karo, phir diff se confirm karo.
Forecast: power rule kehta hai d x d x 3 = 3 x 2 . Hum ise assume nahi karenge — hum ise derive karenge.
Step 1 — Difference quotient banao.
x, h = symbols( 'x h' )
f = x ** 3
q = (f.subs(x, x + h) - f) / h # ((x+h)**3 - x**3)/h
Yeh step kyun? .subs(x, x+h) input ko h se aage khiskaata hai; poora fraction figure mein red secant line ki slope hai.
Step 2 — Limit lo.
limit(q, h, 0 ) # -> 3*x**2
Yeh step kyun? limit(q, h, 0) poochta hai "jab do points merge ho jaate hain toh slope kya approach karti hai?" — tangent ka geometric meaning (blue line).
Step 3 — Built-in rule se cross-check karo.
diff(f, x) # -> 3*x**2 (same!)
Yeh step kyun? diff power rule use karta hai, jo khud bhi isi limit se derive hota hai — toh agreement consistency prove karta hai.
Verify: ( x + h ) 3 = x 3 + 3 x 2 h + 3 x h 2 + h 3 expand karo. x 3 subtract karo, h se divide karo: 3 x 2 + 3 x h + h 2 . h → 0 karo: 3 x 2 . ✓
Ek integral ∫ a b f d x curve ke neeche a se b tak signed area hai. Twist yeh hai: SymPy ise exact (ek fraction ke roop mein) rakhta hai, aur tum choose karte ho kab decimal mein collapse karna hai .evalf() se.
∫ 0 2 ( x 2 + 1 ) d x exactly compute karo, phir decimal mein.
Forecast: antiderivative hai 3 x 3 + x . 0 → 2 evaluate karo: 3 8 + 2 = 3 14 ≈ 4.6 6 .
Step 1 — Definite integral (exact).
x = symbols( 'x' )
integrate(x ** 2 + 1 , (x, 0 , 2 )) # -> 14/3
Yeh step kyun? Tuple (x, 0, 2) Fundamental Theorem trigger karta hai: ∫ a b f = F ( b ) − F ( a ) . SymPy exact rational 14/3 return karta hai, koi rounded 4.6666... nahi.
Step 2 — Abhi decimal lo.
(Rational( 14 , 3 )).evalf() # -> 4.66666666666667
Yeh step kyun? .evalf() "please ab round karo" button hai. Hum ise last mein maangte hain, taaki algebra mein koi rounding error na ghuse.
Verify: F ( x ) = 3 x 3 + x . F ( 2 ) = 3 8 + 2 = 3 14 ; F ( 0 ) = 0 . Difference = 3 14 . ✓
Twist yeh hai: target value plug karne par 0 0 milta hai, jo direct substitution se undefined hai. Isliye hi limit tool exist karta hai — yeh woh value dhundta hai jisko expression approach karta hai , chahe wahan directly evaluate na ho sake. Deeper "why" ke liye dekho Taylor and Maclaurin Series .
x → 0 lim x sin x evaluate karo.
Forecast: x = 0 par fraction hai 0 s i n 0 = 0 0 — indeterminate. Lekin graph dikhata hai ki curve ek clean value ki taraf ja rahi hai . Guess karo woh value.
Step 1 — Direct substitution try karo (aur dekhna fail hogi). x = 0 rakhne par 0/0 milta hai: koi answer nahi. Isliye hume limit chahiye , substitution nahi.
Step 2 — SymPy se limit maango.
x = symbols( 'x' )
limit(sin(x) / x, x, 0 ) # -> 1
Yeh step kyun? limit x = 0 par nahi, balki uske paas behaviour inspect karta hai, 0/0 trap se bachte hue.
Step 3 — Series ke zariye dekhte hain kyun. sin x = x − 6 x 3 + ⋯ , toh x s i n x = 1 − 6 x 2 + ⋯ → 1 .
(sin(x) / x).series(x, 0 , 4 ) # -> 1 - x**2/6 + O(x**4)
Yeh step kyun? Leading term 1 hi limit hai — series answer ko visible bana deti hai.
Verify: sin ( x ) / x ki 0 par series ka constant term 1 ke barabar hai. ✓
Ek ODE (ordinary differential equation) ek function ko uske derivatives se link karta hai; ise solve karne ka matlab hai function dhundhna, koi number nahi. dsolve tool hai. Twist yahan yeh hai: characteristic roots complex hain, jo oscillation (cos , sin ) encode karte hain. Dekho Ordinary Differential Equations — characteristic equation method .
y ′′ + 9 y = 0 solve karo.
Forecast: guess karo y = e r x . Toh r 2 + 9 = 0 ⇒ r = ± 3 I . Imaginary roots ± 3 I ka matlab hai frequency 3 ke saath oscillation : predict karo cos ( 3 x ) aur sin ( 3 x ) .
Step 1 — Ek unknown function declare karo.
x = symbols( 'x' )
y = Function( 'y' )
Yeh step kyun? Function('y') y ko x ka function banata hai (toh y(x).diff(x, 2) ka matlab hai y ′′ ), unlike symbols jo ek plain unknown number banata hai.
Step 2 — Solve karo.
dsolve(Eq(y(x).diff(x, 2 ) + 9 * y(x), 0 ), y(x))
# -> Eq(y(x), C1*sin(3*x) + C2*cos(3*x))
Yeh step kyun? Characteristic equation r 2 + 9 = 0 ke roots hain ± 3 I ; complex roots α ± β I solutions dete hain e α x ( cos β x , sin β x ) . Yahan α = 0 , β = 3 → pure waves, figure mein drawn.
Step 3 — C 1 , C 2 padhna. Yeh arbitrary constants hain — SymPy poori family of solutions return karta hai kyunki koi starting condition nahi di gayi.
Verify: maano y = sin ( 3 x ) . Toh y ′′ = − 9 sin ( 3 x ) , isliye y ′′ + 9 y = − 9 sin ( 3 x ) + 9 sin ( 3 x ) = 0 . ✓ (cos ( 3 x ) ke liye bhi same.)
Bacteria. Ek colony is tarah grow karti hai ki uski increase ki rate uski current size ki aadhi hai: y ′ = 2 1 y . Time 0 par 20 bacteria hain. y ( x ) aur x = 4 par population dhundho.
Forecast: "size ke proportional rate" exponential growth hai. General shape C 1 e x /2 ; start y ( 0 ) = 20 pin karta hai C 1 = 20 . Predict karo y ( x ) = 20 e x /2 .
Step 1 — Words ko ODE mein translate karo. "rate of increase" hai y ′ ; "equals half its size" hai 2 1 y . Toh y ′ − 2 1 y = 0 .
Yeh step kyun? Har growth word problem ek aisi equation banti hai jo y ′ ko y se link karti hai — yeh pehchanna hi poori skill hai.
Step 2 — Initial condition ke saath solve karo.
x = symbols( 'x' )
y = Function( 'y' )
dsolve(Eq(y(x).diff(x) - Rational( 1 , 2 ) * y(x), 0 ), y(x),
ics = {y( 0 ): 20 }) # -> Eq(y(x), 20*exp(x/2))
Yeh step kyun? ics={y(0): 20} starting count supply karta hai, jo first-order ODE ke ek arbitrary constant ko fix karta hai.
Step 3 — x = 4 par population.
( 20 * exp(Rational( 1 , 2 ) * 4 )).evalf() # -> 147.781...
Yeh step kyun? x = 4 substitute karo: 20 e 2 ≈ 147.78 . Units: bacteria (ek count).
Verify: y ( 0 ) = 20 e 0 = 20 ✓ (start se match karta hai). ODE check karo: y ′ = 20 ⋅ 2 1 e x /2 = 2 1 ( 20 e x /2 ) = 2 1 y . ✓
Worked example Ek problem mein do classic exam traps.
(a) Kya (x+2)**2 == x**2 + 4*x + 4 True return karta hai?
(b) solve(x**2 - 2, x) se positive root decimal mein nikalo.
Forecast: (a) yeh mathematically equal hain — lekin predict karo ki == False bolega. (b) roots hain ± 2 ; positive decimal hai 1.4142 …
Step 1 — == trap.
x = symbols( 'x' )
(x + 2 ) ** 2 == x ** 2 + 4 * x + 4 # -> False
simplify((x + 2 ) ** 2 - (x ** 2 + 4 * x + 4 )) == 0 # -> True
Yeh step kyun? == structural identity check karta hai (same tree), mathematical equality nahi. Dono forms alag likhe hain, isliye == False bolta hai. True equality test karne ke liye, difference ko simplify karke 0 dekho.
Step 2 — solve ki list padhna.
sol = solve(x ** 2 - 2 , x) # -> [-sqrt(2), sqrt(2)]
sol[ 1 ].evalf() # -> 1.4142135623731
Yeh step kyun? solve symbolic roots ki list return karta hai (sqrt ke saath), koi single number nahi. Positive root ke liye sol[1] index karo, phir float pane ke liye .evalf().
Verify: ( 2 ) 2 − 2 = 2 − 2 = 0 ✓, aur ( x + 2 ) 2 − ( x 2 + 4 x + 4 ) = 0 identically ✓.
Recall Agar quadratic ka discriminant negative ho, toh
solve kya return karta hai aur kyun?
Ek complex-conjugate pair jaise 2 ± 3*I, kyunki negative imaginary unit I = − 1 produce karta hai.
Recall
solve(...) ne [] diya. Kya yeh error hai?
Nahi — [] ka matlab hai "koi solution exist nahi karta," ek valid mathematical answer (jaise x + 1 = x + 2 ).
Recall
lim x → 0 sin ( x ) / x compute kyun karte hain, x = 0 substitute karne ki jagah?
Direct substitution indeterminate 0/0 deta hai; limit woh value dhundta hai jisko expression approach karta hai (= 1 ).
Recall Complex characteristic roots
± 3 I kis tarah ke ODE solution mein badte hain?
Oscillations: C 1 sin ( 3 x ) + C 2 cos ( 3 x ) (frequency 3 ).
Recall
y ′ = 2 1 y , y ( 0 ) = 20 ka answer 20*exp(x/2) kyun hai?
"Rate ∝ size" → exponential; 2 1 exponent set karta hai, aur y ( 0 ) = 20 constant fix karta hai.
"Sign dekho, list padhho, trap ko limit karo." — discriminant sign batata hai real vs complex; solve ek list deta hai; 0/0 ko limit chahiye, .subs nahi.
Limits and the definition of the derivative (Ex 4, Ex 6)
Taylor and Maclaurin Series (sin x / x → 1 kyun)
Ordinary Differential Equations — characteristic equation method (Ex 7, Ex 8)
Lambdify — bridging SymPy to NumPy for plotting (in symbolic answers ko curves mein badlo)
NumPy — numerical arrays (floats vs upar ke exact rationals)
solve mein negative discriminant kya produce karta hai?I (√−1) use karte hue ek complex-conjugate root pair.
Jab koi solution exist na kare toh solve kya return karta hai? Ek empty list [].
x³ ka derivative first principles se kaise nikaalte hain? limit(((x+h)**3 - x**3)/h, h, 0) → 3*x**2.
integrate(x**2+1,(x,0,2)) ki value?14/3 (exact rational).
limit(sin(x)/x, x, 0) ki value?1.
y''+9y=0 ka solution?C1*sin(3*x)+C2*cos(3*x).
y'=y/2 with y(0)=20 ka solution? 20*exp(x/2).
(x+2)**2 == x**2+4*x+4 False kyun deta hai?== structural identity test karta hai, math equality nahi; simplify(a-b)==0 use karo.