# Multi-component pendulum becomes unstable

**URL:** https://discourse.julialang.org/t/multi-component-pendulum-becomes-unstable/115319
**Category:** Modelling & Simulations
**Tags:** modelingtoolkit
**Created:** [June 7, 2024, 10:40am UTC](https://discourse.julialang.org/t/multi-component-pendulum-becomes-unstable/115319 "2024-06-07T10:40:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![natlampen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natlampen/32/36640_2.png) [@natlampen](https://discourse.julialang.org/u/natlampen)
#### Post date: [June 7, 2024, 10:40am UTC](https://discourse.julialang.org/t/multi-component-pendulum-becomes-unstable/115319/1 "2024-06-07T10:40:29Z")

</div>

Hi everyone,

I have started my journey into using ModelingToolkit. So far it has been working quite well and I am very impressed.

I am going down the component-based path. I have a somewhat complex system in mind but I have started implementing a few components and testing them individually. I have hit an issue with some multi-body dynamics, I have a mass-less rigid Link, which when combined with a Mass component to create a pendulum has issues when crossing vertical. I have attached two files:

- A single component pendulum which seems to work as expected.
- A two component pendulum which appears to explode/become unstable when the pendulum crosses vertical (θ = -90). QNDF-solver does seem to stabilize the model but it seems to do so at the cost of dissipating a lot of energy from the system. Also, if gravity is applied in horizontal direction it does become stable as long as the pendulum does not cross vertical.

To the best of my knowledge the two systems should be equivalent.

ModelingToolkit does not generate the same set of equations for both systems but I am not sure that is to be expected. Output of full\_equations() below:

Single component system:

```julia
 Differential(t)(θ(t)) ~ θ̇(t)
 Differential(t)(θ̇(t)) ~ θˍtt(t)
 0 ~ (-cos(θ(t))*f(t)) / m + l*sin(θ(t))*θˍtt(t) + l*(θ̇(t)^2)*cos(θ(t))
 0 ~ (-g*m - sin(θ(t))*f(t)) / m - l*θˍtt(t)*cos(θ(t)) + l*sin(θ(t))*(θ̇(t)^2)

```

Two component system:

```julia
 Differential(t)(link₊pY₊u(t)) ~ link₊pY₊uˍt(t)
 Differential(t)(link₊pY₊uˍt(t)) ~ link₊l*cos(link₊θ(t))*link₊θˍtt(t) - link₊l*sin(link₊θ(t))*(link₊θˍt(t)^2)
 0 ~ -link₊pY₊u(t) + link₊l*sin(link₊θ(t))
 0 ~ mass₊g*mass₊m + link₊f(t)*sin(link₊θ(t)) + (link₊l*cos(link₊θ(t))*link₊θˍtt(t) - link₊l*sin(link₊θ(t))*(link₊θˍt(t)^2))*mass₊m
 0 ~ -link₊pY₊uˍt(t) + link₊l*cos(link₊θ(t))*link₊θˍt(t)
 0 ~ (cos(link₊θ(t))*link₊f(t)) / mass₊m - link₊l*sin(link₊θ(t))*link₊θˍtt(t) - link₊l*cos(link₊θ(t))*(link₊θˍt(t)^2)

```

I wonder what I am missing here? Any ideas/inputs on how to stabilize the model would be much appreciated.

[pendulum.jl](https://discourse.julialang.org/uploads/short-url/ooDkurW9aXvqbM3MYuel9x0dEQ8.jl) (1.2 KB)  
[pendulum\_split.jl](https://discourse.julialang.org/uploads/short-url/nGfepIMU7paPBmKKe7ZQuAoQsMF.jl) (2.0 KB)

---

<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: [June 8, 2024, 9:43am UTC](https://discourse.julialang.org/t/multi-component-pendulum-becomes-unstable/115319/2 "2024-06-08T09:43:44Z")

</div>

This is a phenomena due to dummy derivatives since there is not a globally stable cartesian coordinate choice for the multi-pendulum. It’s explained in detail in the lecture notes:

> **[Dummy Derivatives and Reordering of Equations · ModelingToolkit Course](https://sciml.github.io/ModelingToolkitCourse/dev/lectures/lecture8/)**
>
> Documentation for ModelingToolkit Course.

This case is also explained in the recent webinar starting at minute 41:

[![](https://global.discourse-cdn.com/julialang/original/3X/a/f/afd11ba3186bb32f7416c2d29b4f0a42ce12021e.jpeg "Advancements in Acausal Modeling with ModelingToolkit v9") ](https://www.youtube.com/watch?v=b47FIsAb8zw&t=2463)

As described, this case might want `dummy_derivative=false` in the structural simplification, though with the trade-offs as discussed in the video.

---

<div class="post-metadata">

### Author: ![natlampen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natlampen/32/36640_2.png) [@natlampen](https://discourse.julialang.org/u/natlampen)
#### Post date: [June 9, 2024, 5:30pm UTC](https://discourse.julialang.org/t/multi-component-pendulum-becomes-unstable/115319/3 "2024-06-09T17:30:06Z")

</div>

Thanks Chris. This really helped a lot.

My biggest concern was if I had made a mistake in my model but the issue I was seeing is to be expected for my test case. After seeing and reading through what you shared I converted into a very stiff spring and that seems to eliminate the singularity as well, although it adds some other complexities that may not be worth the trade of.
