Professional Documents
Culture Documents
Wa0014.
Wa0014.
Double Pendulum in
python
Elton Benjamin D'souza
& Taniya K C
CONTENTS
01 What is a Double
Pendulum?
03 The code
Then simplify
Dynamical equations for a double
pendulum
Consider a double bob pendulum with
masses m1 and m2 attached by rigid
massless
wires of lengths l1 and l2. Further, let the
angles the two wires make with the vertical
be denoted θ1 and θ2, as illustrated above.
Finally, let gravity be given by g. Then the
positions of the bobs are given by,
𝑥1 = 𝑙1 sin 𝜃1
𝑦1 = − 𝑙1 cos 𝜃1
𝑥2 = 𝑙1 sin 𝜃1 + 𝑙2 sin 𝜃2
𝑦2 = − 𝑙1 cos 𝜃1 − 𝑙2 cos 𝜃2
This differential equation can be solved by Runge-kutta method.
10
Runge-Kutta method
This is a methods to solve differential equations with higher accuracy,without
performing more calculations. One of the most significant advantages of
Runge-Kutaa formulae is that it requires the function’s values at some
specified points.
The formula for the fourth-order Runge-Kutta method is given by:
y1 = y0 + (⅙) (k1 + 2k2 + 2k3 + k4)
Here,
k1 = hf(x0, y0)
k2 = hf[x0 + (½)h, y0 + (½)k1]
k3 = hf[x0 + (½)h, y0 + (½)k2]
k4 = hf(x0 + h, y0 + k3)
03
The python code used & it's
functions
We are using jyupiter for this purpose
To mention the libraries we use:
import numpy as np
import sympy as smp
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from matplotlib import animation
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import pillow writer
To define appropriate symbols using sympy to the functions ,
• Time
• Leght of pendulum 1
• Length of pendulum 2
• Mass of pendulum 1
• Mass of pendulum 2
t, g = smp.symbols('t g')
m1, m2 = smp.symbols('m1 m2')
L1, L2 = smp.symbols('L1, L2')
To define angles made by each pendulum:
the1 = the1(t)
the2 = the2(t)
To define first and second derivative of angle
(For later on.Since they appears in lagrangian equation) :
the1_d = smp.diff(the1, t)
the2_d = smp.diff(the2, t)
the1_dd = smp.diff(the1_d, t)
the2_dd = smp.diff(the2_d, t)
To find the cartesian coordinates of position of the ends of
pendulum:
x1 = L1*smp.sin(the1)
y1 = -L1*smp.cos(the1)
x2 = L1*smp.sin(the1)+L2*smp.sin(the2)
y2 = -L1*smp.cos(the1)-L2*smp.cos(the2)
To define kinetic and potential engergy ,
hence obtainthe lagrangian, L:
# Kinetic
T1 = 1/2 * m1 * (smp.diff(x1, t) **2 + smp.diff(y1, t) **2)
T2 = 1/2 * m2 * (smp.diff(x2, t) **2 + smp.diff(y2, t) **2)
T = T1+T2
# Potential
V1 = m1*g*y1
V2 = m2*g*y2
V = V1 + V2
# Lagrangian
L = T-V
Now get the lagrangian equation of motion to substitute value of
Lagrangian in it:
dz1dt_f =
smp.lambdify((t,g,m1,m2,L1,L2,the1,the2,the1_d,the2_d),
sols[the1_dd])
dz2dt_f =
smp.lambdify((t,g,m1,m2,L1,L2,the1,the2,the1_d,the2_d),
sols[the2_dd])
dthe1dt_f = smp.lambdify(the1_d, the1_d)
dthe2dt_f = smp.lambdify(the2_d, the2_d)
All the system of ODE's specified using dS/dt(S, t) :
the1 = ans.T[0]
the2 = ans.T[2]
plt.plot(t, the2)
To get floating values for cartesian
coordinates (x1, y1)&(x2, y2) from L1, L2, the1 & the2:
def animate(i):
ln1.set_data([0, x1[i], x2[i]], [0, y1[i], y2[i]])