# Wrap Parameter of ODE in struct with automatic differentiation

**URL:** <https://discourse.julialang.org/t/wrap-parameter-of-ode-in-struct-with-automatic-differentiation/114647>\
**Category:** General Usage\
**Tags:** question, diffeq, ode, autodiff\
**Created:** [May 23, 2024, 10:36pm UTC](https://discourse.julialang.org/t/wrap-parameter-of-ode-in-struct-with-automatic-differentiation/114647 "2024-05-23T22:36:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgsimard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgsimard/32/209469_2.png) [@jgsimard](https://discourse.julialang.org/u/jgsimard)\
**Post date:** [May 23, 2024, 10:36pm UTC](https://discourse.julialang.org/t/wrap-parameter-of-ode-in-struct-with-automatic-differentiation/114647/1 "2024-05-23T22:36:55Z")

</div>

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

---

<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:** [May 24, 2024, 1:48am UTC](https://discourse.julialang.org/t/wrap-parameter-of-ode-in-struct-with-automatic-differentiation/114647/2 "2024-05-24T01:48:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jgsimard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgsimard/32/209469_2.png) [@jgsimard](https://discourse.julialang.org/u/jgsimard)\
**Post date:** [May 24, 2024, 2:22am UTC](https://discourse.julialang.org/t/wrap-parameter-of-ode-in-struct-with-automatic-differentiation/114647/3 "2024-05-24T02:22:13Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![jgsimard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgsimard/32/209469_2.png) [@jgsimard](https://discourse.julialang.org/u/jgsimard)\
**Post date:** [May 24, 2024, 2:31am UTC](https://discourse.julialang.org/t/wrap-parameter-of-ode-in-struct-with-automatic-differentiation/114647/4 "2024-05-24T02:31:41Z")

</div>

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 ?

---

<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:** [May 24, 2024, 5:15am UTC](https://discourse.julialang.org/t/wrap-parameter-of-ode-in-struct-with-automatic-differentiation/114647/5 "2024-05-24T05:15:55Z")

</div>

Try ComponentArrays.jl

---

<div class="post-metadata">

**Author:** ![jgsimard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgsimard/32/209469_2.png) [@jgsimard](https://discourse.julialang.org/u/jgsimard)\
**Post date:** [May 25, 2024, 7:50pm UTC](https://discourse.julialang.org/t/wrap-parameter-of-ode-in-struct-with-automatic-differentiation/114647/6 "2024-05-25T19:50:38Z")

</div>

Thanks, this is exactly what I was looking for!
