# MTK & missing parameters, MWE

**URL:** <https://discourse.julialang.org/t/mtk-missing-parameters-mwe/96945>\
**Category:** Modelling & Simulations\
**Created:** [April 1, 2023, 12:15pm UTC](https://discourse.julialang.org/t/mtk-missing-parameters-mwe/96945 "2023-04-01T12:15:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [April 1, 2023, 12:15pm UTC](https://discourse.julialang.org/t/mtk-missing-parameters-mwe/96945/1 "2023-04-01T12:15:45Z")

</div>

OK – I’m trying again – with a MWE. Consider this an _observation_ (I’m not saying this should be changed in MTK). This post is more meant as a request for advice on best practice.

Consider the DAE

\frac{dx}{dt} = -b\cdot (x-z) \\ 0 = z - c\cdot x

where b=a/10 and c=a/20, and I want to study uncertainty in a.

Suppose I do the following:

```julia
using ModelingToolkit

@parameters a=10 b=a/10 c=a/20

@variables t
Dt = Differential(t)

@variables x(t)=1 z(t)

eqs = [Dt(x) ~ - b*(x-z),
            0 ~ z - c*x]

@named sys = ODESystem(eqs)

sys_simp = structural_simplify(sys)

ModelingToolkit.defaults(sys_simp)

```

leading to  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/f/4f7098d9a352c154c7ab9f06fec98dd514daa9a1.png)

**Observe** that the default value `a=10` seems to have disappeared. This is confirmed – kind of – when I try to make a numeric version of the model:

```julia
> tspan = (0.0, 10)
> prob = ODEProblem(sys_simp,[],tspan)

Output exceeds the size limit. Open the full output data in a text editorMethodError: no method matching AbstractFloat(::Type{SymbolicUtils.BasicSymbolic{Real}})
Closest candidates are:
  (::Type{T})(!Matched::AbstractChar) where T<:Union{AbstractChar,

```

**Suspicion** : My guess is that MTK has stripped off all parametes in `sys`/`sys_simp` that are not used _explicitly_ in the equations. Thus, information about the value of `a` has disappeared.

OK – because I only have uncertainty in the single parameter `a`, and not independently in `b` and `c`, I cannot really drop `a` and replace the parameters by `b=1` and `c=1/2`.

So what is the “correct” way to get around this? I have tried to define `a` as a _constant_ instead, in the hope that the constant will be carried over to the system `sys`/`sys_simp`. But no dice – seems like also _constants_ must appear explicitly to be included in the system (`sys`/`sys_simp`).

**Possible solution** : keep `a` as a parameter, and instead specify `b` and `c` as _variables_:

```julia
using ModelingToolkit
using DifferentialEquations

@parameters a=10.0
@variables t 
Dt = Differential(t)
@variables x(t)=1.0 z(t) b(t) c(t)

eqs = [Dt(x) ~ - b*(x-z),
        0 ~ z - c*x,
        b ~ a/10,
        c ~ a/20]

@named sys = ODESystem(eqs)

sys_simp = structural_simplify(sys)
tspan = (0.0,10)

prob = ODEProblem(sys_simp,[],tspan)

sol = solve(prob)

sol(5, idxs=x)
sol(5, idxs=z)
sol(5, idxs=b)
sol(5, idxs=c)

```

With this strategy, the model runs and produces sensible results.

**QUESTION** : is this strategy of making _parameters_ into _variables_ the recommended strategy?

---

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [April 7, 2023, 1:45am UTC](https://discourse.julialang.org/t/mtk-missing-parameters-mwe/96945/2 "2023-04-07T01:45:00Z")

</div>

When using `ODESystem` with 1 input (`eqs`), I think MTK might struggle to determine the full system information. I recommend using the 4 input version:

```julia
using ModelingToolkit

pars = @parameters a=10 b=a/10 c=a/20

@variables t
Dt = Differential(t)

vars = @variables x(t)=1 z(t)

eqs = [Dt(x) ~ - b*(x-z),
            0 ~ z - c*x]

@named sys = ODESystem(eqs, t, vars, pars)

sys_simp = structural_simplify(sys)

ModelingToolkit.defaults(sys_simp)

```

Which keeps `a` in the defaults

```julia
Dict{Any, Any} with 4 entries:
  a => 10
  x(t) => 1
  b => (1//10)*a
  c => (1//20)*a

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [April 10, 2023, 1:07pm UTC](https://discourse.julialang.org/t/mtk-missing-parameters-mwe/96945/3 "2023-04-10T13:07:45Z")

</div>

> [@Brad\_Carman](#):
>
> When using `ODESystem` with 1 input (`eqs`), I think MTK might struggle to determine the full system information. I recommend using the 4 input version:

Thanks for clarification! Hm… where do I find information about the “1 input” vs. “4 input” version? I looked up the info on constructor `ODESystem` in the documentation, and found the following (snippet):

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/e/3e0eddcd2aec8c68fbd4996ea8307eb61f542af4.png)

Very useful to find info on _fields_. However, I don’t find info on constructor arguments… except for the following _at the end of the **fields** list_:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/a/9a5b7c72f7d52ba3cd76238dee9608a753717030.png)

Here, a _5 input_ version is listed as _an example_.

- But is there a _6 input_ version, too?
- Should I use `methods(ODESystem)` to find all possible `args` (and their order) and `kwargs`?
- How do I know what is default? Do I _need_ to know this?
- Or is this detailed in the documentation?

---

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [April 12, 2023, 11:22am UTC](https://discourse.julialang.org/t/mtk-missing-parameters-mwe/96945/4 "2023-04-12T11:22:39Z")

</div>

If you run `methods(ODESystem)` you can see there are actually 12 ways to construct an `ODESystem`. What you refer to a 5 input version is actually 4 inputs with 1 keyword argument `tspan` which is the first method listed…

```julia
julia> methods(ODESystem)
# 12 methods for type constructor:
  [1] ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps; controls, observed, systems, tspan, name, default_u0, default_p, defaults, connector_type, preface, continuous_events, discrete_events, checks, metadata, gui_metadata)

```
