Differential equations can be used to describe the time-dependent behaviour of a variable.
$$\frac{\text{d}\vec{x}}{\text{d}t} = f(\vec{x}, t)$$
The variable stands for a concentration or a number of individuals in a population.
In general, a first order ODE has two parts, the increasing (birth, production,...) and the decreasing (death, degradation, consumption,...) part:
$$\frac{\text{d}\vec{x}}{\text{d}t} = \sum_{}\text{Rates}_{\text{production}} - \sum_{}\text{Rates}_{\text{loss}}$$You probably already know ways to solve a differential equation algebraically by 'separation of variables' (Trennung der Variablen) in the homogeneous case or 'variation of parameters' (Variation der Konstanten) in the inhomogeneous case. Here, we want to discuss the use of numerical methods to solve your ODE system.
In principle, every numerical procedure to solve an ODE is based on the so-called "Euler" method. It's very easy to understand, you just have to read the $\frac{\text{d}\vec{x}}{\text{d}t}$ as a $\frac{\Delta \vec{x}}{\Delta t}$. Then you can multiply both sides of the equation with $\Delta t$ and you have an equation describing the change of your variables during a certain time intervall $\Delta t$:
$$ \Delta \vec{x} = f(\vec{x}, t)\cdot \Delta t$$Of course, the smaller yoy choose the time intervall $\Delta t$, the more accurate your result will be in comparison to the analytical solution.
So it's clear, we chose a tiny one, right? Well, not exactly, the smaller your time intervall the longer the simulation will take. Therefore, we need a compromise and here the provided software will help us by constantly testing and observing the numerical solution and adapt the "step size" $\Delta t$ automatically.
we write a small function f, that receives a list of the current values of our variables x, the current time t and parameters p. The function has to evaluate the equations of our system or $\frac{\text{d}\vec{x}}{\text{d}t}$, respectively. Afterwards, it returns the values of the equations as another list.
Important
Since this function f is used by the solver, we are not allowed to change the input (arguments) or output (return value) of this function.
In [1]:
import numpy as np
# define ODE (y,t,p)
Before we start the simulation of our model, we have to define our system.
We start with our static information:
In [2]:
# initial values of variables
# define p = [a, b, c, d]
# time grid
Last but not least, we need to import and call our solver. The result will be a matrix with our time courses as columns and the values at the specified time points. Since we have a values for every time point and every species, we can directly plot the results using matplotlib.
In [5]:
from scipy.integrate import odeint
# solve ODE using odeint (f, y0, t, (p,))
In [6]:
%matplotlib inline
import matplotlib.pyplot as plt
Out[6]:
In [5]:
%matplotlib inline
import matplotlib.pyplot as plt
Out[5]:
$\frac{\mathrm{d}C}{\mathrm{d}t}=v_i-v_dX\frac{C}{K_d+C}-k_dC$
$\frac{\mathrm{d}M}{\mathrm{d}t}=V_1\frac{1-M}{K_1+(1-M)}-V_2\frac{M}{K_2+M}$
$\frac{\mathrm{d}X}{\mathrm{d}t}=V_3\frac{1-X}{K_3+(1-X)}-V_4\frac{X}{K_4+X}$
with $V_1=\frac{C}{K_c+C}V_{M1}$ and $V_3=MV_{M3}$
and $M+M^*=1$ and $X+X^*=1$
In [7]:
In [8]:
In [9]:
# solve ODE using odeint
y = odeint(f, y0, t, (p,))