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
+ """
3
7
4
- from numpy import sin , cos
5
8
import numpy as np
6
9
import matplotlib .pyplot as plt
7
10
import scipy .integrate as integrate
8
11
import matplotlib .animation as animation
9
12
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
15
18
16
19
17
20
def derivs (state , t ):
18
-
21
+ """Derivatives"""
19
22
dydx = np .zeros_like (state )
20
23
dydx [0 ] = state [1 ]
21
24
22
25
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
28
33
29
34
dydx [2 ] = state [3 ]
30
35
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
36
42
37
43
return dydx
38
44
@@ -53,11 +59,11 @@ def derivs(state, t):
53
59
# integrate your ODE using scipy.integrate.
54
60
y = integrate .odeint (derivs , state , t )
55
61
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 ])
58
64
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
61
67
62
68
fig = plt .figure ()
63
69
ax = fig .add_subplot (111 , autoscale_on = False , xlim = (- 2 , 2 ), ylim = (- 2 , 2 ))
@@ -70,21 +76,29 @@ def derivs(state, t):
70
76
71
77
72
78
def init ():
79
+ """Create a line and text to show time of the animation."""
73
80
line .set_data ([], [])
74
81
time_text .set_text ('' )
82
+ plt .xlabel ('horizontal position (m)' )
83
+ plt .ylabel ('vertical position (m)' )
75
84
return line , time_text
76
85
77
86
78
87
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
+ """
79
93
thisx = [0 , x1 [i ], x2 [i ]]
80
94
thisy = [0 , y1 [i ], y2 [i ]]
81
95
82
96
line .set_data (thisx , thisy )
83
- time_text .set_text (time_template % (i * dt ))
97
+ time_text .set_text (time_template % (i * dt ))
84
98
return line , time_text
85
99
86
100
ani = animation .FuncAnimation (fig , animate , np .arange (1 , len (y )),
87
101
interval = 25 , blit = True , init_func = init )
88
102
89
- #ani.save('double_pendulum.mp4', fps=15)
103
+ # ani.save('double_pendulum.mp4', fps=15)
90
104
plt .show ()
0 commit comments