Exercises — SymPy — symbolic algebra, calculus, ODE solving
5.4.20 · D4· Coding › Scientific Computing (Python) › SymPy — symbolic algebra, calculus, ODE solving
Level 1 — Recognition
Recall Solution
KYA: Brackets open karna = sab kuch multiply out karna. WHY woh tool: expand "multiply out" wala pencil move hai; factor iska reverse hai, simplify ek heuristic hai jo yeh kare ya na kare.
x = symbols('x')
expand((x + 3)*(x - 3)) # -> x**2 - 9Result: x**2 - 9.
Recall Solution
KYA: roots dhundhna. WHY solve: solve(eq, x) eq ko eq = 0 maanta hai aur symbolic roots ki list return karta hai — ek akela number nahi.
x = symbols('x')
solve(x**2 - 7*x + 12, x) # -> [3, 4]Result: [3, 4]. Dhyan raho yeh ek list hai, isliye 3 lene ke liye sol[0] index karna hoga.
Level 2 — Application
Recall Solution
KYA: symbol swap karo, exactness rakho. WHY .subs phir simplify: .subs x ko sqrt(3) se swap karta hai lekin expression un-collapsed chhod deta hai; simplify isko combine karta hai. Kaisa dikhta hai: .
x = symbols('x')
e = (x - 2)**2
simplify(e.subs(x, sqrt(3))) # -> 7 - 4*sqrt(3)Result: 7 - 4*sqrt(3). Agar decimal chahiye toh .evalf() add karo → 0.0718....
Recall Solution
KYA: ek product differentiate karna. WHY diff: yeh known rules (product + power + trig) automatically apply karta hai.
x = symbols('x')
diff(x**3 * sin(x), x) # -> x**3*cos(x) + 3*x**2*sin(x)Result: x**3*cos(x) + 3*x**2*sin(x). Do rules: product rule jisme (power rule deta hai ) aur (deta hai ).
Recall Solution
KYA: par curve ke neeche ka area. WHY tuple form (x,a,b): yeh Fundamental Theorem apply karta hai. Haath se: , toh .
x = symbols('x')
integrate(3*x**2 + 1, (x, 0, 2)) # -> 10Result: 10 (exact integer, 10.0 nahi).
Level 3 — Analysis
Recall Solution
KYA: ko ke around order 7 tak (lekin 7 include nahi) expand karo.
x = symbols('x')
cos(x).series(x, 0, 7)
# -> 1 - x**2/2 + x**4/24 - x**6/720 + O(x**7)Result: 1 - x**2/2 + x**4/24 - x**6/720 + O(x**7).
KYUN sirf even powers: ka Taylor coefficient hota hai (dekho Taylor and Maclaurin Series). ke liye, par har odd derivative hota hai, isliye woh coefficients zero ho jaate hain. Geometrically, ek even function hai (-axis ke across mirror-symmetric), aur even functions mein sirf even-power terms hote hain. Figure s01 dekho: mirror symmetry hi visual reason hai.

Recall Solution
KYA: ek quadratic ke roots jiske koi real solutions nahi hain.
x = symbols('x')
solve(x**2 + x + 1, x)
# -> [-1/2 - sqrt(3)*I/2, -1/2 + sqrt(3)*I/2]Result: [-1/2 - sqrt(3)*I/2, -1/2 + sqrt(3)*I/2].
KYUN yeh form: quadratic formula deta hai . Kyunki (jahan ), SymPy isko exact aur complex rakhta hai crash karne ki bajay. Yahi complex-root logic hai jo characteristic equation method mein ko oscillations mein badle ke liye use hota hai.
Recall Solution
KYA: compute karo. WHY limit aur diff nahi: hum derive karna chahte hain, assume nahi — yeh Limits and the definition of the derivative ki definition hai.
x, h = symbols('x h')
limit(((x + h)**3 - x**3)/h, h, 0) # -> 3*x**2
diff(x**3, x) # -> 3*x**2 (matches!)Result: dono 3*x**2 dete hain. Kaisa dikhta hai: expand karne par, ; se divide karo taaki mile; hone par sirf bachta hai.
Level 4 — Synthesis
Recall Solution
KYA: teen tools chain kiye — series, .removeO(), aur lambdify.
KYUN chain karo: series ek symbolic polynomial deta hai; .removeO() bookkeeping wala strip karta hai taaki woh real polynomial ban jaye; lambdify isko fast numeric function mein compile karta hai evaluation ke liye.
x = symbols('x')
p = sin(x).series(x, 0, 6).removeO() # x - x**3/6 + x**5/120
f = lambdify(x, p, 'math')
f(0.5) # -> 0.4794270833333333
math.sin(0.5) # -> 0.479425538604203Polynomial 0.47942708... deta hai, true value 0.47942553... hai — yeh 5 decimals tak agree karte hain kyunki humne tak ke terms rakhe. Kaisa dikhta hai: figure s02 mein polynomial (orange) true (blue) par overlay hai; 0 ke paas yeh chipke hain, door jaane par alag ho jaate hain.

