# How to specify nonhomogeneous system with ModelingToolkit

**URL:** <https://discourse.julialang.org/t/how-to-specify-nonhomogeneous-system-with-modelingtoolkit/74341>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit\
**Created:** [January 10, 2022, 2:39pm UTC](https://discourse.julialang.org/t/how-to-specify-nonhomogeneous-system-with-modelingtoolkit/74341 "2022-01-10T14:39:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bgctw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bgctw/32/22050_2.png) [@bgctw](https://discourse.julialang.org/u/bgctw)\
**Post date:** [January 10, 2022, 2:39pm UTC](https://discourse.julialang.org/t/how-to-specify-nonhomogeneous-system-with-modelingtoolkit/74341/1 "2022-01-10T14:39:39Z")

</div>

DifferentialEquations.jl can [deal with nonhomogeneous](https://diffeq.sciml.ai/stable/tutorials/ode_example/#Example-3:-Solving-Nonhomogeneous-Equations-using-Parameterized-Functions) systems specified as an ODEProblem, where some parameter also depends on time.

How do I specify the example as an ODESystem using ModelingToolkit?

So far I tried:

```julia
using ModelingToolkit, DifferentialEquations
modelingtoolkitize(prob)

using ModelingToolkit, DifferentialEquations
@parameters t
@variables θ(t) ω(t)
D = Differential(t)
@parameters l m g M(t)
eqs = [
    D(θ) ~ ω,
    #D(ω) ~ -3g/(2l)*sin(θ) + 3/(m*l^2)*M(t),
    D(ω) ~ -3g/(2l)*sin(θ) + 3/(m*l^2)*M,
    ]
p = [
    l => 1.0 # length [m]
    m => 1.0 # mass[m]
    g => 9.81 # gravitational acceleration [m/s²]
    M => (t->0.1sin(t)) # external torque [Nm]
        ]     
@named sys = ODESystem(eqs)    
sys = structural_simplify(sys)
states(sys)
equations(sys) # looks good
u0 = [
    θ => 0.01, # initial angular deflection [rad]
    ω => 0.0    
    ]
tspan = (0.0,10.0)    
prob = ODEProblem(sys,u0, tspan, p)
#prob = ODEProblem(sesam1,u0, tspan, p, jac=true)
sol = solve(prob)

```

but get error (Julia 1.7.1):  
`LoadError: MethodError: no method matching *(::Int64, ::var"#40#41")`

---

<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:** [January 10, 2022, 3:18pm UTC](https://discourse.julialang.org/t/how-to-specify-nonhomogeneous-system-with-modelingtoolkit/74341/2 "2022-01-10T15:18:20Z")

</div>

Just make M a state and it will simplify out.

---

<div class="post-metadata">

**Author:** ![bgctw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bgctw/32/22050_2.png) [@bgctw](https://discourse.julialang.org/u/bgctw)\
**Post date:** [January 10, 2022, 3:43pm UTC](https://discourse.julialang.org/t/how-to-specify-nonhomogeneous-system-with-modelingtoolkit/74341/3 "2022-01-10T15:43:44Z")

</div>

thanks, this worked.  
This way one can use `t` at the left hand side:

```julia
using ModelingToolkit, DifferentialEquations
@parameters t
@variables θ(t) ω(t) M(t)
D = Differential(t)
@parameters l m g 
Mf(t) = 0.1sin(t)
eqs = [
    D(θ) ~ ω,
    #D(ω) ~ -3g/(2l)*sin(θ) + 3/(m*l^2)*M(t),
    D(ω) ~ -3g/(2l)*sin(θ) + 3/(m*l^2)*M,
    M ~ Mf(t)
    ]
p = [
    l => 1.0 # length [m]
    m => 1.0 # mass[m]
    g => 9.81 # gravitational acceleration [m/s²]
        ]     
@named sys = ODESystem(eqs)    
sys = structural_simplify(sys)
states(sys)
equations(sys)
u0 = [
    θ => 0.01, # initial angular deflection [rad]
    ω => 0.0,    
    ]
tspan = (0.0,10.0)    
prob = ODEProblem(sys,u0, tspan, p)
sol = solve(prob)

```
