forked from scienceetonnante/Chaos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pendule_comparaison.py
85 lines (64 loc) · 2.41 KB
/
pendule_comparaison.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# http://www.physics.usyd.edu.au/~wheat/dpend_html/solve_dpend.c
from numpy import sin, cos
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as integrate
import matplotlib.animation as animation
plt.rcParams['animation.ffmpeg_path'] = r'/Volumes/Data/Youtube/[ffmpeg]/ffmpeg'
G = 9.8 # acceleration due to gravity, in m/s^2
L1 = 1.0 # length of pendulum 1 in m
L2 = 0.8 # length of pendulum 2 in m
M1 = 1.2 # mass of pendulum 1 in kg
M2 = 1.0 # mass of pendulum 2 in kg
def derivs(state, t):
dydx = np.zeros_like(state)
dydx[0] = state[1]
del_ = state[2] - state[0]
den1 = (M1 + M2)*L1 - M2*L1*cos(del_)*cos(del_)
dydx[1] = (M2*L1*state[1]*state[1]*sin(del_)*cos(del_) +
M2*G*sin(state[2])*cos(del_) +
M2*L2*state[3]*state[3]*sin(del_) -
(M1 + M2)*G*sin(state[0]))/den1
dydx[2] = state[3]
den2 = (L2/L1)*den1
dydx[3] = (-M2*L2*state[3]*state[3]*sin(del_)*cos(del_) +
(M1 + M2)*G*sin(state[0])*cos(del_) -
(M1 + M2)*L1*state[1]*state[1]*sin(del_) -
(M1 + M2)*G*sin(state[2]))/den2
return dydx
# create a time array from 0..100 sampled at 0.05 second steps
dt = 0.04
t = np.arange(0.0, 20, dt)
# initial state : angles (degrees) and angular velocities (degrees per second)
th1 = 120.0
w1 = 0.0
th2 = -9.99
w2 = 0.0
state = np.radians([th1, w1, th2, w2])
# integrate your ODE using scipy.integrate.
res = integrate.odeint(derivs, state, t)
x1, y1 = L1*sin(res[:, 0]), -L1*cos(res[:, 0])
x2, y2 = L2*sin(res[:, 2]) + x1, -L2*cos(res[:, 2]) + y1
fig = plt.figure()
ax = fig.add_subplot(111, autoscale_on=False, xlim=(-2, 2), ylim=(-2, 2))
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
line, = ax.plot([], [], 'o-', lw=2)
time_template = 't = %.1fs'
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
def init():
line.set_data([], [])
time_text.set_text('')
return line, time_text
def animate(i):
print("Computing frame",i)
thisx = [0, x1[i], x2[i]]
thisy = [0, y1[i], y2[i]]
line.set_data(thisx, thisy)
time_text.set_text(time_template % (i*dt))
return line, time_text
ani = animation.FuncAnimation(fig, animate, np.arange(1, len(y)),
interval=40, blit=True, init_func=init)
writer = animation.FFMpegWriter(fps=25, bitrate=5000)
ani.save('double_pendulum_120_m9.99.mp4', writer = writer)
plt.show()