# Best way to define a set of differential equations?

**URL:** <https://discourse.julialang.org/t/best-way-to-define-a-set-of-differential-equations/67650>\
**Category:** Modelling & Simulations\
**Tags:** differentialequation\
**Created:** [September 3, 2021, 1:58pm UTC](https://discourse.julialang.org/t/best-way-to-define-a-set-of-differential-equations/67650 "2021-09-03T13:58:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pafloxy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pafloxy/32/28694_2.png) [@pafloxy](https://discourse.julialang.org/u/pafloxy)\
**Post date:** [September 3, 2021, 1:58pm UTC](https://discourse.julialang.org/t/best-way-to-define-a-set-of-differential-equations/67650/1 "2021-09-03T13:58:29Z")

</div>

Hi!  
I am using DifferentialEquations.jl for simulating trajectories over high-dimensional vector fields.  
For my case, the time-evolution function for each coordinate is generated by a pre-defined random function (\*\* same function for each coordinate) which takes in as input the current state of the system.  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/4/d44d04d677fd700fbcefb9370054c53d57d68c44.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/e/be027006c14e99a7c735e6742148724bc3f7136e.png)  
Here **nonlinear\_2(x,index)** generates the vector field for each index

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [September 3, 2021, 2:16pm UTC](https://discourse.julialang.org/t/best-way-to-define-a-set-of-differential-equations/67650/2 "2021-09-03T14:16:45Z")

</div>

And what is the question or the problem?

It would also help if you enter the code not as a screenshot but rather as a copy-pastable text. Use triple backticks as shown in item 2 at [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757).

---

<div class="post-metadata">

**Author:** ![pafloxy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pafloxy/32/28694_2.png) [@pafloxy](https://discourse.julialang.org/u/pafloxy)\
**Post date:** [September 3, 2021, 2:29pm UTC](https://discourse.julialang.org/t/best-way-to-define-a-set-of-differential-equations/67650/3 "2021-09-03T14:29:16Z")

</div>

Hi!  
You can see that I’ve used a `for` loop in the `deff` function over the range of dimensions `dim`, I wanted to ask if there was any better way to do this in regard to computational efficiency?

I’ll reupload the code once I make some corrections!

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 3, 2021, 2:56pm UTC](https://discourse.julialang.org/t/best-way-to-define-a-set-of-differential-equations/67650/4 "2021-09-03T14:56:11Z")

</div>

It is a bit strange that `G1` and `G2` are randomly generated in each function call. Are you sure that’s what you want?

In my experience, it is sometimes even better to use for loops. That often avoids to accidentally allocate new vectors.

Assuming that G1 and G2 are constants, you could do

```julia
dim = 10
p = (G1 = randn(dim,dim), G2 = randn(dim,dim,dim), dim=dim)

function deff(dx, x, p, t)
   for j = 1:p.dim
       dx[j] = p.G1[j,:]' * x + x' * p.G2[j,:,:] * x + x[j]^3
   end
end

x0 = randn(dim)
tspan = (0., 10.)
ODEProblem(deff, x0, tspan, p)

```

If you want to be fancy with passing parameters back and forth, you can use UnPack.jl

---

<div class="post-metadata">

**Author:** ![pafloxy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pafloxy/32/28694_2.png) [@pafloxy](https://discourse.julialang.org/u/pafloxy)\
**Post date:** [September 4, 2021, 5:49am UTC](https://discourse.julialang.org/t/best-way-to-define-a-set-of-differential-equations/67650/5 "2021-09-04T05:49:17Z")

</div>

Yep sorry! I noticed that error ( \*\* `G1 & G2` being called everytime ) after uploading the question.

Also thanks for your opinion in support of using ` for` loops.  
However, I would like to ask if can similar “summation over some indices” can be done for higher rank tensors ?  
`dx[j] = p.G1[j,:]' * x + x' * p.G2[j,:,:] * x + x[j]^3`  
Here `x' * p.G2[j,:,:] * x` works as it is a matrix-vector multiplication, but is there an analouge for higher rank tensors ? Also could you suggest any methods to reduce computational time as I’ll be increasing `dim`  
upto 60 for my project.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 4, 2021, 7:26am UTC](https://discourse.julialang.org/t/best-way-to-define-a-set-of-differential-equations/67650/6 "2021-09-04T07:26:34Z")

</div>

You could use [https://github.com/Jutho/TensorOperations.jl](https://github.com/Jutho/TensorOperations.jl) for avoiding the summation.

For performance, the most common advice for ODE problems is that you should avoid allocations and avoid access to global variables (as you probably know already, since you did it.)  
Also, check out [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/)  
and [Frequently Asked Questions · DifferentialEquations.jl](https://diffeq.sciml.ai/stable/basics/faq/#faq_performance)  
Use BenchmarkTools.jl for performance measurements.

Further vectorisation could lead to a small increase of speed, but often normal for loops are just as fast.  
You can add `@inbounds for i = 1:p.dim` to avoid bound checking. There is also `@simd` to encourage better CPU instructions.

I would guess, that for your problem a simple implementation like the one above is sufficient. You could rather experiment with finding the best ODE solver, which could have quite some impact.

---

<div class="post-metadata">

**Author:** ![pafloxy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pafloxy/32/28694_2.png) [@pafloxy](https://discourse.julialang.org/u/pafloxy)\
**Post date:** [September 4, 2021, 9:11am UTC](https://discourse.julialang.org/t/best-way-to-define-a-set-of-differential-equations/67650/7 "2021-09-04T09:11:06Z")

</div>

okay! thanks very much
