This lecture introduces ordinary differential equations, and some techniques for solving first order equations. This notebook uses computer algebra via Sympy to solve some ODE examples from the lecture notes.
In [1]:
import sympy
from sympy import symbols, Eq, Derivative, init_printing, Function, dsolve, exp, classify_ode, checkodesol
# This initialises pretty printing
init_printing()
from IPython.display import display
# Support for interactive plots
from ipywidgets import interact
# This command makes plots appear inside the browser window
%matplotlib inline
In [2]:
t, tau, v0 = symbols("t tau v0")
x = Function("x")
Next, we define the differential equation, and print it to the screen for checking:
In [3]:
eqn = Eq(Derivative(x(t), t), v0*exp(-t/(tau)))
display(eqn)
The dsolve
function solves the differential equation symbolically:
In [4]:
x = dsolve(eqn, x(t))
display(x)
where $C_{1}$ is a constant. As expected for a first-order equation, there is one constant.
SymPy is not yet very good at eliminating constants from initial conditions, so we will do this manually assuming that $x = 0$ and $t = 0$:
In [5]:
x = x.subs('C1', v0*tau)
display(x)
Specifying a value for $v_{0}$, we create an interactive plot of $x$ as a function of the parameter $\tau$:
In [6]:
x = x.subs(v0, 100)
def plot(τ=1.0):
x1 = x.subs(tau, τ)
# Plot position vs time
sympy.plot(x1.args[1], (t, 0.0, 10.0), xlabel="time", ylabel="position");
interact(plot, τ=(0.0, 10, 0.2));
In [7]:
classify_ode(eqn)
Out[7]:
Find the variation of speed with time of a parachutist subject to a drag force of $kv^{2}$.
The equations to solve is
$$ \frac{m}{k} \frac{dv}{dt} = \alpha^{2} - v^{2} $$where $m$ is mass, $k$ is a prescribed constant, $v$ is the velocity, $t$ is time and $\alpha^{2} = mg/k$ ($g$ is acceleration due to gravity).
We specify the symbols, unknown function $v$ and the differential equation
In [8]:
t, m, k, alpha = symbols("t m k alpha")
v = Function("v")
eqn = Eq((m/k)*Derivative(v(t), t), alpha*alpha - v(t)*v(t))
display(eqn)
First, let's classify the ODE:
In [9]:
classify_ode(eqn)
Out[9]:
We see that it is not linear, but it is separable. Using dsolve
again,
In [10]:
v = dsolve(eqn, v(t))
display(v)
SymPy can verify that an expression is a solution to an ODE:
In [11]:
print("Is v a solution to the ODE: {}".format(checkodesol(eqn, v)))
Try adding the code to plot velocity $v$ against time $t$.