How Do You Solve a Differential Equation With Python?

Rhett Allain
The Startup
Published in
8 min readAug 24, 2020

--

Photo: Rhett Allain

I guess I should start with a very basic explanation of differential equations. Of course you probably know something about equations, right? This is the equation for the motion of an object with a constant acceleration. Notice that my examples are oriented towards physics — because that’s what I like.

This shows a relationship between two variables — in this case, the position (x) and time (t). Note: the acceleration is just a constant in this case. Great.

Now suppose that I have a mass oscillating back and forth while connected to a spring (horizontally with no friction). In that case, I have the following relationship between position (x) and the acceleration (a).

Here, k is the spring constant and m is the value of the mass. But in this case, you can see that the acceleration is NOT constant. It changes as x changes. Also, the acceleration is the derivative of the velocity (with respect to time) and the velocity is the derivative of the position. So, maybe I should write the equation as:

Now we have a relationship between a variable (x) and a derivative (technically a second derivative). This is a differential equation. There are many methods to solve differential equations — such as separation of variables, variation of parameters, or my favorite: guessing a solution.

--

--

Rhett Allain
The Startup

Physics faculty, science blogger of all things geek. Technical Consultant for CBS MacGyver and MythBusters. WIRED blogger.