# MTK with MonteCarloMeasurements Error

**URL:** https://discourse.julialang.org/t/mtk-with-montecarlomeasurements-error/135988
**Category:** Modelling & Simulations
**Tags:** question, package, error, mtk
**Created:** [March 3, 2026, 9:11am UTC](https://discourse.julialang.org/t/mtk-with-montecarlomeasurements-error/135988 "2026-03-03T09:11:50Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![optimal\_georg](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@optimal\_georg](https://discourse.julialang.org/u/optimal_georg)
#### Post date: [March 3, 2026, 9:11am UTC](https://discourse.julialang.org/t/mtk-with-montecarlomeasurements-error/135988/1 "2026-03-03T09:11:50Z")

</div>

Hi out there!

I am trying to use MonteCarloMeasurements.jl together with MTK to solve a differential equation with uncertain parameters.

```julia-auto
using ModelingToolkit, DifferentialEquations
using MonteCarloMeasurements
using CairoMakie

@independent_variables t
D = Differential(t)
@parameters my_param = 1.5
@variables xp(t) Tp(t)  

function my_problematic_function(xp)
    xp^1.8 
end

eqs = [
    D(xp) ~ my_param * my_problematic_function(xp),
    D(Tp) ~ (Tp + my_param * my_problematic_function(xp))
]

@named sys_model = System(eqs, t)
sys = mtkcompile(sys_model)

tspan = (0.0, 30.0)
u0_nom = [
    sys.xp => 0.75,
    sys.Tp => 293.15]
prob = ODEProblem(sys, u0_nom, tspan; jac=true)
# Redefine prob with uncertain parameters
prob_uncertain = remake(prob; u0 = u0_nom, p=[my_param=> 1.6 ± 0.3])
sol_uncertain = solve(prob_uncertain, Tsit5())

```

I think I ran into a bug. Whenever I take the power of a variable, I get a stackoverflow-error.

```julia-auto
Warning: detected a stack overflow; program state may be corrupted, so further execution might be unreliable.
ERROR: StackOverflowError:

```

This might need a fix. I would also be interested in workarounds!  
Thanks for your efforts in advance.

---

<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: [March 3, 2026, 11:34am UTC](https://discourse.julialang.org/t/mtk-with-montecarlomeasurements-error/135988/2 "2026-03-03T11:34:05Z")

</div>

This probably needs an issue and an overload.

---

<div class="post-metadata">

### Author: ![sablonl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sablonl/32/217805_2.png) [@sablonl](https://discourse.julialang.org/u/sablonl)
#### Post date: [March 3, 2026, 12:28pm UTC](https://discourse.julialang.org/t/mtk-with-montecarlomeasurements-error/135988/3 "2026-03-03T12:28:21Z")

</div>

A temporary workaround could be to register the function:

```julia-auto
function my_problematic_function(xp)
    # xp^1.8 -> domain error
    exp(1.8*log(xp)) # works
end
@register_symbolic my_problematic_function(xp)

```

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [March 3, 2026, 1:36pm UTC](https://discourse.julialang.org/t/mtk-with-montecarlomeasurements-error/135988/4 "2026-03-03T13:36:40Z")

</div>

> [@optimal\_georg](#):
>
> `my_param=> 1.6 ± 0.3`

Until the issue is sorted out, does it work if you avoid changing the type of the parameters when you remake? You could try to add `my_param=> 1.6 ± 0.0` to the `u0_nom` map to make sure the problem has the intended type from the start.

---

<div class="post-metadata">

### Author: ![optimal\_georg](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@optimal\_georg](https://discourse.julialang.org/u/optimal_georg)
#### Post date: [March 4, 2026, 10:14am UTC](https://discourse.julialang.org/t/mtk-with-montecarlomeasurements-error/135988/5 "2026-03-04T10:14:57Z")

</div>

Thanks for the Reply!

If I put my\_param into the u0\_nom map like you recommended, I get an

```julia-auto
ArgumentError: invalid index: ModelingToolkitBase.ParameterIndex{SciMLStructures.Tunable, Int64}(SciMLStructures.Tunable(), 1, false) of type ModelingToolkitBase.ParameterIndex{SciMLStructures.Tunable, Int64}

```

If I start off with

```julia-auto
@parameters my_param = 1.5 ± 0.1

```

I get a

```julia-auto
ERROR: MethodError: no method matching Float64(::Particles{Float64, 2000})
The type `Float64` exists, but no method is defined for this combination of argument types when trying to construct it.

```

---

<div class="post-metadata">

### Author: ![optimal\_georg](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@optimal\_georg](https://discourse.julialang.org/u/optimal_georg)
#### Post date: [March 4, 2026, 10:17am UTC](https://discourse.julialang.org/t/mtk-with-montecarlomeasurements-error/135988/6 "2026-03-04T10:17:11Z")

</div>

Thanks for your reply!

This workaround indeed works!

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [March 4, 2026, 11:42am UTC](https://discourse.julialang.org/t/mtk-with-montecarlomeasurements-error/135988/7 "2026-03-04T11:42:02Z")

</div>

Here’s an example where it works

> **[Parametric uncertainty - Robust control · DyadControlSystems](https://juliacomputing.github.io/DyadControlSystems.jl/dev/robust_control/#Parametric-uncertainty)**
>
> Robust control refers to a set of design and analysis methods that attempt to guarantee stability and performance of a closed-loop control system in the presence of uncertainties, such as plant model mismatch and unknown disturbances. | Documentation...

the difference is that the example defines the parameter with the type annotated, in your case it would be

```julia-auto
T = typeof(1.0 ± 0.0)
@parameters my_param::T = 1.5 ± 0.0

```

---

<div class="post-metadata">

### Author: ![optimal\_georg](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@optimal\_georg](https://discourse.julialang.org/u/optimal_georg)
#### Post date: [March 4, 2026, 2:05pm UTC](https://discourse.julialang.org/t/mtk-with-montecarlomeasurements-error/135988/8 "2026-03-04T14:05:21Z")

</div>

Thanks for the link! This certainly is more elegant as it avoids using remake, but I still get the stack overflow error
