This article demonstrates how to solve ordinary differential equations with SymPy. Please refer to the previous article for introductory stuffs of SymPy:
- Indefinite integral
- Ordinary Differential Equations
- Linear ODE of 2nd order: Free Fall
- One-dimensional harmonic oscillator
- Airy equation
Indefinite integral
Let it start with reviewing an indefinite integral. The indefinite integral of a function
where is the constant of integral, is given as follows. As an example, let whose integral shall be obtained by running the following code in Python
import sympy as sp sp.var('a, x') sp.integrate(sp.exp(-a*x), x)
Let I be a function returning its indefinite integral of the input:
def I(f): return sp.integrate(f, x)
The example above is also found by applying this function I:
I(sp.exp(-a*x))
resulting in
For another example,
sp.var('n')
I(x**n)
one finds the indefinite integral of as
For
I(sp.cos(n*x))
one sees
and so on.
Ordinary Differential Equations
Linear ODE of 1st order
As a simpler example, consider the following equation
where is an unknown function of and is a constant.
This ODE is easily solved by a method of separation of variables:
implies
Taking logarithms on both sides, one may find
dsolve: general solution
SymPy has a function to solve ODE "dsolve" which is run as follows:
import sympy as sp sp.var('a, x') y = sp.Function('y')(x) eq = sp.Eq( sp.diff(y, x), -a*y ) sp.dsolve(eq)
gives
dsolve: solution with boundary condition
In order to impose (a) boundary condition(s), for example, putting
sp.dsolve(eq, ics={y.subs(x,0):1})
shows
Linear ODE of 2nd order: Free Fall
One-dimensional free fall is described by the following ODE (equation of motion)
where coordinates the position of the mass along the vertical direction (positive upwards) and is gravitational acceleration.
This equation is solved by Python running
import sympy as sp sp.var('g, t') x = sp.Function('x')(t) eq1 = sp.Eq( sp.diff(x,t,2), -g ) sp.dsolve(eq1)
resulting in
where is initial speed and is initial position.
Once imposing the initial conditions, e.g.
sp.var('v') sp.dsolve(eq1, ics={x.subs(t,0):0, sp.diff(x,t,1).subs(t,0):v})
gives
One-dimensional harmonic oscillator
The position of mass shall be labeled by . The EoM is given by
which is implemented as
import sympy as sp sp.var('m,k, t') x = sp.Function('x')(t) F = -k*x eq2 = sp.Eq( m*sp.diff(x,t,2), F ) sp.dsolve(eq2)
showing general solution
where are constants of integral.
Since both and are positive constants, introducing
and applying the Euler formula , one may find the general solution is also written as
Or, more explicitly, considering
import sympy as sp sp.init_printing() sp.var('t') x = sp.Function('x')(t) eq3 = sp.Eq( sp.diff(x,t,2), -x) sp.dsolve(eq3)
one gets
Airy equation
Consider the Airy equation
running the following code on Python
import sympy as sp sp.init_printing() sp.var('x') y = sp.Function('y')(x) eq4 = sp.Eq( sp.diff(y,x,2), x*y) sp.dsolve(eq4)
one may obtain
which is by definition given by linear combination of and .
Keywords: Differential equation, Python