# Differential Equations¶

[1]:

import brainpy as bp


In BrainPy, the difinition of differential equations is supportted by a powerfull decorator @bp.integrate. Users should only explicitly write out the right hand of the differential equations, and BrainPy will automatically integerates your defined differential equations.

BrainPy supports the numerical integration of ordinary differential equations (ODEs) and stochastic differential equations (SDEs).

## ODEs¶

For an ordinary differential equation

$\frac{dx}{dt} = f(x, t)$

the coding in BrainPy has a general form of:

@bp.integrate
def func(x, t, other_arguments):
# ... do some computation
f = ...
return f

x_t_plus = func(x_t, t, other_arguments)


## SDEs¶

For the stochastic differential equation:

$\frac{dx}{dt} = f(x, t) + g(x, t) dW$

the coding in BrainPy can be conducted as:

@bp.integrate
def func(x, t, other_arguments):
# ... do some computation
f = ...
g = ...
return f, g

x_t_plus = func(x_t, t, other_arguments)


## Return intermediate values¶

BrainPy also supports the user return the intermediate computed results. Let’s take the differential equation of $$V$$ in Hodgkin–Huxley (HH) neuron model as an example. In HH model, the stochastic differential equation $$V$$ is expressed as:

\begin{align} C_{m}{\frac {d V}{dt}}&=-{\bar {g}}_{K}n^{4}(V-V_{K}) - {\bar {g}}_{Na}m^{3}h(V-V_{Na}) -{\bar {g}}_{l}(V-V_{l}) + I^{ext} + I^{noise} * dW, \end{align}

where

• the potassium channel current is $$I_{K} = {\bar {g}}_{K}n^{4}(V-V_{K})$$,

• the sodium channel current is $$I_{Na} = {\bar {g}}_{Na}m^{3}h(V-V_{Na})$$, and

• the leaky current is $$I_{L} = {\bar {g}}_{l}(V-V_{l})$$.

The user may not only has the interest of the final value $$V$$, but also take care of the intermediate value $$I_{Na}$$, $$I_K$$ and $$I_L$$. In BrainPy, this kind of requirement can be coded as:

@bp.integrate
def func(V, t, m, h, n, Iext):
INa = gNa * m ** 3 * h * (V - ENa)
IK = gK * n ** 4 * (V - EK)
IL = gLeak * (V - ELeak)
f = (- INa - IK - IL + Isyn) / C
g = noise / C
return (f, g), INa, IK, IL

V_t_plus, INa, IK, IL = func(V_t, t, m, h, n, Iext)


Generally, return intermediate values in ODE function can be coded as:

@bp.integrate
def func(x, t, other_arguments):
# ... do some computation
f = ...
return (f, ), some_values


Return intermediate values in SDE function can be coded as:

@bp.integrate
def func(x, t, other_arguments):
# ... do some computation
f = ...
g = ...
return (f, g), some_values