# ModelingToolkit | Error with uncertain parameters using Measurements.jl

**URL:** <https://discourse.julialang.org/t/modelingtoolkit-error-with-uncertain-parameters-using-measurements-jl/105701>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit\
**Created:** [November 2, 2023, 1:51pm UTC](https://discourse.julialang.org/t/modelingtoolkit-error-with-uncertain-parameters-using-measurements-jl/105701 "2023-11-02T13:51:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabianmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabianmueller/32/202905_2.png) [@fabianmueller](https://discourse.julialang.org/u/fabianmueller)\
**Post date:** [November 2, 2023, 1:51pm UTC](https://discourse.julialang.org/t/modelingtoolkit-error-with-uncertain-parameters-using-measurements-jl/105701/1 "2023-11-02T13:51:46Z")

</div>

Hello! 🙂  
Lately I am always getting an error when simulating a ModelingToolkit.jl model with uncertain parameters from Measurements.jl. I have the feeling that this worked before and the error especially came up after upgrading Julia from 1.8 to 1.9 but I am not exactly sure.

My code is the following:

```julia
using ModelingToolkit
using DifferentialEquations
using Measurements
@variables t
D = Differential(t)
@mtkmodel batchferm1 begin
@parameters begin
	μ_max = 0.6 ± 0.1
	k_s = 0.05
	Yₓ = 0.45 ± 0.1
end
@variables begin
	X(t)
	S(t)
	μ(t)
end
@equations begin
	D(X) ~ μ * X
	D(S) ~ - μ / Yₓ * X
	μ ~ μ_max * S / (k_s + S)
end

@mtkbuild batch1 = batchferm1()
prob1 = ODEProblem(batch1, 
		[batch1.X => 0.3,
		batch1.S => 10.0],
		(0,7.5));
sol = solve(prob1, Tsit5(), reltol=1e-6, abstol=1e-6)

```

I am getting the following error:

MethodError: no method matching Float64(::Measurements.Measurement{Float64})  
Closest candidates are:  
(::Type{T})(::Real, !Matched::RoundingMode) where T\<:AbstractFloat  
@ Base rounding.jl:207  
(::Type{T})(::T) where T\<:Number  
@ Core boot.jl:792  
(::Type{T})(!Matched::AbstractChar) where T\<:Union{AbstractChar, Number}  
@ Base char.jl:50

---

<div class="post-metadata">

**Author:** ![fabianmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabianmueller/32/202905_2.png) [@fabianmueller](https://discourse.julialang.org/u/fabianmueller)\
**Post date:** [November 2, 2023, 2:03pm UTC](https://discourse.julialang.org/t/modelingtoolkit-error-with-uncertain-parameters-using-measurements-jl/105701/2 "2023-11-02T14:03:05Z")

</div>

When I am removing the parameter uncertainties it simlates perfectly fine.

---

<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:** [November 2, 2023, 2:06pm UTC](https://discourse.julialang.org/t/modelingtoolkit-error-with-uncertain-parameters-using-measurements-jl/105701/3 "2023-11-02T14:06:36Z")

</div>

Hello and welcome to the community! 👋  
Could you try making the initial condition of the uncertain type?

```julia
batch1.X => 0.3 ± 0.0

```

should do it

---

<div class="post-metadata">

**Author:** ![fabianmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabianmueller/32/202905_2.png) [@fabianmueller](https://discourse.julialang.org/u/fabianmueller)\
**Post date:** [November 2, 2023, 2:09pm UTC](https://discourse.julialang.org/t/modelingtoolkit-error-with-uncertain-parameters-using-measurements-jl/105701/4 "2023-11-02T14:09:57Z")

</div>

Thank you!  
Yes I haven’t tried this 😉

---

<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:** [November 2, 2023, 3:53pm UTC](https://discourse.julialang.org/t/modelingtoolkit-error-with-uncertain-parameters-using-measurements-jl/105701/5 "2023-11-02T15:53:35Z")

</div>

Note, that you get a very poor approximation to the uncertainty in the solution using linear uncertainty propagation, the code below allows you to switch to nonlinear uncertainty propagation by redefining the `±` and the difference is rather big

```julia
using ModelingToolkit
using Plots
using OrdinaryDiffEq

import Measurements, MonteCarloMeasurements

± = Measurements.:(±) # Pick one or the other
± = MonteCarloMeasurements.:(∓)

@parameters t
D = Differential(t)
@mtkmodel batchferm1 begin
    @parameters begin
        μ_max = 0.6 ± 0.1
        k_s = 0.05
        Yₓ = 0.45 ± 0.1
    end
    @variables begin
        X(t)
        S(t)
        μ(t)
    end
    @equations begin
        D(X) ~ μ * X
        D(S) ~ - μ / Yₓ * X
        μ ~ μ_max * S / (k_s + S)
    end
end

@mtkbuild batch1 = batchferm1()
tspan = (0,7.5)
prob1 = ODEProblem(batch1, 
		[batch1.X => 0.3 ± 0,
		batch1.S => 10.0],
		tspan);
sol = solve(prob1, Tsit5(), reltol=1e-6, abstol=1e-6)

t = range(tspan..., length=100)
plot(t, sol(t, idxs=1).u, layout=2, sp=1)
plot!(t, sol(t, idxs=2).u, sp=2)

```

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

Nonlinear:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/9/89cd5a8c8df2fcccc5858f6f37d83ec7aadff87a.png)

In particular, the linear version

- gives zero uncertainty for the second variable after t \approx 5
- severely underestimates the uncertainty for the first variable after the same time point
- Says that it’s possible for the second variable to be negative

So if this problem is representative of what you would like to do, it would probably be wise to interpret the result with caution.

---

<div class="post-metadata">

**Author:** ![fabianmueller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabianmueller/32/202905_2.png) [@fabianmueller](https://discourse.julialang.org/u/fabianmueller)\
**Post date:** [November 2, 2023, 4:14pm UTC](https://discourse.julialang.org/t/modelingtoolkit-error-with-uncertain-parameters-using-measurements-jl/105701/6 "2023-11-02T16:14:24Z")

</div>

Wow thanks a lot! Yes I already encountered that and thought nonlinear propagation might be better especially because of the highly nonlinear behaviour at about 5 h (batch end) … but I had no idead that this is so easy to achieve 😃

Really appreciate it

---

<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:** [November 2, 2023, 5:18pm UTC](https://discourse.julialang.org/t/modelingtoolkit-error-with-uncertain-parameters-using-measurements-jl/105701/7 "2023-11-02T17:18:21Z")

</div>

> [@baggepinnen](#):
>
> Could you try making the initial condition of the uncertain type?

Is the conclusion that _at least_ one of the initial conditions must be uncertain if some of the parameters are uncertain?

---

<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:** [November 2, 2023, 6:46pm UTC](https://discourse.julialang.org/t/modelingtoolkit-error-with-uncertain-parameters-using-measurements-jl/105701/8 "2023-11-02T18:46:22Z")

</div>

All of the entries will be promoted to the closest common supertype, and standard floats thus promotes to uncertain numbers if at least one number is uncertain.

The initial condition is used to determine the element type of the integrator state.
