-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathpendulum.py
More file actions
73 lines (54 loc) · 1.63 KB
/
pendulum.py
File metadata and controls
73 lines (54 loc) · 1.63 KB
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
# https://en.wikipedia.org/wiki/Pendulum
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def full_pendulum(g, l, theta, theta_velocity, time_step):
"""
fungsi untuk menghitung persamaan pendulum.
"""
theta_acceleration = -(g / l) * np.sin(theta)
theta_velocity += time_step * theta_acceleration
theta += time_step * theta_velocity
return theta, theta_velocity
g = 9.8 # konstanta gravitasi
l = 1.0 # panjang tali
theta = [np.radians(90)]
theta_velocity = 0
time_step = 20 / 300
time_stap = np.linspace(0, 20, 300)
"""
Looping untuk memasukkan setiap angka.
"""
for t in time_stap:
theta_new, theta_velocity = full_pendulum(g, l, theta[-1],
theta_velocity, time_step)
theta.append(theta_new)
# persamaan proyeksi panjang tali
x = l * np.sin(theta)
y = -l * np.cos(theta)
fig, axis = plt.subplots()
# batas sumbu x dan y
axis.set_xlim(-l - 0.2 , l + 0.2)
axis.set_ylim(-l - 0.2 , l)
plt.grid()
# benda yang akan bergerak
rod_line, = axis.plot([], [], lw=2)
mass_point, = axis.plot([], [], marker='o', markersize=10)
trace, = axis.plot([], [], '-', lw=1, alpha=0.6)
def animate(frame):
"""
Fungsi ini berguna untuk mengambil setiap hasil hitungan
dan menjadikan setiap hitungan frame.
"""
rod_line.set_data([0, x[frame]], [0, y[frame]])
mass_point.set_data([x[frame]], [y[frame]])
trace.set_data(x[:frame], y[:frame])
return rod_line, mass_point, trace
animation = FuncAnimation(
fig=fig,
func=animate,
frames=len(time_stap),
interval=25,
blit=True
)
plt.show()