Wrap Parameter of ODE in struct with automatic differentiation

Hi,
I would like to learn the parmeters of an ode, I can do it when the parameters to learn are a vector of floats64, but it doesn’t work if I wrap them in a struct.
I am using the SIR model to test it and I get this error message

ERROR: MethodError: no method matching sir!(::Vector{Float64}, ::Vector{Float64}, ::Vector{Float64}, ::Float64)

Closest candidates are:
sir!(::Vector{Float64}, ::Vector{Float64}, ::Main.Models.SIR, ::Float64)

btw, I am very new to julia so this might be super easy

Did you happen to ::T in your function definition and force it to be the wrong type?

this is my function so far

@kwdef mutable struct SIR
β :: Float64
γ :: Float64
end
@functor SIR
function sir!(du, u, p, t)
S, I, R = u
β = p.β
γ = p.γ
λ = β * S * I / N
du[1] = -λ
du[2] = λ - γ * I
du[3] = γ * I
end
When I use

function sir!(du, u, p, t)
S, I, R = u
β, γ = p
# β, γ = params
λ = β * S * I / N_sir
du[1] = -λ
du[2] = λ - γ * I
du[3] = γ * I
end

it works.In both cases I can use solve, but only in the second case I can learn the parameters. Why ?

What I would also like to know, it what is the best way to handle thousands of parameters. In what I will eventually use, I would like more structure than just a vector of floats for holding the parameters, is that possible/advisable ?

Try ComponentArrays.jl

1 Like

Thanks, this is exactly what I was looking for!