How to write an ordinary differential equation (in Python, using SymPy)
Task
Differential equations are equations that contain differentials like
Solution
This answer assumes you have imported SymPy as follows.
1
2
from sympy import * # load all math functions
init_printing( use_latex='mathjax' ) # use pretty math output
The following code tells SymPy that
1
2
3
4
var( 'x' ) # Let x be a variable.
y = Function('y')(x) # Literally, y is a function, named y, based on x.
dydx = Derivative( y, x ) # How to write dy/dx.
dydx # Let's see how SymPy displays dy/dx.
Let’s now write a very simple differential equation,
As with how to do implicit differentiation, SymPy expects us to move everything
to the left hand side of the equation. In this case, that makes the equation
1
2
ode = dydx - y
ode
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Contributed by Nathan Carter (ncarter@bentley.edu)