# Systems of differential equations

**URL:** <https://discourse.julialang.org/t/systems-of-differential-equations/35618>\
**Category:** General Usage\
**Created:** [March 6, 2020, 12:46am UTC](https://discourse.julialang.org/t/systems-of-differential-equations/35618 "2020-03-06T00:46:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [March 6, 2020, 12:46am UTC](https://discourse.julialang.org/t/systems-of-differential-equations/35618/1 "2020-03-06T00:46:08Z")

</div>

Consider the set of equations and its solution. There are two variables: c and ce.

```julia
using DifferentialEquations

function diffusion!(du, u, p, t)
    α, β = p
    c, ce = u
    dc = -α*c
    dce = -β*ce
    du .= [dc, dce] # note the broadcast operator!
end

u0 = [1., 2.]
tspan = (0., 3.)
p = [1.0, 1.]
p1 = [1., 2., 8., 5.] # α, β, D1, D2

prob = ODEProblem(coupled!, u0, tspan, p1)
sol = solve(prob, Tsit5())

```

The code runs as expected. Consider these two equations a model of a biological neuron (this is a minimal example). I would like to create a collection of N neurons that will interact with each other.  
So I need an array of variables: c[1], …, c[N] and ce[1] to ce[N]. These equations will interact with each other. One way to handle this is with a “u” vector that has length 2\*N, but then it becomes hard to track the variables. I would prefer to create a vector “c\_vec” of size N, and “ce\_vec” of size N, create a function that computes the system in terms of these higher-level constructs, and then somehow interfaces with ODEProblem and solve, from DifferentialEquation.jl .

Alternatively, there may be tools to handle networks of interacting nodes where each node is described by a set of ODEs (perhaps a reduced version of Brian2, found at [Brian 2 documentation — Brian 2 2.5.1 documentation](https://brian2.readthedocs.io/en/stable/).

I could create a small example to demonstrate what I am trying to do. Thanks.

---

<div class="post-metadata">

**Author:** ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)\
**Post date:** [March 6, 2020, 1:18am UTC](https://discourse.julialang.org/t/systems-of-differential-equations/35618/2 "2020-03-06T01:18:07Z")

</div>

You may not have to track the variables if you do, for example, `cs = @view(u[1:2:end])` and `ces = @view(u[2:2:end])`. That’s one way to go about it - I was also interested in this problem, and had tried a graph-based description, but that proved to be too fiddly.

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [March 6, 2020, 1:52am UTC](https://discourse.julialang.org/t/systems-of-differential-equations/35618/3 "2020-03-06T01:52:29Z")

</div>

Interesting approach, thanks. I assume there is almost no performance penalty? I will try this for now. Perhaps I will get other replies. I was wondering if the ModelingToolkit might be of use. There might be other packages as well to provide a high-level interface that could feed into DifferentialEquations.jl.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 6, 2020, 2:38am UTC](https://discourse.julialang.org/t/systems-of-differential-equations/35618/4 "2020-03-06T02:38:43Z")

</div>

> [@erlebach](#):
>
> Alternatively, there may be tools to handle networks of interacting nodes where each node is described by a set of ODEs (perhaps a reduced version of Brian2, found at [https://brian2.readthedocs.io/en/stable/](https://brian2.readthedocs.io/en/stable/).

> [@erlebach](#):
>
> Interesting approach, thanks. I assume there is almost no performance penalty? I will try this for now. Perhaps I will get other replies. I was wondering if the ModelingToolkit might be of use. There might be other packages as well to provide a high-level interface that could feed into DifferentialEquations.jl.

@wsphillips and I were just talking about this today, building a high level tool for neural modeling which generates ModelingToolkit IR to then auto-GPU/distributed parallelize. I think this is a direction we will be going in the near future.

(In fact, if you know of the right grant to shoot for in this area, please share 🙂)

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [March 6, 2020, 2:42am UTC](https://discourse.julialang.org/t/systems-of-differential-equations/35618/5 "2020-03-06T02:42:02Z")

</div>

I have no idea, Chris. But I am open to collaboration, at least as a way to learn. That is what I am interested in. Do you know about Brian2? Ultra-cool system. But Julia could improve on it.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [March 6, 2020, 8:02am UTC](https://discourse.julialang.org/t/systems-of-differential-equations/35618/6 "2020-03-06T08:02:37Z")

</div>

You should have a look at Struct of Arrays for this. I am interested in Brian2 and a Julia version of it too!
