Home Subscribe

7. Double Pendulum Simulation

2pendulum

This simulation is about the motion of a double pendulum system, which consists of two pendulums connected end-to-end. The simulation demonstrates the chaotic behavior that can arise in classical mechanical systems due to the inherent nonlinearity of the double pendulum’s equations of motion.

7.1. Use cases in engineering

  • Mechatronics: The double pendulum system serves as a useful model for studying the dynamics and control of robotic arms and legged robots. It provides insights into the challenges of controlling complex, multi-joint systems with nonlinear dynamics.

  • Vibration analysis: Double pendulum systems can be used to study the behavior of more complex vibrating structures, such as suspension bridges or building frames during earthquakes.

  • Chaos and nonlinear dynamics: The study of chaotic systems, like the double pendulum, helps engineers develop a deeper understanding of nonlinear dynamics and its impact on the stability and control of engineering systems.

7.2. Possible useful improvements

  • Implementing a more advanced numerical integration method (e.g., Runge-Kutta) to improve the accuracy of the simulation.

  • Adding control mechanisms, such as PID controllers or model predictive control, to study the stabilization of the double pendulum system.

  • Adding a graphical user interface (GUI) for inputting the parameters, initial conditions, and control strategies, making the simulation more interactive and user-friendly.

  • Extending the simulation to a three-dimensional space, allowing for a more realistic representation of real-world engineering systems.

  • Simulating the effects of friction and damping forces, as well as external disturbances, to better understand their impact on the system’s dynamics and stability.

Check this code on GitHub and you are welcome to improve it.

The double pendulum system can be described using the Lagrangian formalism, where the Lagrangian is defined as the difference between the kinetic and potential energies of the system.

The kinetic energy of the system can be expressed as:

\[T = 1/2 m_1 L_1^2 \dot{\theta_1}^2 + 1/2 m_2 (L_1^2 \dot{\theta_1}^2 + L_2^2 \dot{\theta_2}^2 + 2 L_1 L_2 \dot{\theta_1} \dot{\theta_2} \cos{(\theta_1 - \theta_2)})\]

where \(m_1\) and \(m_2\) are the masses of the pendulums, \(L_1\) and \(L_2\) are their lengths, and \(\theta_1\) and \(\theta_2\) are the angles that the pendulums make with respect to the vertical axis. The dots represent the time derivative of the variables.

The potential energy of the system can be expressed as:

\[V = -m_1 g L_1 \cos{\theta_1} - m_2 g (L_1 \cos{\theta_1} + L_2 \cos{\theta_2})\]

where \(g\) is the acceleration due to gravity.

The Lagrangian of the double pendulum system can be written as:

\[L = T - V\]

Using the Euler-Lagrange equation, the equations of motion for the double pendulum can be derived. These equations can be written as a set of four coupled, first-order ordinary differential equations:

\[\begin{align*} \dot{\theta_1} &= (L_2 p_1 - L_1 p_2 \cos{(\theta_1 - \theta_2)}) / (L_1^2 L_2 (m_1 + m_2 \sin^2{(\theta_1 - \theta_2)}))\\ \dot{\theta_2} &= (-L_1 m_1 p_1 \cos{(\theta_1 - \theta_2)} + (m_1 + m_2) L_2 p_2) / (L_1 L_2^2 (m_1 + m_2 \sin^2{(\theta_1 - \theta_2)}))\\ \dot{p_1} &= - (m_1 + m_2) g L_1 \sin{\theta_1} - m_2 L_1 L_2 \dot{\theta_2}^2 \sin{(\theta_1 - \theta_2)}\\ \dot{p_2} &= - m_2 g L_2 \sin{\theta_2} + m_2 L_1 L_2 \dot{\theta_1}^2 \sin{(\theta_1 - \theta_2)} \end{align*}\]

where \(p_1\) and \(p_2\) are the momenta of the pendulums.

The equations of motion can be implemented in Python as follows:

import numpy as np
from scipy.integrate import solve_ivp

#Double pendulum equations
def double_pendulum_equations(t, y, L1, L2, m1, m2,g):
    theta1, theta2, p1, p2 = y
    cos_diff = np.cos(theta1 - theta2)
    sin_diff = np.sin(theta1 - theta2)

    # Angular velocities
    theta1_dot = (L2 * p1 - L1 * p2 * cos_diff) / (L1 ** 2 * L2 * (m1 + m2 * sin_diff ** 2))
    theta2_dot = (-L1 * m1 * p1 - L1 * m2 * p1 * cos_diff + (m1 + m2) * L2 * p2) / (L1 * L2 ** 2 * (m1 + m2 * sin_diff ** 2))

    # Time derivatives of momenta
    p1_dot = - (m1 + m2) * g * L1 * np.sin(theta1) - m2 * L1 * L2 * theta2_dot * theta1_dot * sin_diff
    p2_dot = - m2 * g * L2 * np.sin(theta2) + m2 * L1 * L2 * theta1_dot * theta2_dot * sin_diff

    return [theta1_dot, theta2_dot, p1_dot, p2_dot]

where t is time, y is an array of the four variables [theta1, theta2, p1, p2], and the other parameters L1, L2, m1, m2, and g are passed as arguments.

The simulation can be performed using the solve_ivp function from the scipy.integrate module. This function integrates the equations of motion over a specified time span using a specified numerical method. The resulting solution is stored in the sol object, which contains the values of the variables at each time step.

The animation of the double pendulum system can be created using the Matplotlib library. The animation function plots the positions of the pendulums at each time step using the x and y coordinates of the pendulum endpoints. The endpoints are calculated using the equations:

\[\begin{align*} x_1 &= L_1 \sin{\theta_1}\\ y_1 &= -L_1 \cos{\theta_1}\\ x_2 &= x_1 + L_2 \sin{\theta_2}\\ y_2 &= y_1 - L_2 \cos{\theta_2} \end{align*}\]

where (\(x_1\), \(y_1\)) and (\(x_2\), \(y_2\)) are the endpoints of the first and second pendulums, respectively.

The animation also plots the pendulums as circles, with the first pendulum in red and the second in blue. The current time step is indicated by a black circle. The animation function clears the previous frame, sets the limits of the plot, and adds labels and text to the plot.



Add Comment

* Required information
1000
Drag & drop images (max 3)
What is the day after Friday?

Comments

No comments yet. Be the first!