Recall Solution
FORECAST: matlab "growth rate, size ke proportional hai" → exponential . General solution ; se pin hota hai. Toh predict hai .
x = symbols('x')
y = Function('y')
sol = dsolve(Eq(y(x).diff(x) - 3*y(x), 0), y(x), ics={y(0): 5})
sol # -> Eq(y(x), 5*exp(3*x))Result: Eq(y(x), 5*exp(3*x)) — forecast se match karta hai. Verify: aur , toh ✓ aur ✓.
Level 5 — Mastery
Recall Solution
KYA: structural identity aur mathematical equality mein farq samjho.
x = symbols('x')
(x + 2)**2 == x**2 + 4*x + 4 # -> False (different trees!)
simplify((x + 2)**2 - (x**2 + 4*x + 4)) == 0 # -> True
((x + 2)**2).equals(x**2 + 4*x + 4) # -> TrueKYUN False: == expression tree ki shape compare karta hai. (x+2)**2 ek power node ke roop mein store hai; x**2+4*x+4 ek sum node hai — different structures, isliye == False hai chahe woh functions ki tarah equal hon. Fix: subtract karo aur simplify karke 0 dekho, ya .equals use karo.
Recall Solution
KYA: improper integrals (upper limit oo hai, SymPy ki infinity).
x = symbols('x')
integrate(1/x**2, (x, 1, oo)) # -> 1
integrate(1/x, (x, 1, oo)) # -> ooResults: 1 aur oo. KYUN yeh farq: ka antiderivative hai, jo par approach karta hai, isliye area bounded hai: . Lekin ka antiderivative hai, jo bina bound ke badhta hai, isliye area infinite hai. Edge lesson: do curves jo dono zero ki taraf shrink hoti hain, wildly different total areas enclose kar sakti hain — itni tezi se shrink hoti hai ki ho jaata hai, nahi hoti.
Recall Solution
KYA: simplification is baat par depend karta hai ki SymPy ko assume karne ki permission kya hai.
x = symbols('x')
simplify(sqrt(x**2)) # -> sqrt(x**2) (stays, or Abs(x))
p = symbols('p', positive=True)
simplify(sqrt(p**2)) # -> pKYUN: general real ke liye, (socho : , nahi ). SymPy absolute value drop karne se mana kar deta hai kyunki woh prove nahi kar sakta ki hai. Jab tum positive=True declare karte ho, assumption isko sirf p tak simplify karne deta hai. Edge lesson: wahi expression ke do correct answers ho sakte hain sign domain par depend karke — SymPy ko hamesha apni assumptions batao.
Recall Solution
KYA: solve, list indexing, aur .evalf combine karo.
x = symbols('x')
roots = solve(x**2 - 2, x) # -> [-sqrt(2), sqrt(2)]
pos = roots[1] # sqrt(2)
pos # exact: sqrt(2)
pos.evalf(6) # -> 1.41421Results: exact root sqrt(2), decimal 1.41421. KYUN pehle index phir evalf: solve exact symbolic roots ki list return karta hai; hum positive wala pick karte hain, algebra ke liye exact rakhte hain, aur sirf ekdum end mein float mein collapse karte hain.
Active Recall
Recall
(x+2)**2 == x**2+4*x+4 False kyun return karta hai?
== expression-tree structure compare karta hai, mathematical value nahi. simplify(a-b)==0 ya a.equals(b) use karo.
Recall
series ko lambdify karne se pehle kya call karna zaroori hai?
.removeO(), taaki O(x**n) order term strip ho jaye jo numerically evaluable nahi hai.
Recall
infinite kyun hai lekin finite kyun hai? Antiderivative bina bound ke badhta hai; ek finite limit () approach karta hai.
Recall SymPy
sqrt(x**2) ko x mein simplify kyun nahi karta?
Kyunki general real ke liye yeh ke barabar hai. Reduction allow karne ke liye positive=True declare karo.
Connections
- SymPy — symbolic algebra, calculus, ODE solving
- Limits and the definition of the derivative
- Taylor and Maclaurin Series
- Ordinary Differential Equations — characteristic equation method
- Lambdify — bridging SymPy to NumPy for plotting
- NumPy — numerical arrays