-
Notifications
You must be signed in to change notification settings - Fork 0
/
projeto_pendulo_sem_atrito.py
71 lines (50 loc) · 1.26 KB
/
projeto_pendulo_sem_atrito.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
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
dt = 1/60
L = 1
ymax = 2
theta0 = np.pi/2
w0 = 0
g = 9.81
r = (theta0, w0)
# r = [theta, w]
def f_r(ri):
thetai, wi = ri
f_thetai = wi
f_wi = -g/L*np.sin(thetai)
return np.array([f_thetai, f_wi])
x0 = L*np.sin(r[0])
y0 = ymax - L*np.cos(r[0])
fig, ax = plt.subplots()
ax.set_aspect('equal')
plt.ylim([-0.25, 3.05])
plt.xlim([-1.65, 1.65])
plt.grid()
plt.title("Pêndulo simples sem atrito")
xx_chao = [-1.65, 1.65, 1.65,-1.65]
yy_chao = [0, 0, -0.25, -0.25]
xx_mastro = [0, 0]
yy_mastro = [0, ymax]
xx_corda = [0, x0]
yy_corda = [ymax, y0]
h = ax.plot(xx_mastro, yy_mastro, color="gray")
floor = ax.fill(xx_chao, yy_chao, color="black")
ball, = ax.plot([x0], [y0], color="red", marker="o")
l, = ax.plot(xx_corda, yy_corda)
def animate(i):
global r
k1 = dt*f_r(r)
k2 = dt*f_r(r + k1/2)
k3 = dt*f_r(r + k2/2)
k4 = dt*f_r(r + k3)
r = r + 1/6*(k1 + 2*k2 + 2*k3 + k4)
theta, w = r
x = L*np.sin(theta)
y = ymax - L*np.cos(theta)
xx_corda[1] = x
yy_corda[1] = y
l.set_data(xx_corda, yy_corda)
ball.set_data([x], [y])
ani = FuncAnimation(fig, animate, frames=None, interval=dt*1000)
plt.show()