Non linear Second Order DE in ModelingToolkit.jl

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

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*(mgθ + 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 = 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!

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:

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
1 Like

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.

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!