Pada latihan ini, kita menggunakan Python dan modul Matplotlib untuk perhitungan dan visualisasi
gerakan bola dilempar dari atas permukaan Bumi. Perhatikan Gambar di bawah ini.
Metode Euler
Dengan metode Euler kita memperoleh persamaan iterasi numerik untuk selang waktu Δ𝑡 adalah
𝑥𝑛+1 = 𝑥𝑛 + 𝑣𝑥,𝑛Δ𝑡
𝑣𝑥,𝑛+1 = 𝑣𝑥,𝑛 + 𝑎𝑥,𝑛 Δ𝑡
In [4]: # Iterasi metode Euler
# Notasi:
# xn untuk posisi pada indeks n
# xnp1 untuk posisi pada indeks n+1 (n plus 1)
# Nilai awal
xn = x0
vxn = vx0
# Array
x = [xn]
vx = [vxn]
t = [0.0]
tn = 0.0
for i in range(40):
# Update percepatan
axn = -g
# Metode Euler
xnp1 = xn + vxn*dt
vxnp1 = vxn + axn*dt
In [6]: #Visualisasi
plt.plot(t,x,'ob', label = 'Metode Euler')
plt.plot(t,xe,'-r', label = 'Eksak')
plt.xlabel('$t$')
plt.ylabel('$x$')
plt.legend()
plt.show()
Metode Leapfrog
Persamaan iterasi numerik untuk metode leapfrog dengan selang waktu Δ𝑡 adalah
𝑥𝑛+1 = 𝑥𝑛 + 𝑣𝑥,𝑛+ 12 Δ𝑡
𝑣𝑥,𝑛+ 12 = 𝑣𝑥,𝑛− 12 + 𝑎𝑥,𝑛 Δ𝑡
Untuk memulai iterasi kita membutuhkan pendekatan untuk kecepatan pada waktu 𝑡 = 12 , 𝑣𝑥, 12 .
𝑣𝑥, 12 = 𝑣𝑥,0 + 12 𝑎𝑥,0 Δ𝑡
# Nilai awal
ax0 = -g
vxs0 = vx0 + 0.5*ax0*dt
# Array
x = [x0]
vx = [vxs0]
t = [0.0]
tn = 0.0
xn = x0
vxn = vxs0
for i in range(40):
# Update percepatan
axn = -g
In [8]: #Visualisasi
plt.plot(t,x,'ob', label = 'Metode Leap Frog')
plt.plot(t,xe,'-r', label = 'Eksak')
plt.xlabel('$t$')
plt.ylabel('$x$')
plt.legend()
plt.show()
# Nilai awal
ax0 = -g
x1 = x0 + vx0*dt + 0.5*ax0*dt**2
xnm1 = x0
xn = x1
# Array
x = [x0, x1]
t2 = [0.0, dt]
tn = dt
for i in range(40):
# Update percepatan
axn = -g
In [10]: #Visualisasi
plt.plot(t2,x,'ob', label = 'Metode Verlet Posisi')
plt.plot(t,xe,'-r', label = 'Eksak')
plt.xlabel('$t$')
plt.ylabel('$x$')
plt.legend()
plt.show()
# Nilai awal
xn = x0
vxn = vx0
# Array
x = [x0]
vx = [vx0]
t = [0.0]
tn = 0.0
for i in range(40):
# Update percepatan
axn = -g
In [12]: #Visualisasi
plt.plot(t,x,'ob', label = 'Metode Verlet Posisi')
plt.plot(t,xe,'-r', label = 'Eksak')
plt.xlabel('$t$')
plt.ylabel('$x$')
plt.legend()
plt.show()
In [ ]: