# Solving system of ode with Modeling Toolkit

**URL:** <https://discourse.julialang.org/t/solving-system-of-ode-with-modeling-toolkit/85524>\
**Category:** Modelling & Simulations\
**Created:** [August 9, 2022, 11:36am UTC](https://discourse.julialang.org/t/solving-system-of-ode-with-modeling-toolkit/85524 "2022-08-09T11:36:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [August 9, 2022, 11:36am UTC](https://discourse.julialang.org/t/solving-system-of-ode-with-modeling-toolkit/85524/1 "2022-08-09T11:36:00Z")

</div>

I solved this ode system with DifferentialEquations and got the correct result.  
I am trying with ModelingToolkit but I don’t get the correct result. What am I doing wrong?

```julia
p₀, q₀ = 0.0, 3.0

@variables t
@parameters P(t) Q(t)
∂t = Differential(t)

eqs = [∂t(Q) ~ 0.2P
       ∂t(P) ~ -80.0sin(Q)]
@named sys = ODESystem(eqs)

u₀ = [P=>p₀, Q=>q₀]

t_span = (0.0, 20.0)
prob = ODEProblem(sys, u₀, t_span)
sol = solve(prob, Tsit5())

```

The solution with DifferentialEquations that gives me the correct result is:

```julia
p₀, q₀ = 0.0, 3.0
function mySystem(du,u,p,t)
    du[1] = 0.2u[2]
    du[2] = -80.0sin(u[1])
end

u0 = [q₀, p₀]
tspan = (0.0,20.0)
prob = ODEProblem(mySystem, u0, tspan)
sol = solve(prob, Tsit5())

```

---

<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:** [August 9, 2022, 12:03pm UTC](https://discourse.julialang.org/t/solving-system-of-ode-with-modeling-toolkit/85524/2 "2022-08-09T12:03:59Z")

</div>

Interesting… the fix is:

```julia
using ModelingToolkit, OrdinaryDiffEq
p₀, q₀ = 0.0, 3.0

@variables t
@variables P(t) Q(t)
∂t = Differential(t)

eqs = [∂t(Q) ~ 0.2P
       ∂t(P) ~ -80.0sin(Q)]
@named sys = ODESystem(eqs)

u₀ = [P=>p₀, Q=>q₀]

t_span = (0.0, 20.0)
prob = ODEProblem(sys, u₀, t_span)
sol = solve(prob, Tsit5())

```

The only change was to make `@variables P(t) Q(t)` instead of parameters. It seems that by making them parameters, the automatic system detection put them as both variables and parameters, printing out:

```julia
julia> @named sys = ODESystem(eqs)
Model sys with 2 equations
States (2):
  Q(t)
  P(t)
Parameters (2):
  Q(t)
  P(t)

```

So it would be great if you could open an issue on ModelingToolkit.jl. We should add a sanity check that the intersection between states and parameters should be null. Somehow this one slipped by. Thanks for pointing this out!

---

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [August 9, 2022, 9:33pm UTC](https://discourse.julialang.org/t/solving-system-of-ode-with-modeling-toolkit/85524/3 "2022-08-09T21:33:50Z")

</div>

Sorry, my mistake. Declaring P and Q as parameters is wrong. P and Q must be declared as variables. There are no problems in Modeling Toolkit. Thank you.

---

<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:** [August 10, 2022, 10:08am UTC](https://discourse.julialang.org/t/solving-system-of-ode-with-modeling-toolkit/85524/4 "2022-08-10T10:08:45Z")

</div>

There is a problem with MTK. It should’ve thrown an error here to help you find this. We’ll add that.
