Skip to content

Commit b2f4f54

Browse files
feat: menambahkan pendulum
1 parent ce827e3 commit b2f4f54

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

implementation/physics/pendulum.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# https://en.wikipedia.org/wiki/Pendulum
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
from matplotlib.animation import FuncAnimation
5+
6+
7+
def full_pendulum(g, l, theta, theta_velocity, time_step):
8+
"""
9+
fungsi untuk menghitung persamaan pendulum.
10+
"""
11+
theta_acceleration = -(g / l) * np.sin(theta)
12+
theta_velocity += time_step * theta_acceleration
13+
theta += time_step * theta_velocity
14+
return theta, theta_velocity
15+
16+
17+
g = 9.8 # konstanta gravitasi
18+
l = 1.0 # panjang tali
19+
20+
21+
theta = [np.radians(90)]
22+
theta_velocity = 0
23+
time_step = 20 / 300
24+
25+
26+
time_stap = np.linspace(0, 20, 300)
27+
"""
28+
Looping untuk memasukkan setiap angka.
29+
"""
30+
for t in time_stap:
31+
theta_new, theta_velocity = full_pendulum(g, l, theta[-1],
32+
theta_velocity, time_step)
33+
theta.append(theta_new)
34+
35+
# persamaan proyeksi panjang tali
36+
x = l * np.sin(theta)
37+
y = -l * np.cos(theta)
38+
39+
fig, axis = plt.subplots()
40+
41+
# batas sumbu x dan y
42+
axis.set_xlim(-l - 0.2 , l + 0.2)
43+
axis.set_ylim(-l - 0.2 , l)
44+
45+
plt.grid()
46+
47+
# benda yang akan bergerak
48+
rod_line, = axis.plot([], [], lw=2)
49+
mass_point, = axis.plot([], [], marker='o', markersize=10)
50+
trace, = axis.plot([], [], '-', lw=1, alpha=0.6)
51+
52+
53+
def animate(frame):
54+
"""
55+
Fungsi ini berguna untuk mengambil setiap hasil hitungan
56+
dan menjadikan setiap hitungan frame.
57+
"""
58+
rod_line.set_data([0, x[frame]], [0, y[frame]])
59+
mass_point.set_data([x[frame]], [y[frame]])
60+
trace.set_data(x[:frame], y[:frame])
61+
62+
return rod_line, mass_point, trace
63+
64+
65+
animation = FuncAnimation(
66+
fig=fig,
67+
func=animate,
68+
frames=len(time_stap),
69+
interval=25,
70+
blit=True
71+
)
72+
73+
plt.show()

0 commit comments

Comments
 (0)