# Is there any way to extract the name of the parameter from the ODE function

**URL:** <https://discourse.julialang.org/t/is-there-any-way-to-extract-the-name-of-the-parameter-from-the-ode-function/90923>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 27, 2022, 11:07pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-extract-the-name-of-the-parameter-from-the-ode-function/90923 "2022-11-27T23:07:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![YiboXia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yiboxia/32/50734_2.png) [@YiboXia](https://discourse.julialang.org/u/YiboXia)\
**Post date:** [November 27, 2022, 11:07pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-extract-the-name-of-the-parameter-from-the-ode-function/90923/1 "2022-11-27T23:07:09Z")

</div>

Hi!  
I want to extract the name of the parameter from the ODE function to make the tuple out of given parameters.  
For example, we have an ODE function:

```julia
function f(du,u,p,t)
  a, b = p
  x = u
  du[1] = a * x+b * x^2
end

```

and I want to create a tuple for the parameters automatically according to the function, like:

```julia
para_tuple = (a = 1,b = 2)

```

How can I do that? (may use `@ntuple` function in `DrWatson.jl`.

Many thanks!

---

<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:** [November 28, 2022, 12:47pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-extract-the-name-of-the-parameter-from-the-ode-function/90923/2 "2022-11-28T12:47:58Z")

</div>

I don’t quite get the question. Try again with a bit more detail? You can use a `NamedTuple` for `p` though.

---

<div class="post-metadata">

**Author:** ![YiboXia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yiboxia/32/50734_2.png) [@YiboXia](https://discourse.julialang.org/u/YiboXia)\
**Post date:** [November 28, 2022, 1:39pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-extract-the-name-of-the-parameter-from-the-ode-function/90923/3 "2022-11-28T13:39:33Z")

</div>

Hi Chirs!  
Thanks so much for your time!  
Usually, we define an ODEproblem with a vector `para = [1.0,2.0]` like:

```julia
function f!(du,u,p,t)
  a, b = p
  x = u
  du[1] = a * x+b * x^2
end
u0 = [0.0]
para = [1.0,2.0]
prob = ODEProblem(f,u0,(0.0,100.0),para)

```

My question is, how can I create a `namedtuple` like `para_tuple = (a = 1.0,b = 2.0)` after the definition of `prob`?

I know `NamedTuple`, but how can I use it for `p`? Sorry I tried several times inside function`f!` or like ` NamedTuple(prob.p)` but it doesn’t work.

Many thanks man.

---

<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:** [November 28, 2022, 1:59pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-extract-the-name-of-the-parameter-from-the-ode-function/90923/4 "2022-11-28T13:59:34Z")

</div>

> [@YiboXia](#):
>
> I know `NamedTuple`, but how can I use it for `p`?

Just pass it in to `ODEProblem`

---

<div class="post-metadata">

**Author:** ![YiboXia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yiboxia/32/50734_2.png) [@YiboXia](https://discourse.julialang.org/u/YiboXia)\
**Post date:** [November 28, 2022, 2:22pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-extract-the-name-of-the-parameter-from-the-ode-function/90923/5 "2022-11-28T14:22:54Z")

</div>

Sorry… still unclear. Could you be more specific?  
`NamedTuple` should be key-value pairs if I am not wrong, but there is no value inside the function `f!`

also, how can I output it in `ODEProblem`? I think there is no field in `prob.f` or `prob.p`.

---

<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:** [November 28, 2022, 2:32pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-extract-the-name-of-the-parameter-from-the-ode-function/90923/6 "2022-11-28T14:32:09Z")

</div>

`prob = ODEProblem(f,u0,(0.0,100.0),para_tuple)` and then you can use it as `p`.

---

<div class="post-metadata">

**Author:** ![YiboXia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yiboxia/32/50734_2.png) [@YiboXia](https://discourse.julialang.org/u/YiboXia)\
**Post date:** [November 28, 2022, 2:45pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-extract-the-name-of-the-parameter-from-the-ode-function/90923/7 "2022-11-28T14:45:22Z")

</div>

Sorry for the unclear message.  
of course I know I can use a tuple (or dict) like `para_tuple = (a = 1,b = 2)` when defining an `ODEProblem`.

My question is, can I create a parameter tuple by an existing ODEProblem? I want to do this because the `BifurcationKit.jl` only accept parameters in tuples but I want to do the bifurcation analysis automatically after plotting the trajectory.

Until now that package can’t support the direct use of `ODEProblem`, so I want to ask about a possible transition between vector to tuple for parameters.

---

<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:** [November 28, 2022, 3:47pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-extract-the-name-of-the-parameter-from-the-ode-function/90923/8 "2022-11-28T15:47:07Z")

</div>

> [@YiboXia](#):
>
> can I create a parameter tuple by an existing ODEProblem?

I just don’t understand what you mean by this sentence. Can you show an example of the code you would like to work? Maybe that would explain the idea.

---

<div class="post-metadata">

**Author:** ![YiboXia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yiboxia/32/50734_2.png) [@YiboXia](https://discourse.julialang.org/u/YiboXia)\
**Post date:** [November 28, 2022, 4:21pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-extract-the-name-of-the-parameter-from-the-ode-function/90923/9 "2022-11-28T16:21:09Z")

</div>

Okey…  
What I have right now:

- a defined ODE problem `prob`:

```julia
function f!(du,u,p,t)
  k1, k2, k3 = p
  x = u
  du[1] = k1 * x+ k2 * x^2 +k3
end
u0 = [0.0]
tspan = (0.0,100.0)
para = [1.0, 2.0, 1.0] #parameters in vector form
prob = ODEProblem(f!,u0,tspan,para)

```

What I want then:

- generate a tuple `para_tuple` from the `prob` which saves the keys as well as values of the parameter `p `:

`para_tuple = (k1 = 1.0, k2 = 2.0, k3 = 1.0)`

So that I can use it to define a `BifurcationProblem` (which requires the parameter tuple:

`prob_bifur = BifurcationProblem(f!, u0, para_tuple, (@lens _.k1) ) `

---

<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:** [November 28, 2022, 7:21pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-extract-the-name-of-the-parameter-from-the-ode-function/90923/10 "2022-11-28T19:21:33Z")

</div>

> [@YiboXia](#):
>
> generate a tuple `para_tuple` from the `prob` which saves the keys as well as values of the parameter `p `:

If you just always use `para_tuple`, then your `f` code would still work and `prob.p` would be what you want. That seems like the most straightforward way?

---

<div class="post-metadata">

**Author:** ![YiboXia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yiboxia/32/50734_2.png) [@YiboXia](https://discourse.julialang.org/u/YiboXia)\
**Post date:** [November 29, 2022, 2:28am UTC](https://discourse.julialang.org/t/is-there-any-way-to-extract-the-name-of-the-parameter-from-the-ode-function/90923/11 "2022-11-29T02:28:21Z")

</div>

Yes, you are right. But I am thinking of building a general function or module that can create parameter tuples for different models. I think I would try `DrWatson.jl` to find the possibility.

Still thank you Chris, for your time and your extraordinary work!
