5.4.20 · D2Scientific Computing (Python)

Visual walkthrough — SymPy — symbolic algebra, calculus, ODE solving

2,088 words9 min readBack to topic

Prerequisite trails you can wander back to: Limits and the definition of the derivative, Ordinary Differential Equations — characteristic equation method, Taylor and Maclaurin Series, and the home base the parent SymPy note.


Step 1 — What a derivative is, drawn

WHAT. Before any equation, we need one idea: the derivative. Pick a curve — a height that changes as you move right. The derivative at a point is the steepness of the curve there — how fast the height climbs or falls.

WHY this tool and not another. We are about to meet the symbol and . If we don't nail down what they mean geometrically, the whole page is symbol-pushing. So: earn the symbol first. We use the slope-of-the-tangent-line picture because it is the one thing measures, nothing more.

PICTURE. Look at the figure. The white curve is some function . The amber line just kisses the curve at one point — that's the tangent line. Its tilt is at that point.

Figure — SymPy — symbolic algebra, calculus, ODE solving

Read it aloud: " is the rate of change of the slope." Nothing more mysterious than that.


Step 2 — Reading the equation as a sentence

WHAT. We rewrite the equation so its meaning is loud:

WHY. An equation with symbols is a sentence in disguise. Rearranging to isolate turns it into a physical rule we can picture. We move to the right (subtract it from both sides) purely so the "curving" sits alone on the left.

PICTURE. Each symbol is doing a job:

The sentence: "however high you are, bend back toward zero by exactly that much." High up → bend down hard. Below zero → bend up hard. At zero → don't bend. Look at the figure: the amber arrows are the bending force , always pointing back at the centre line, always as long as the height itself.

Figure — SymPy — symbolic algebra, calculus, ODE solving

Step 3 — The educated guess

WHAT. We try a candidate solution:

Here is a fixed number, and is an unknown number we get to choose.

WHY this function and not another. We need a function whose derivative looks like itself, because the equation compares with — apples must meet apples. The exponential is the unique function with the magic property

— differentiating just multiplies by . That means , , are all copies of scaled by powers of , so the equation will collapse into simple algebra in . No other elementary function keeps its shape under differentiation, so this is the right tool.

PICTURE. The figure shows and its derivative sitting on top of each other, differing only by a vertical stretch factor — same shape, rescaled.

Figure — SymPy — symbolic algebra, calculus, ODE solving

Step 4 — Plug in and cancel: the characteristic equation

WHAT. Differentiate the guess twice and substitute into .

Substituting:

WHY. This is the whole point of the guess: the equation now contains only as a common factor. We factor it out:

Now here's the key move. is never zero — an exponential is always positive, it can't vanish. So the only way the product is zero is if the other factor is zero:

This tiny equation is called the characteristic equation. We just traded a calculus problem for an algebra problem — that's the trick.

PICTURE. The figure shows the exponential curve staying strictly above zero everywhere (so it can never be the "zero" factor), forcing .

Figure — SymPy — symbolic algebra, calculus, ODE solving

Step 5 — Solving : meeting

WHAT. Solve for :

WHY the symbol . We need a number whose square is . No ordinary number works — squaring any real number gives something . So mathematicians defined a new number, called , by the single rule

That's all means: "the thing that squares to ." We introduce it only because the equation demanded it — the algebra hit a wall, and is the doorway through the wall.

PICTURE. The figure is the number line bent into a plane: real numbers run left–right, and points up, off the line. The two solutions and sit above and below the centre — mirror images.

Figure — SymPy — symbolic algebra, calculus, ODE solving

Step 6 — From to real waves (Euler's bridge)

WHAT. Our two solutions are and . But we wanted a real height — a curve you can draw. Euler's identity converts the imaginary exponential into ordinary waves:

WHY. We are stuck with , which looks unplottable. Euler's identity is the translator from "exponential of imaginary" to "sine and cosine." It's not a trick pulled from air — it falls straight out of the Taylor and Maclaurin Series of , and (the parent note's series tool): line up the three power series and the pattern matches exactly.

PICTURE. The figure shows the point travelling around a unit circle as grows. Its horizontal shadow traces ; its vertical shadow traces . Circular motion is two oscillations at right angles — exactly the wobble we forecast in Step 2.

Figure — SymPy — symbolic algebra, calculus, ODE solving

Step 7 — Assembling the general solution

WHAT. The two imaginary solutions combine into two real building blocks, and . Because the equation is linear (no , no ), any weighted mix of solutions is again a solution. So:

WHY two constants. A second-derivative equation () needs two facts to pin down a unique curve — where you start () and how fast you start (). Each fact eats one constant. That is exactly why dsolve hands back and : it doesn't yet know your starting conditions.

PICTURE. The figure overlays several members of the family — different give waves of different height and phase, but every one wobbles, confirming Step 2's spring forecast.

Figure — SymPy — symbolic algebra, calculus, ODE solving
x = symbols('x')
y = Function('y')
dsolve(Eq(y(x).diff(x, 2) + y(x), 0), y(x))
# -> Eq(y(x), C1*sin(x) + C2*cos(x))

Step 8 — The degenerate & edge cases (never leave a gap)

WHAT. What if the equation were slightly different? We check the neighbours so no scenario surprises you.

WHY. The judge of understanding is coverage: you must know why this equation gives waves and a cousin gives something else.

Equation Characteristic eqn Roots Solution shape Picture
(imaginary) pure waves Step 7
(real) run away / decay below
(repeated) straight line below
(single) growth, one constant parent §4

The sign inside the characteristic equation decides everything: gives imaginary roots and oscillation; gives real roots and exponential growth/decay; a repeated root at gives a straight line. The figure draws all three side by side.

Figure — SymPy — symbolic algebra, calculus, ODE solving

The one-picture summary

This single figure compresses all eight steps: the equation's meaning (spring), the exponential guess, the algebra collapse, the imaginary roots on the vertical axis, Euler's circle, and the two real waves that come out.

Figure — SymPy — symbolic algebra, calculus, ODE solving
Recall Feynman retelling — the whole walkthrough in plain words

We started with a curve and learned that is its steepness and is how the steepness bends. The equation literally says "bend back toward the middle as hard as you're far from it" — that's a spring, so we guessed the answer wobbles. To find it we tried , the one function that stays itself when you take its slope, so the calculus turned into simple algebra: . That needs a number squaring to , so walked in, giving . Euler's identity then said is just a point spinning around a circle, whose two shadows are and . Mix those two waves with any amounts and you have every possible solution — exactly what dsolve prints. Change one sign and the roots turn real and the wobble becomes runaway growth. That's the whole story: a spring, a clever guess, a doorway called , and two waves.

Recall Why does

dsolve return two constants for but only one for ? The order of the highest derivative equals the number of independent starting facts you must supply, hence the number of arbitrary constants: second order → ; first order → alone.

Recall Why is factoring out

legal and useful? is never zero, so dividing (or factoring) by it can't lose or invent solutions — and it strips the calculus away, leaving the pure algebra .

Connections

  • Limits and the definition of the derivative — Step 1's slope idea, made rigorous.
  • Ordinary Differential Equations — characteristic equation method — the hand method dsolve automates.
  • Taylor and Maclaurin Series — where Euler's (Step 6) comes from.
  • Lambdify — bridging SymPy to NumPy for plotting — turn into numbers to plot.
  • NumPy — numerical arrays — the numeric counterpart that would only ever see the decimals.