Skip to content

Commit 47f6cec

Browse files
committed
cleaned up double_pendulum_animated.py
1 parent fc22cba commit 47f6cec

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed
Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
1-
# Double pendulum formula translated from the C code at
2-
# http://www.physics.usyd.edu.au/~wheat/dpend_html/solve_dpend.c
1+
"""
2+
Show animation of a double pendulum.
3+
4+
Double pendulum formula translated from the C code at
5+
http://www.physics.usyd.edu.au/~wheat/dpend_html/solve_dpend.c
6+
"""
37

4-
from numpy import sin, cos
58
import numpy as np
69
import matplotlib.pyplot as plt
710
import scipy.integrate as integrate
811
import matplotlib.animation as animation
912

10-
G = 9.8 # acceleration due to gravity, in m/s^2
11-
L1 = 1.0 # length of pendulum 1 in m
12-
L2 = 1.0 # length of pendulum 2 in m
13-
M1 = 1.0 # mass of pendulum 1 in kg
14-
M2 = 1.0 # mass of pendulum 2 in kg
13+
g = 9.8 # acceleration due to gravity, in m/s^2
14+
length1 = 1.0 # length of pendulum 1 in m
15+
length2 = 1.0 # length of pendulum 2 in m
16+
mass1 = 1.0 # mass of pendulum 1 in kg
17+
mass2 = 1.0 # mass of pendulum 2 in kg
1518

1619

1720
def derivs(state, t):
18-
21+
"""Derivatives"""
1922
dydx = np.zeros_like(state)
2023
dydx[0] = state[1]
2124

2225
del_ = state[2] - state[0]
23-
den1 = (M1 + M2)*L1 - M2*L1*cos(del_)*cos(del_)
24-
dydx[1] = (M2*L1*state[1]*state[1]*sin(del_)*cos(del_) +
25-
M2*G*sin(state[2])*cos(del_) +
26-
M2*L2*state[3]*state[3]*sin(del_) -
27-
(M1 + M2)*G*sin(state[0]))/den1
26+
den1 = ((mass1 + mass2) * length1 -
27+
mass2 * length1 * np.cos(del_) * np.cos(del_))
28+
dydx[1] = (mass2 * length1 * state[1] * state[1] * np.sin(del_) *
29+
np.cos(del_) +
30+
mass2 * g * np.sin(state[2]) * np.cos(del_) +
31+
mass2 * length2 * state[3] * state[3] * np.sin(del_) -
32+
(mass1 + mass2) * g * np.sin(state[0])) / den1
2833

2934
dydx[2] = state[3]
3035

31-
den2 = (L2/L1)*den1
32-
dydx[3] = (-M2*L2*state[3]*state[3]*sin(del_)*cos(del_) +
33-
(M1 + M2)*G*sin(state[0])*cos(del_) -
34-
(M1 + M2)*L1*state[1]*state[1]*sin(del_) -
35-
(M1 + M2)*G*sin(state[2]))/den2
36+
den2 = (length2 / length1) * den1
37+
dydx[3] = (-mass2 * length2 * state[3] * state[3] * np.sin(del_) *
38+
np.cos(del_) +
39+
(mass1 + mass2) * g * np.sin(state[0]) * np.cos(del_) -
40+
(mass1 + mass2) * length1 * state[1] * state[1] * np.sin(del_) -
41+
(mass1 + mass2) * g * np.sin(state[2])) / den2
3642

3743
return dydx
3844

@@ -53,11 +59,11 @@ def derivs(state, t):
5359
# integrate your ODE using scipy.integrate.
5460
y = integrate.odeint(derivs, state, t)
5561

56-
x1 = L1*sin(y[:, 0])
57-
y1 = -L1*cos(y[:, 0])
62+
x1 = length1 * np.sin(y[:, 0])
63+
y1 = -length1 * np.cos(y[:, 0])
5864

59-
x2 = L2*sin(y[:, 2]) + x1
60-
y2 = -L2*cos(y[:, 2]) + y1
65+
x2 = length2 * np.sin(y[:, 2]) + x1
66+
y2 = -length2 * np.cos(y[:, 2]) + y1
6167

6268
fig = plt.figure()
6369
ax = fig.add_subplot(111, autoscale_on=False, xlim=(-2, 2), ylim=(-2, 2))
@@ -70,21 +76,29 @@ def derivs(state, t):
7076

7177

7278
def init():
79+
"""Create a line and text to show time of the animation."""
7380
line.set_data([], [])
7481
time_text.set_text('')
82+
plt.xlabel('horizontal position (m)')
83+
plt.ylabel('vertical position (m)')
7584
return line, time_text
7685

7786

7887
def animate(i):
88+
"""Update the animation.
89+
90+
Updates the positions of the double pendulum and the time shown,
91+
indexed by i.
92+
"""
7993
thisx = [0, x1[i], x2[i]]
8094
thisy = [0, y1[i], y2[i]]
8195

8296
line.set_data(thisx, thisy)
83-
time_text.set_text(time_template % (i*dt))
97+
time_text.set_text(time_template % (i * dt))
8498
return line, time_text
8599

86100
ani = animation.FuncAnimation(fig, animate, np.arange(1, len(y)),
87101
interval=25, blit=True, init_func=init)
88102

89-
#ani.save('double_pendulum.mp4', fps=15)
103+
# ani.save('double_pendulum.mp4', fps=15)
90104
plt.show()

0 commit comments

Comments
 (0)