Uploaded by 2203-Rafly Aditya Darmawan

radar uas

advertisement
No 4
import numpy as np
import matplotlib.pyplot as plt
def vx(x, y):
return x**2 - y**2 - 4
def vy(x, y):
return 2 * x * y
x_min, x_max = -5, 5
y_min, y_max = -5, 5
x = np.linspace(x_min, x_max, 1000)
y = np.linspace(y_min, y_max, 1000)
vx_vec = vx(x, y)
vy_vec = vy(x, y)
u = vx_vec / np.sqrt(vx_vec**2 + vy_vec**2)
v = vy_vec / np.sqrt(vx_vec**2 + vy_vec**2)
plt.quiver(x, y, u, v, scale=10)
plt.show()
no 5
import numpy as np
def dy_dx(y, x):
return np.exp(x) * np.sin(x)
def euler(dy_dx, y0, x0, h, n):
y = np.zeros(n + 1)
x = np.zeros(n + 1)
y[0] = y0
x[0] = x0
for i in range(n):
y[i + 1] = y[i] + h * dy_dx(y[i], x[i])
x[i + 1] = x[i] + h
return x, y
x0 = 0
y0 = 1
h = 0.1
n = 10
x, y = euler(dy_dx, y0, x0, h, n)
print(y[10])
no 6
import numpy as np
import matplotlib.pyplot as plt
def f(t, x):
return -k * x - m * g * 0.5
def rk4(f, t0, x0, h, n):
t = np.zeros(n + 1)
x = np.zeros(n + 1)
t[0] = t0
x[0] = x0
for i in range(n):
k1 = h * f(t[i], x[i])
k2 = h * f(t[i] + h / 2, x[i] + k1 / 2)
k3 = h * f(t[i] + h / 2, x[i] + k2 / 2)
k4 = h * f(t[i] + h, x[i] + k3)
x[i + 1] = x[i] + (k1 + 2 * k2 + 2 * k3 + k4) / 6
t[i + 1] = t[i] + h
return t, x
t0 = 0
x0 = 0.4
h = 0.01
n = 1000
k = 20
m = 0.2
g = 9.81
t, x = rk4(f, t0, x0, h, n)
plt.plot(t, x)
plt.xlabel("Waktu (s)")
plt.ylabel("Posisi (m)")
plt.show()
Download