5.4.20 · D3Scientific Computing (Python)

Worked examples — SymPy — symbolic algebra, calculus, ODE solving

2,847 words13 min readBack to topic

This page assumes the tools from the parent SymPy note. If a symbol here feels unfamiliar, it is defined below before use.


The scenario matrix

Read each row as "a situation this topic creates" and the cell as "the twist that makes it its own case."

# Category The specific case (the 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 recovered from the limit (first principles) Ex 4
E Calculus Definite integral giving an exact rational vs a decimal Ex 5
F Calculus A limit that is not obvious (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, and reading solve's output Ex 9

Every symbol we use — solve, diff, limit, integrate, dsolve, discriminant, I — is unpacked at the moment it appears.


Ex 1 — Cell A: quadratic, two real roots


Ex 2 — Cell B: quadratic, complex roots

Here the twist is the discriminant. For , the discriminant is — the number under the square root in the quadratic formula. It answers one question: "are the roots real?" If it is negative, the square root of a negative number appears, and SymPy writes it with the symbol , meaning (the "imaginary unit").


Ex 3 — Cell C: degenerate equations

The twist: sometimes an equation is not really a quadratic, or has no or infinitely many solutions. You must recognise the empty list [] and the "always true" case.


Ex 4 — Cell D: derivative from the limit

The tool here is limit. A derivative measures the instantaneous steepness of ; it is defined as a limit:

The fraction is the slope of the line through two nearby points; we squeeze the gap to zero to get the slope at the point. See Limits and the definition of the derivative.

Figure — SymPy — symbolic algebra, calculus, ODE solving

Ex 5 — Cell E: exact rational vs decimal integral

An integral is the signed area under the curve from to . The twist: SymPy keeps it exact (a fraction), and you choose when to collapse to a decimal with .evalf().


Ex 6 — Cell F: a limit that is not obvious (0/0)

The twist: plugging in the target value gives , which is undefined by direct substitution. This is exactly why the tool limit exists — it finds the value the expression approaches, even where it cannot be evaluated directly. See Taylor and Maclaurin Series for the deeper "why."


Ex 7 — Cell G: second-order ODE, oscillating solution

An ODE (ordinary differential equation) links a function to its derivatives; solving it means finding the function, not a number. dsolve is the tool. The twist here: the characteristic roots are complex, which encode oscillation (). See Ordinary Differential Equations — characteristic equation method.

Figure — SymPy — symbolic algebra, calculus, ODE solving

Ex 8 — Cell H: first-order ODE with an initial condition (word problem)


Ex 9 — Cell I: exam twist — == and reading solve output


Active Recall

Recall If a quadratic's discriminant is negative, what does

solve return and why? A complex-conjugate pair like 2 ± 3*I, because produces the imaginary unit .

Recall

solve(...) gave []. Is that an error? No — [] means "no solution exists," a valid mathematical answer (e.g. ).

Recall Why compute

instead of substituting ? Direct substitution gives the indeterminate ; limit finds the value the expression approaches ().

Recall Complex characteristic roots

turn into what kind of ODE solution? Oscillations: (frequency ).

Recall Why is

20*exp(x/2) the answer to ? "Rate ∝ size" → exponential; the sets the exponent, and fixes the constant.

Connections

  • Limits and the definition of the derivative (Ex 4, Ex 6)
  • Taylor and Maclaurin Series (why )
  • Ordinary Differential Equations — characteristic equation method (Ex 7, Ex 8)
  • Lambdify — bridging SymPy to NumPy for plotting (turn these symbolic answers into curves)
  • NumPy — numerical arrays (floats vs the exact rationals above)
What does a negative discriminant produce in solve?
A complex-conjugate root pair using I (√−1).
What does solve return when no solution exists?
An empty list [].
How do you get the derivative of x³ from first principles?
limit(((x+h)**3 - x**3)/h, h, 0)3*x**2.
Value of integrate(x**2+1,(x,0,2))?
14/3 (exact rational).
Value of limit(sin(x)/x, x, 0)?
1.
Solution of y''+9y=0?
C1*sin(3*x)+C2*cos(3*x).
Solution of y'=y/2 with y(0)=20?
20*exp(x/2).
Why does (x+2)**2 == x**2+4*x+4 give False?
== tests structural identity, not math equality; use simplify(a-b)==0.