# ModelingToolkit with DataInterpolation in time-varying external force

**URL:** <https://discourse.julialang.org/t/modelingtoolkit-with-datainterpolation-in-time-varying-external-force/61859>\
**Category:** General Usage\
**Tags:** diffeq, modelingtoolkit\
**Created:** [May 26, 2021, 2:40pm UTC](https://discourse.julialang.org/t/modelingtoolkit-with-datainterpolation-in-time-varying-external-force/61859 "2021-05-26T14:40:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lnacquaroli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lnacquaroli/32/20467_2.png) [@lnacquaroli](https://discourse.julialang.org/u/lnacquaroli)\
**Post date:** [May 26, 2021, 2:40pm UTC](https://discourse.julialang.org/t/modelingtoolkit-with-datainterpolation-in-time-varying-external-force/61859/1 "2021-05-26T14:40:21Z")

</div>

Hi there, I’m trying to get to work the simple example of an ODE with the external varying force with a `DataInterpolation` function, but I get an error building the `ODESystem`. The example is as follow:

```julia
using ModelingToolkit
using DifferentialEquations: solve
using DataInterpolations: LinearInterpolation

@variables t v(t) f(t) 
@parameters C      
D = Differential(t) 

a = float.(collect(0:10))
b = 2.0 .* a .+ randn() 
f_fun(t) = LinearInterpolation(b, a)(t)

@named im = ODESystem([f ~ f_fun(t), D(v) ~ f/C])

```

The last line errors with:

```julia
MethodError: isless(::Float64, ::Num) is ambiguous. Candidates:
  isless(a::Real, b::Num) in Symbolics at C:\Users\flvisa\.julia\packages\Symbolics\h8kPL\src\num.jl:156
  isless(x::AbstractFloat, y::Real) in Base at operators.jl:169
Possible fix, define
  isless(::AbstractFloat, ::Num)
lt(o::Base.Order.ForwardOrdering, a::Float64, b::Num) at ordering.jl:109
searchsortedfirst at sort.jl:184 [inlined]
searchsortedfirst at sort.jl:295 [inlined]
#searchsortedfirst#4 at sort.jl:297 [inlined]
searchsortedfirst at sort.jl:297 [inlined]
_interpolate(A::LinearInterpolation{Vector{Float64}, Vector{Float64}, true, Float64}, t::Num) at interpolation_methods.jl:3
(::LinearInterpolation{Vector{Float64}, Vector{Float64}, true, Float64})(t::Num) at DataInterpolations.jl:32
f_fun(t::Num) at untitled-35d453aeb3d55920c72726264c4c44d7:23
top-level scope at untitled-35d453aeb3d55920c72726264c4c44d7:25
eval at boot.jl:360 [inlined]

```

I made the interpolation to work for [this example](https://mtk.sciml.ai/stable/tutorials/ode_modeling/#Specifying-a-time-variable-forcing-function-1).

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:** [May 26, 2021, 3:45pm UTC](https://discourse.julialang.org/t/modelingtoolkit-with-datainterpolation-in-time-varying-external-force/61859/2 "2021-05-26T15:45:57Z")

</div>

Did you try `@register f_fun(t)`?

---

<div class="post-metadata">

**Author:** ![lnacquaroli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lnacquaroli/32/20467_2.png) [@lnacquaroli](https://discourse.julialang.org/u/lnacquaroli)\
**Post date:** [May 26, 2021, 5:35pm UTC](https://discourse.julialang.org/t/modelingtoolkit-with-datainterpolation-in-time-varying-external-force/61859/3 "2021-05-26T17:35:40Z")

</div>

Oh dear, yeah, now the example does work.

I still don’t get a good solution for the piecewise `f_fun(t)` I have, as shown below:

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

When I run the solver inside a `for` loop I get a fair solution, not good enough for the model, but closer than that from ModelingToolkit. Below the data `y2` I am trying to reproduce, and in blue the solution with the example above.

![results](https://global.discourse-cdn.com/julialang/original/3X/d/3/d331c7f9165cfba78ce375760eb51a6b079aff18.png)

Not sure if I am approaching correctly with the MTK though.

Thanks for your help Chris. Cheers!

---

<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 26, 2021, 6:24pm UTC](https://discourse.julialang.org/t/modelingtoolkit-with-datainterpolation-in-time-varying-external-force/61859/4 "2021-05-26T18:24:22Z")

</div>

Lower the tolerance and use `tstops`?

---

<div class="post-metadata">

**Author:** ![lnacquaroli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lnacquaroli/32/20467_2.png) [@lnacquaroli](https://discourse.julialang.org/u/lnacquaroli)\
**Post date:** [May 26, 2021, 6:40pm UTC](https://discourse.julialang.org/t/modelingtoolkit-with-datainterpolation-in-time-varying-external-force/61859/5 "2021-05-26T18:40:00Z")

</div>

The `tstops` did the trick.  
Thanks for the help again and for the great tool you guys developed.  
Cheers.
