This page describes a HTML5 simulation modeling the motion of a damped triple pendulum. We get first equations of dynamics with Euler-Lagrange equations, and implement the solving of differential system with adaptative Runge-Kutta fourth-order
numerical method.
Differential system is deduced from Euler-Lagrange equations. With $L$ the Lagrangian, $q_{i}$ each of the 3 coordinates and $\dot{q_{i}}$ their time derivatives, we can write :
This equation is true when system is conservative (work of non-conservative forces is zero). When existing non-conservative forces whose work is not zero, we call them "damped" or "friction" forces. In this case, we have to add the following right member in the above equation : $\overrightarrow{F_{f}}=-\overrightarrow{\nabla_{v}}\,D_{r}$ with $D_{r}$ the Rayleigh's dissipation function. So we get :
with $v_{i}^{2}=\left\Vert\dfrac{\text{d}\overrightarrow{OM_{i}}}{\text{d}t}\right\Vert^{2}=\dfrac{\text{d}\overrightarrow{OM_{i}}}{\text{d}t}\cdot\dfrac{\text{d}\overrightarrow{OM_{i}}}{\text{d}t}$ and $h_{i}$ which is related to the height of mass $i$.
Calculation of Kinetic energy - $v_{i}^{2}$ terms are equal to :
where $k_{i}$ corresponds to damped or friction coefficient according to $\theta_{i}$ angle and $v_{i}^{2}$ to the calculated velocities in \eqref{eq-vi2}. The right term in Euleur-Lagranage is then given by :
Calculating the dynamics equations can be made by hand but it may be onerous. A quicker solution is to use the Symbolic Math Toolbox. After calculating the symbolic expression of Lagrangian, we get the differential system in an automatic way. See the following Matlab source compute_diff_system.m where these operations are performed.
We describe briefly this little code :
Starting from expressions of \eqref{eq-kinetic-potential} and $v_{i}^{2}$, we get the system Lagrangian with variables $t_{i}=\theta_{i}$, $dt_{i}=\dot{\theta_{i}}$ :
% Kinetic Energy
T=1/2*l1*l1*dt1*dt1*(m1+m2+m3)...+1/2*l2*l2*dt2*dt2*(m2+m3)...+1/2*l3*l3*dt3*dt3*m3...+l1*l2*dt1*dt2*cos(t1-t2)*(m2+m3)...+l2*l3*dt2*dt3*cos(t2-t3)*m3...+l1*l3*dt1*dt3*cos(t1-t3)*m3;% Potential Energy
V=g*(l1+l2+l3)*(m1+m2+m3)...-g*l1*(m1+m2+m3)*cos(t1)...-g*l2*(m2+m3)*cos(t2)...-g*l3*m3*cos(t3);% Lagrangian
L=T-V;
Let's take now the expression of Rayleigh's dissipation function :
% Rayleigh Dissipation function
Dr=1/2*l1*l1*dt1*dt1*(k1+k2+k3)...+1/2*l2*l2*dt2*dt2*(k2+k3)...+1/2*l3*l3*dt3*dt3*k3...+l1*l2*dt1*dt2*cos(t1-t2)*(k2+k3)...+l2*l3*dt2*dt3*cos(t2-t3)*k3...+l1*l3*dt1*dt3*cos(t1-t3)*k3;% Derivative of Rayleigh function
f1=diff(Dr,dt1);
f2=diff(Dr,dt2);
f3=diff(Dr,dt3);
We have now to establish the differential system to solve. This can be done in the following way :
% Get Lagrange equations and solve for expressing theta_second
Equations=Lagrange(L,[t1,dt1,ddt1,t2,dt2,ddt2,t3,dt3,ddt3]);[theta1_second theta2_second theta3_second]=solve(Equations(1)==-f1,...
Equations(2)==-f2,Equations(3)==-f3,ddt1,ddt2,ddt3);
Finally, we have the 3 equations of dynamics.
Remark :
Javascript language doesn't recognize exponent character like '^'. In order to convert equations expressions from Matlab to Javascript, as 'm1^2', to 'm1*m1', we can use the terminal command below (with sed) :
Taking $u=\dfrac{\text{d}x}{\text{d}t}$, $v=\dfrac{\text{d}y}{\text{d}t}$, $w=\dfrac{\text{d}z}{\text{d}t}$, and defining the vector $\overrightarrow{V}$ :
\begin{equation}
\overrightarrow{V}=\begin{bmatrix} x \\ u \\ y \\ v \\ z \\ w\end{bmatrix}
\end{equation}
We can reformulate the differential system to an equivalent form of \eqref{eq-rk4-basic} :
Below this canvas can simulate the motion of a triple pendulum. The total length of axis is fixed to 3 meters, each initial mass is set to 1 kg and its starting rotation speed is set to $5\,\text{rad.s}^{-1}$. You can drag'n drop with mouse each mass in order to set wanted initial position.
The Canvas HTML5 source is available on this link triple_pendulum.js. We begin by initializing graphics interface and then define functions corresponding to the differential system. Afterwards, adaptative Runge-Kutta 4th is implemented to compute next positions and velocities. Once getting these variables, we update the graphics display of triple pendulum.
Under some choices of axes length, or a relative important difference between the 3 masses of pendulum, angles $\theta_{i}$ may to vary very quickly. In these cases, a smaller time step than the default one ($h=0.005$) has to be used for avoiding divergence. This method is called "Adaptative Runke-Kutta". Indeed, the goal is to get a time step which is adapted as a function of the variability of computed variables : timestep is decreased when they are changing roughly and increased when variation is slowly.
For estimating this variability, we set a minimal tolerance beyond which one must reduce the time step. This is done by comparing the variables got by one iteration wih $h$ current time step and ones got by two iterations with current $h/2$. If difference is greater than tolerance, we set a smaller step, otherwise we increase it.