Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Project

Double Pendulum in
python
Elton Benjamin D'souza
& Taniya K C
CONTENTS
01 What is a Double
Pendulum?

02 The classical mechanics and


lagrangian equation of motion

03 The code

04 The chaotic behaviour


of
double pendulum
01
Double Pendulum

A double pendulum is simply two


pendulums connected end to end.
This system demonstrates chaos
theory and how small variations
lead to large changes.
Physical significance of
Double pendulum

 Robotic arms  Arms of crane is an inverted


double pendulum
02
The classical mechanics
behind double pendulum
 First find the generalised coordinate(Here
the angle,which is the only variable).
 Then, write kinetic energy and potential
energy in terms of generalised coordinate.
 Then find the Lagrangian, L from equation

 Now put this on lagrangian equation of


motion.

 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, the2 = smp.symbols(r'\theta_1, \theta_2', cls=smp.Function)

To define angle as a function of time


(Since,We eventually solve for these two angles):

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:

LE1 = smp.diff(L, the1) - smp.diff(smp.diff(L, the1_d), t).simplify()


LE2 = smp.diff(L, the2) - smp.diff(smp.diff(L, the2_d), t).simplify()

To solve this equation to get angular velocities of


pendulum 1 & 2:

sols = smp.solve([LE1, LE2], (the1_dd, the2_dd),


simplify=False, rational=False)
To get converts a second order ODE's to first order &
to solve numerically:

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) :

def dSdt(S, t, g, m1, m2, L1, L2):


the1, z1, the2, z2 = S
return [
dthe1dt_f(z1),
dz1dt_f(t, g, m1, m2, L1, L2, the1, the2, z1, z2),
dthe2dt_f(z2),
dz2dt_f(t, g, m1, m2, L1, L2, the1, the2, z1, z2),
]
To solve the system of ODE'S using scipy's
"odient" method:
t = np.linspace(0, 40, 1001)
g = 9.81
m1=2
m2=1
L1 = 2
L2 = 1
ans = odeint(dSdt, y0=[1, -3, -1, 5], t=t, args=(g,m1,m2,L1,L2))
Taking the transpose of "ans" to get the1, z1, the2, z2
in rows for ease of coding:

the1 = ans.T[0]
the2 = ans.T[2]

To polt graphically variation of the2 w. r. t. Time, t:

plt.plot(t, the2)
To get floating values for cartesian
coordinates (x1, y1)&(x2, y2) from L1, L2, the1 & the2:

def get_x1y1x2y2(t, the1, the2, L1, L2):


return (L1*np.sin(the1),
-L1*np.cos(the1),
L1*np.sin(the1)+L2*np.sin(the2),
-L1*np.cos(the1)-L2*np.cos(the2))
x1, y1, x2, y2 = get_x1y1x2y2(t, ans.T[0], ans.T[2], L1, L2)
To make animation of the motion of two lines with
the given cartesian coordinates:

def animate(i):
ln1.set_data([0, x1[i], x2[i]], [0, y1[i], y2[i]])

fig, ax = plt.subplots(1,1, figsize=(8,8))


ax.set_facecolor('k')
ax.get_xaxis().set_ticks([]) # enable this to hide x axis ticks
ax.get_yaxis().set_ticks([]) # enable this to hide y axis ticks
ln1, = plt.plot([], [], 'ro--', lw=3, markersize=8)
ax.set_ylim(-4,4)
ax.set_xlim(-4,4)
ani = animation.FuncAnimation(fig, animate, frames=1000, interval=50)
ani.save('pen.gif',writer='pillow',fps=25)
04
The Chaotic behaviour
Source: https://gereshes.com/2018/11/19/chaos-and-the-double-pendulum/
27
Chaotic dynamics, in a nutshell, means that a system
is extremely sensitive to initial conditions. That means
a small change in where the system begins, becomes a
big difference in where it ends up.
The Result
• We could plot the graph of the chaotic
motion of a double pendulum.
• We could animate using python.
THANK
YOU.........
Elton Benjamin D'souza &
Taniya K C

You might also like