Integrating ordinary differential equations

The following examples demonstrate how to integrate various differential equations using kontrast.integrateODE(). Please refer to the documentation on integrating differential equations for details on all individual input and output properties.

Simple sets of equations

The following interactive code examples show first how to use the derivative callback function and secondly how to enter the system of equations using the Kontrast math syntax.


Differential equation:

\frac{\partial N}{\partial t} = -k N

Trigonometric functions

The following set of differential equations describes the sine and cosine functions:

\begin{aligned} \frac{\partial s}{\partial t} &= +c\\ \frac{\partial c}{\partial t} &= -s \end{aligned}

Harmonic oscillator

The following set of differential equations is equivalent to the equation of motion of the harmonic oscillator (m\ddot q=-kq-\gamma \dot q):

\begin{aligned} \frac{\partial q}{\partial t} &= \frac{v}{m}\\ \frac{\partial v}{\partial t} &= -\frac{k}{m}q - \frac{\gamma}{m} v \end{aligned}

Incorporating conditional logic

Decay with switching the decay constant

In the following example we include a jump in the decay constant

Approaches like this are best implemented using the derivative callback function (as it provides the most flexibility). However, in this particular example we can also use the math parser with the condition function.

Asynchronous execution