# DynamicalSystems: dynamic rule evaluates to nonzero but trajectory stays at initial condition

**URL:** <https://discourse.julialang.org/t/dynamicalsystems-dynamic-rule-evaluates-to-nonzero-but-trajectory-stays-at-initial-condition/96481>\
**Category:** Modelling & Simulations\
**Tags:** question, dynamical-systems\
**Created:** [March 22, 2023, 10:17pm UTC](https://discourse.julialang.org/t/dynamicalsystems-dynamic-rule-evaluates-to-nonzero-but-trajectory-stays-at-initial-condition/96481 "2023-03-22T22:17:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjoshliu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjoshliu/32/47978_2.png) [@cjoshliu](https://discourse.julialang.org/u/cjoshliu)\
**Post date:** [March 22, 2023, 10:17pm UTC](https://discourse.julialang.org/t/dynamicalsystems-dynamic-rule-evaluates-to-nonzero-but-trajectory-stays-at-initial-condition/96481/1 "2023-03-22T22:17:02Z")

</div>

Hi, I’m new to Julia and trying to find fixed points of an s-species generalized Lotka-Volterra model using DynamicalSystems. My model is

d\_t\vec{u}=\vec{\lambda}+\vec{r}\cdot\vec{u}\cdot (1-\hat{\alpha}\vec{u})

where \hat{\alpha}\_{ii}=1 and \hat{\alpha}\_{ij,i\neq j} are distributed c\cdot\text{Gamma}(1/\sigma^2, \sigma^2).

I have implemented the dynamical rule as

```julia
function interaction_matrix(σ, s, c)
    α = c*rand(Gamma(1/σ^2,σ^2), (s,s))
    α[diagind(α)] .= 1.0
    return α
end

function GeneralizedLotkaVolterra!(du, u, p, t)
    λ, r, α = p
    du = λ+r.*u.*(1 .-α*u)
    return nothing
end

```

such that system initialization

```julia
SIGMA = 0.5
SEED = 1234
DISPERSAL = 1.0e-5 #scalar lambda
GROWTH_RATE = 1.0 #scalar r
N_SPECIES = 3 #s
IJ_STRENGTH = 0.25 #c

λ = DISPERSAL*ones(N_SPECIES)
r = GROWTH_RATE*ones(N_SPECIES)
α = interaction_matrix(SIGMA, N_SPECIES, IJ_STRENGTH)
p = [λ, r, α]

u0 = rand(N_SPECIES)
xg = range(0, 1; length=101)
grid = ntuple(x->xg, N_SPECIES)

glv = CoupledODEs(GeneralizedLotkaVolterra!, u0, p)

```

yields

```julia
3-dimensional CoupledODEs
 deterministic: true
 discrete time: false
 in-place: true
 dynamic rule: GeneralizedLotkaVolterra!
 ODE solver: Tsit5
 ODE kwargs: (abstol = 1.0e-6, reltol = 1.0e-6)
 parameters: Array{Float64}[[1.0e-5, 1.0e-5, 1.0e-5], [1.0, 1.0, 1.0], [1.0 0.17264272321853863 0.24272781912293961; 0.433123579186088 1.0 0.31834434917230875; 0.364142261750813 0.08754102121231638 1.0]]
 time: 0.0
 state: [0.8316040500722955, 0.9020798266115233, 0.06725925088948714]

```

which is what I expect. However, if I then run

```julia
X, t = trajectory(glv, 1000)
X

```

I get that the state has not evolved at all

```julia
3-dimensional StateSpaceSet{Float64} with 10001 points
 0.831604 0.90208 0.0672593
 0.831604 0.90208 0.0672593
 0.831604 0.90208 0.0672593
 ⋮                  
 0.831604 0.90208 0.0672593
 0.831604 0.90208 0.0672593
 0.831604 0.90208 0.0672593

```

which seems unreasonable, because directly evaluating `du = λ+r.*u0.*(1 .-α*u0)` gives the nonzero

```julia
3-element Vector{Float64}:
 -0.003039707589467017
 -0.2558908791421405
  0.03706645604209235

```

suggesting that _at least_ the second point should be different from the first, especially as the average timestep is about 0.1.

I’ve tried a few different ODE solvers and kwargs, and all give me the same results. Where does my code go wrong? Thanks!

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [March 22, 2023, 10:23pm UTC](https://discourse.julialang.org/t/dynamicalsystems-dynamic-rule-evaluates-to-nonzero-but-trajectory-stays-at-initial-condition/96481/2 "2023-03-22T22:23:45Z")

</div>

> [@cjoshliu](#):
>
> ```julia
> function GeneralizedLotkaVolterra!(du, u, p, t)
> λ, r, α = p
> du = λ+r.*u.*(1 .-α*u)
> return nothing
> end
> 
> ```

repalce `du = ` with `du .= ` so that the state is **updated in place**.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [March 22, 2023, 10:24pm UTC](https://discourse.julialang.org/t/dynamicalsystems-dynamic-rule-evaluates-to-nonzero-but-trajectory-stays-at-initial-condition/96481/3 "2023-03-22T22:24:13Z")

</div>

also, hi, welcome 🙂

---

<div class="post-metadata">

**Author:** ![cjoshliu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjoshliu/32/47978_2.png) [@cjoshliu](https://discourse.julialang.org/u/cjoshliu)\
**Post date:** [March 22, 2023, 10:25pm UTC](https://discourse.julialang.org/t/dynamicalsystems-dynamic-rule-evaluates-to-nonzero-but-trajectory-stays-at-initial-condition/96481/4 "2023-03-22T22:25:02Z")

</div>

Thank you! It worked!
