Solution (Workshop 3): Deriving the EOM of a pendulum (Solution)
In this tutorial you will learn to derive the Equations of Motion of the pendulum as covered in the lecture. That is, a pendulum attached to the floor with a spring. We also consider an external horizontal force acting on the mass.
Part 1: Kinematic equations
Using the following rotation matrix R(\(\phi\)): https://en.wikipedia.org/wiki/Rotation_matrix the zero angle points to the right for this matrix.
We first start by defining the variables. Assuming that x1, z1 and r1 do not change in time:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[1], line 2
1 import numpy as np
----> 2 from sympy import *
4 var("t s x1 z1 r1")
5 phi1 = Function("phi1")(t)
ModuleNotFoundError: No module named 'sympy'
Problem: Define the kinematic relations
Hint: How do you write the position of the pendulum (x2, z2) as a function of x1, z1, r1 and phi1?
The velocities can then be obtained using:
Problem: Define the velocities
Hint: Use derivatives.
Part 2: Energy equations
Kinetic energy:
The only mass in this system is the point mass located at P2 with mass \(M\)
Problem: Define the kinetic energy
This expression can be simplified to:
\[\displaystyle 0.5 M r_{1}^{2} \left(\frac{d}{d t} \phi_{1}{\left(t \right)}\right)^{2}\]
Potential energy:
Assuming again that \(l0\), the original length of the spring, does not change in time, then the elongation of the spring \(dl\) is given by:
Problem: Define the spring elongation dl
\[\displaystyle - l_{0} + \left(\left(- r_{1} \sin{\left(\phi_{1}{\left(t \right)} \right)} - z_{1} + z_{3}\right)^{2} + \left(- r_{1} \cos{\left(\phi_{1}{\left(t \right)} \right)} - x_{1} + x_{3}\right)^{2}\right)^{0.5}\]
The work done by the spring between P2 and P3 with stiffness \(k\) and gravity.
Problem: Define the potential energy V.
\[\displaystyle M g \left(r_{1} \sin{\left(\phi_{1}{\left(t \right)} \right)} + z_{1}\right) + 0.5 k \left(l_{0} - \left(\left(r_{1} \sin{\left(\phi_{1}{\left(t \right)} \right)} + z_{1} - z_{3}\right)^{2} + \left(r_{1} \cos{\left(\phi_{1}{\left(t \right)} \right)} + x_{1} - x_{3}\right)^{2}\right)^{0.5}\right)^{2}\]
Work by external force
The work done by an external force working in the horizontal direction:
Problem: Define the external work.
\[\displaystyle \left(r_{1} \cos{\left(\phi_{1}{\left(t \right)} \right)} + x_{1}\right) \operatorname{Fx}{\left(t \right)}\]
Step 3: Construct the Lagrangian
Problem: Use T, V and W to find the lagrangian L. Simplify it using L.evalf()
\[\displaystyle - M g \left(r_{1} \sin{\left(\phi_{1}{\left(t \right)} \right)} + z_{1}\right) + 0.5 M r_{1}^{2} \left(\frac{d}{d t} \phi_{1}{\left(t \right)}\right)^{2} - 0.5 k \left(l_{0} - \left(\left(r_{1} \sin{\left(\phi_{1}{\left(t \right)} \right)} + z_{1} - z_{3}\right)^{2} + \left(r_{1} \cos{\left(\phi_{1}{\left(t \right)} \right)} + x_{1} - x_{3}\right)^{2}\right)^{0.5}\right)^{2} - \left(r_{1} \cos{\left(\phi_{1}{\left(t \right)} \right)} + x_{1}\right) \operatorname{Fx}{\left(t \right)}\]
Step 4: Obtaining the EoM
In order to obtain the EoMs we have to take derivatives w.r.t. \(\phi_1\) and its velocity.
Problem: Use the Lagrangian expression to obtain the equation of motion.
Hint: Put all terms on the left hand side
\[\displaystyle \frac{r_{1} \left(- 1.0 k \left(l_{0} - \left(\left(r_{1} \sin{\left(\phi_{1}{\left(t \right)} \right)} + z_{1} - z_{3}\right)^{2} + \left(r_{1} \cos{\left(\phi_{1}{\left(t \right)} \right)} + x_{1} - x_{3}\right)^{2}\right)^{0.5}\right) \left(- x_{1} \sin{\left(\phi_{1}{\left(t \right)} \right)} + x_{3} \sin{\left(\phi_{1}{\left(t \right)} \right)} + z_{1} \cos{\left(\phi_{1}{\left(t \right)} \right)} - z_{3} \cos{\left(\phi_{1}{\left(t \right)} \right)}\right) + \left(\left(r_{1} \sin{\left(\phi_{1}{\left(t \right)} \right)} + z_{1} - z_{3}\right)^{2} + \left(r_{1} \cos{\left(\phi_{1}{\left(t \right)} \right)} + x_{1} - x_{3}\right)^{2}\right)^{0.5} \left(M g \cos{\left(\phi_{1}{\left(t \right)} \right)} + 1.0 M r_{1} \frac{d^{2}}{d t^{2}} \phi_{1}{\left(t \right)} - \operatorname{Fx}{\left(t \right)} \sin{\left(\phi_{1}{\left(t \right)} \right)}\right)\right)}{\left(\left(r_{1} \sin{\left(\phi_{1}{\left(t \right)} \right)} + z_{1} - z_{3}\right)^{2} + \left(r_{1} \cos{\left(\phi_{1}{\left(t \right)} \right)} + x_{1} - x_{3}\right)^{2}\right)^{0.5}}\]
Now we isolate it for the acceleration.
These cells can in practice take quite some time to execute. For this illustration the added spring stiffness k is set to 0, but it can take any value.
\[\displaystyle \frac{r_{1} \left(- 1.0 k \left(l_{0} - \left(\left(r_{1} \sin{\left(\epsilon \psi{\left(t \right)} + \phi_{0} \right)} + z_{1} - z_{3}\right)^{2} + \left(r_{1} \cos{\left(\epsilon \psi{\left(t \right)} + \phi_{0} \right)} + x_{1} - x_{3}\right)^{2}\right)^{0.5}\right) \left(- x_{1} \sin{\left(\epsilon \psi{\left(t \right)} + \phi_{0} \right)} + x_{3} \sin{\left(\epsilon \psi{\left(t \right)} + \phi_{0} \right)} + z_{1} \cos{\left(\epsilon \psi{\left(t \right)} + \phi_{0} \right)} - z_{3} \cos{\left(\epsilon \psi{\left(t \right)} + \phi_{0} \right)}\right) + \left(\left(r_{1} \sin{\left(\epsilon \psi{\left(t \right)} + \phi_{0} \right)} + z_{1} - z_{3}\right)^{2} + \left(r_{1} \cos{\left(\epsilon \psi{\left(t \right)} + \phi_{0} \right)} + x_{1} - x_{3}\right)^{2}\right)^{0.5} \cdot \left(1.0 M \epsilon r_{1} \frac{d^{2}}{d t^{2}} \psi{\left(t \right)} + M g \cos{\left(\epsilon \psi{\left(t \right)} + \phi_{0} \right)} - \operatorname{Fx}{\left(t \right)} \sin{\left(\epsilon \psi{\left(t \right)} + \phi_{0} \right)}\right)\right)}{\left(\left(r_{1} \sin{\left(\epsilon \psi{\left(t \right)} + \phi_{0} \right)} + z_{1} - z_{3}\right)^{2} + \left(r_{1} \cos{\left(\epsilon \psi{\left(t \right)} + \phi_{0} \right)} + x_{1} - x_{3}\right)^{2}\right)^{0.5}}\]
\[\displaystyle - r_{1} \operatorname{Fx}{\left(t \right)} \sin{\left(\phi_{0} \right)} - 1.0 k r_{1} z_{3} \cos{\left(\phi_{0} \right)} + 1.0 k r_{1} z_{1} \cos{\left(\phi_{0} \right)} + 1.0 k r_{1} x_{3} \sin{\left(\phi_{0} \right)} - 1.0 k r_{1} x_{1} \sin{\left(\phi_{0} \right)} + \frac{0.707106781186548 k l_{0} r_{1} z_{3} \cos{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{0.5}} - \frac{0.707106781186548 k l_{0} r_{1} z_{1} \cos{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{0.5}} - \frac{0.707106781186548 k l_{0} r_{1} x_{3} \sin{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{0.5}} + \frac{0.707106781186548 k l_{0} r_{1} x_{1} \sin{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{0.5}} + \epsilon \left(- M g r_{1} \psi{\left(t \right)} \sin{\left(\phi_{0} \right)} + 1.0 M r_{1}^{2} \frac{d^{2}}{d t^{2}} \psi{\left(t \right)} + \frac{0.353553390593274 k l_{0} r_{1}^{2} x_{1}^{2} \psi{\left(t \right)} \sin^{2}{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{1.5}} - \frac{0.707106781186548 k l_{0} r_{1}^{2} x_{1} x_{3} \psi{\left(t \right)} \sin^{2}{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{1.5}} - \frac{0.707106781186548 k l_{0} r_{1}^{2} x_{1} z_{1} \psi{\left(t \right)} \sin{\left(\phi_{0} \right)} \cos{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{1.5}} + \frac{0.707106781186548 k l_{0} r_{1}^{2} x_{1} z_{3} \psi{\left(t \right)} \sin{\left(\phi_{0} \right)} \cos{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{1.5}} + \frac{0.353553390593274 k l_{0} r_{1}^{2} x_{3}^{2} \psi{\left(t \right)} \sin^{2}{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{1.5}} + \frac{0.707106781186548 k l_{0} r_{1}^{2} x_{3} z_{1} \psi{\left(t \right)} \sin{\left(\phi_{0} \right)} \cos{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{1.5}} - \frac{0.707106781186548 k l_{0} r_{1}^{2} x_{3} z_{3} \psi{\left(t \right)} \sin{\left(\phi_{0} \right)} \cos{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{1.5}} + \frac{0.353553390593274 k l_{0} r_{1}^{2} z_{1}^{2} \psi{\left(t \right)} \cos^{2}{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{1.5}} - \frac{0.707106781186548 k l_{0} r_{1}^{2} z_{1} z_{3} \psi{\left(t \right)} \cos^{2}{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{1.5}} + \frac{0.353553390593274 k l_{0} r_{1}^{2} z_{3}^{2} \psi{\left(t \right)} \cos^{2}{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{1.5}} + \frac{0.707106781186548 k l_{0} r_{1} x_{1} \psi{\left(t \right)} \cos{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{0.5}} - \frac{0.707106781186548 k l_{0} r_{1} x_{3} \psi{\left(t \right)} \cos{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{0.5}} + \frac{0.707106781186548 k l_{0} r_{1} z_{1} \psi{\left(t \right)} \sin{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{0.5}} - \frac{0.707106781186548 k l_{0} r_{1} z_{3} \psi{\left(t \right)} \sin{\left(\phi_{0} \right)}}{\left(0.5 r_{1}^{2} \sin^{2}{\left(\phi_{0} \right)} + 0.5 r_{1}^{2} \cos^{2}{\left(\phi_{0} \right)} + r_{1} x_{1} \cos{\left(\phi_{0} \right)} - r_{1} x_{3} \cos{\left(\phi_{0} \right)} + r_{1} z_{1} \sin{\left(\phi_{0} \right)} - r_{1} z_{3} \sin{\left(\phi_{0} \right)} + 0.5 x_{1}^{2} - x_{1} x_{3} + 0.5 x_{3}^{2} + 0.5 z_{1}^{2} - z_{1} z_{3} + 0.5 z_{3}^{2}\right)^{0.5}} - 1.0 k r_{1} x_{1} \psi{\left(t \right)} \cos{\left(\phi_{0} \right)} + 1.0 k r_{1} x_{3} \psi{\left(t \right)} \cos{\left(\phi_{0} \right)} - 1.0 k r_{1} z_{1} \psi{\left(t \right)} \sin{\left(\phi_{0} \right)} + 1.0 k r_{1} z_{3} \psi{\left(t \right)} \sin{\left(\phi_{0} \right)} - r_{1} \operatorname{Fx}{\left(t \right)} \psi{\left(t \right)} \cos{\left(\phi_{0} \right)}\right) + M g r_{1} \cos{\left(\phi_{0} \right)} + O\left(\epsilon^{2}\right)\]
\[\displaystyle - r_{1} \operatorname{Fx}{\left(t \right)} \sin{\left(\phi_{0} \right)} + \epsilon \left(- M g r_{1} \psi{\left(t \right)} \sin{\left(\phi_{0} \right)} + 1.0 M r_{1}^{2} \frac{d^{2}}{d t^{2}} \psi{\left(t \right)} - r_{1} \operatorname{Fx}{\left(t \right)} \psi{\left(t \right)} \cos{\left(\phi_{0} \right)}\right) + M g r_{1} \cos{\left(\phi_{0} \right)} + O\left(\epsilon^{2}\right)\]
\[\displaystyle - M g r_{1} \psi{\left(t \right)} \sin{\left(\phi_{0} \right)} + M g r_{1} \cos{\left(\phi_{0} \right)} + 1.0 M r_{1}^{2} \frac{d^{2}}{d t^{2}} \psi{\left(t \right)} - r_{1} \operatorname{Fx}{\left(t \right)} \psi{\left(t \right)} \cos{\left(\phi_{0} \right)} - r_{1} \operatorname{Fx}{\left(t \right)} \sin{\left(\phi_{0} \right)}\]
\[\displaystyle \frac{M g \left(\psi{\left(t \right)} \sin{\left(\phi_{0} \right)} - \cos{\left(\phi_{0} \right)}\right) + \left(\psi{\left(t \right)} \cos{\left(\phi_{0} \right)} + \sin{\left(\phi_{0} \right)}\right) \operatorname{Fx}{\left(t \right)}}{M r_{1}}\]
The EOM of an actual pendulum can be recovered by removing the spring and the external force. Note that this returns a cos because \(\phi (t)=0\) was set to the right, rather than downward which is more conventional for a pendulum.
(-M*g*psi(t) - Fx(t))/(M*r1)
In the code above the high length of the expressions is a consequence of machine precision. This makes the execution times a multitude larger as well. Depending on the application it becomes more and more usefulf to simplify as early as possible in hte process. adding \(k: 0\) (or another value) to one of the earlier dictionaries for example does the trick here, but it can only be applied if the goal was to not include a constant stiffness spring in the first place.
Bonus: Obtaining the EOMs for nDOF system
If your system contains multiple DOFs, you will have to take the derivative of \(L\) towards each of these separately, thereby obtaining the EOM for each DOF. Let’s say you obtained the following two EOMs (the EOMs are just random things I entered):
\[\displaystyle - c u{\left(t \right)} e^{u{\left(t \right)} w{\left(t \right)}} \log{\left(\sqrt{t} \right)}^{2} \left(\frac{d}{d t} w{\left(t \right)}\right)^{2} - c \frac{d}{d t} w{\left(t \right)} - k u{\left(t \right)} + m \sin{\left(u{\left(t \right)} \right)} \frac{d^{2}}{d t^{2}} w{\left(t \right)} + m \cos{\left(w{\left(t \right)} \right)} \frac{d^{2}}{d t^{2}} u{\left(t \right)}\]
\[\displaystyle J w^{2}{\left(t \right)} \frac{d^{2}}{d t^{2}} u{\left(t \right)} + J \frac{d}{d t} w{\left(t \right)} \frac{d^{2}}{d t^{2}} w{\left(t \right)} - d \frac{d}{d t} w{\left(t \right)} + q u{\left(t \right)}\]
You can collect both EOMs into matrix form using the following code:
Resulting in the following system of equations:
\[\begin{split}\displaystyle \left[\begin{matrix}m \cos{\left(w{\left(t \right)} \right)} & m \sin{\left(u{\left(t \right)} \right)}\\J w^{2}{\left(t \right)} & J \frac{d}{d t} w{\left(t \right)}\end{matrix}\right]\end{split}\]
And the following right-hand-side:
\[\begin{split}\displaystyle \left[\begin{matrix}c u{\left(t \right)} e^{u{\left(t \right)} w{\left(t \right)}} \log{\left(\sqrt{t} \right)}^{2} \left(\frac{d}{d t} w{\left(t \right)}\right)^{2} + c \frac{d}{d t} w{\left(t \right)} + k u{\left(t \right)}\\d \frac{d}{d t} w{\left(t \right)} - q u{\left(t \right)}\end{matrix}\right]\end{split}\]