JuliaDynamics / DynamicalSystems.jl

Award winning software library for nonlinear dynamics and nonlinear timeseries analysis

Home Page:https://juliadynamics.github.io/DynamicalSystemsDocs.jl/dynamicalsystems/dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

(Question) How to construct a dynamic system like this in DynamicSystems.jl?

vtchen0523 opened this issue · comments

Dear authors,
I want to define a dynamic system in DynamicSystems.jl, as follows:
image
where Fx is a function of u1, u2, and u3 (for the convenience of description, it is simplified here )

using DifferentialEquations,
      DynamicalSystems

function foo1(x, i)
    i*x
end # function

function foo2(x, y, z)
    x̄ = (x + y + z)/3.0
    println(typeof(x))
    k = zeros(3, 3)
    for i in 1:3
        for j in 1:3
            k[i, j] = foo1(x̄, j)
        end
    end
    return sum(k)
end

foo2(1.0, 2.0, 4.0)

@inline @inbounds function lorenz!(du,u,p,t)
    Fx = foo2(u[1], u[2], u[3])
    σ, ρ, β = p
    du1 = σ*(u[2] - u[1]) + Fx
    du2 = u[1]*- u[3]) - u[2]
    du3 = u[1]*u[2] - β*u[3]
end

function test_DS()
    u0 = 0.1*ones(3)
    p = 10.0, 28.0, 8/3
    ds = ContinuousDynamicalSystem(lorenz!, u0, p)
end # function

test_DS()

However, this code will report the following errors at runtime

image

I would be grateful if the author could give me a hint

you have to change the line k = zeros(3, 3) to k = zeros(typeof(X), 3, 3). The error you are getting is from the automatic differentiation, that needs dual numbers.

Good that you've asked this, I should put a note about this in the documentation.

Thank you very much for your guidance,it works!