77import scipy .integrate as integrate
88import matplotlib .animation as animation
99
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
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
1515
1616
1717def derivs (state , t ):
@@ -46,19 +46,17 @@ def derivs(state, t):
4646th2 = - 10.0
4747w2 = 0.0
4848
49- rad = pi / 180
50-
5149# initial state
52- state = np .array ([th1 , w1 , th2 , w2 ])* pi / 180.
50+ state = np .radians ([th1 , w1 , th2 , w2 ])
5351
5452# integrate your ODE using scipy.integrate.
5553y = integrate .odeint (derivs , state , t )
5654
57- x1 = L1 * sin (y [:,0 ])
58- y1 = - L1 * cos (y [:,0 ])
55+ x1 = L1 * sin (y [:, 0 ])
56+ y1 = - L1 * cos (y [:, 0 ])
5957
60- x2 = L2 * sin (y [:,2 ]) + x1
61- y2 = - L2 * cos (y [:,2 ]) + y1
58+ x2 = L2 * sin (y [:, 2 ]) + x1
59+ y2 = - L2 * cos (y [:, 2 ]) + y1
6260
6361fig = plt .figure ()
6462ax = fig .add_subplot (111 , autoscale_on = False , xlim = (- 2 , 2 ), ylim = (- 2 , 2 ))
@@ -68,21 +66,23 @@ def derivs(state, t):
6866time_template = 'time = %.1fs'
6967time_text = ax .text (0.05 , 0.9 , '' , transform = ax .transAxes )
7068
69+
7170def init ():
7271 line .set_data ([], [])
7372 time_text .set_text ('' )
7473 return line , time_text
7574
75+
7676def animate (i ):
7777 thisx = [0 , x1 [i ], x2 [i ]]
7878 thisy = [0 , y1 [i ], y2 [i ]]
7979
8080 line .set_data (thisx , thisy )
81- time_text .set_text (time_template % (i * dt ))
81+ time_text .set_text (time_template % (i * dt ))
8282 return line , time_text
8383
8484ani = animation .FuncAnimation (fig , animate , np .arange (1 , len (y )),
85- interval = 25 , blit = True , init_func = init )
85+ interval = 25 , blit = True , init_func = init )
8686
8787#ani.save('double_pendulum.mp4', fps=15)
8888plt .show ()
0 commit comments