# Non linear Second Order DE in ModelingToolkit.jl

**URL:** <https://discourse.julialang.org/t/non-linear-second-order-de-in-modelingtoolkit-jl/89501>\
**Category:** New to Julia\
**Tags:** question, diffeq, modelingtoolkit, nonlinear\
**Created:** [October 29, 2022, 7:01pm UTC](https://discourse.julialang.org/t/non-linear-second-order-de-in-modelingtoolkit-jl/89501 "2022-10-29T19:01:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sh\_166](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sh_166/32/43965_2.png) [@Sh\_166](https://discourse.julialang.org/u/Sh_166)\
**Post date:** [October 29, 2022, 7:01pm UTC](https://discourse.julialang.org/t/non-linear-second-order-de-in-modelingtoolkit-jl/89501/1 "2022-10-29T19:01:11Z")

</div>

Hi, I am new to Julia and am trying to tackle a **non linear 2nd order coupled** DE. The differential equations are given below.

 ![Screenshot 2022-10-30 00.17.15](https://global.discourse-cdn.com/julialang/original/3X/c/8/c88a52df4b5d711e64225b64d0be70b32e75897a.png)

In particular I am having trouble with the **first term on the RHS of the last equation** (the one with the theta double dot). The first 2 equations are separable in the double derivatives, but the last one is not.  
I have tried the DifferentialEquations.jl package by itself (converting the problem into first order) and have solved it, but I rather liked the ModelingToolkit package and was wondering if there was a way to solve this using that.

MWE:

> eqns = [∂(∂(θ)) ~ -1\*(m_g_θ + m₂_g_ϕ + k\*(θ - ϕ)_r)/(m₁_l₁)  
> ∂(∂(ϕ)) ~ ((1+μ)_g_θ + (k/m₁)_(θ - ϕ)r + (μ - 1)gϕ - 2∂(ϕ)∂(r))/(l₂+r)  
> ∂(∂(r)) ~ - wr + l₁_∂(θ)^2 + l₂_∂(ϕ)^2 + 0.5_g_ϕ^2 + l₁_(θ - ϕ)\*∂(∂(θ))]
> 
> @named system = ODESystem(eqns)  
> model = ode\_order\_lowering(system)

(Please note that I have defined m = m\_1 + m\_2 and \mu = m\_2/m\_1)  
I used ode\_order\_lowering on my initial system, but this converts the problematic term to \theta\_{tt}(t), which runs into this error: _UndefVarError: θˍtt not defined_

If I do not use the order lowering technique, then the error that crops up is this:  
_InvalidSystemException: The derivative variable must be isolated to the left-hand side of the equation like `Differential(t)(r(t)) ~ ...`. You may want to use `structural_simplify` or the DAE form._

I have tried defining a new variable U(t) by myself and setting it to d\theta, but to no avail, the error message tells me that \theta is not unique.

Any help would be appreciated. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [October 29, 2022, 7:19pm UTC](https://discourse.julialang.org/t/non-linear-second-order-de-in-modelingtoolkit-jl/89501/2 "2022-10-29T19:19:06Z")

</div>

Something strange happened to the formatting of your example, you should have more multiply operators in there. And is should that `m` be an `m₂` or `m₁`? Try calling `structural_simplify` as suggested in the second error:

```julia
using ModelingToolkit

@variables t
∂ = Differential(t)

function test()
    @variables θ(t) ϕ(t) r(t)
    @parameters m m₂ g k m₁ l₁ μ l₂ w
    eqns = [∂(∂(θ)) ~ -1*(m*g*θ + m₂*g*ϕ + k*(θ - ϕ)r)/(m₁*l₁)
            ∂(∂(ϕ)) ~ ((1+μ)g*θ + (k/m₁)*(θ - ϕ)r + (μ - 1)g*ϕ - 2∂(ϕ)∂(r))/(l₂+r)
            ∂(∂(r)) ~ - w*r + l₁*∂(θ)^2 + l₂*∂(ϕ)^2 + 0.5g*ϕ^2 + l₁*(θ - ϕ)*∂(∂(θ))]
    @named system = ODESystem(eqns)
    model = structural_simplify(system)
end

```

---

<div class="post-metadata">

**Author:** ![Sh\_166](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sh_166/32/43965_2.png) [@Sh\_166](https://discourse.julialang.org/u/Sh_166)\
**Post date:** [October 29, 2022, 7:23pm UTC](https://discourse.julialang.org/t/non-linear-second-order-de-in-modelingtoolkit-jl/89501/3 "2022-10-29T19:23:01Z")

</div>

Yeah, I think it’s cause of \* sign being used for text formatting as well. Some of them are being misinterpreted, I guess.

And, that should be m, I have clarified now. Sorry for the inconvenience.

Thanks for your response, I’ll try and see if I can make some progress.

---

<div class="post-metadata">

**Author:** ![Sh\_166](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sh_166/32/43965_2.png) [@Sh\_166](https://discourse.julialang.org/u/Sh_166)\
**Post date:** [October 29, 2022, 7:28pm UTC](https://discourse.julialang.org/t/non-linear-second-order-de-in-modelingtoolkit-jl/89501/4 "2022-10-29T19:28:23Z")

</div>

I tried it out, but no avail; all I get are the relations:  
 \frac{d\theta}{dt} \ = \ \theta\_{t}   
 \frac{d^{2}\theta}{dt^{2}} \ = \ \theta\_{tt}   
Similar pair of equations for r and \phi.

Edit: I am sorry, it did work; I should’ve called the equations defined by structural\_simplify(system). Thanks a ton, @contradict, you were a massive help!
