the equation is:
d^2 r/dt^2 = -c/m (dr/dt)+g
where r is the position of the projectile, c is the drag coefficient, m is the mass of the projectile and g is the acceleration due to gravity.
Assuming only two-dimensions in component form this, of course, reads,
d^2 X/dt^2 = -c(dX/dt)= U
d^2 Y/dt^2 = -c/m(dY/dt)+g
if we employ the methodology described above and define explicitly the velocities in X AND Y as,
U = dX/dt
and
V = dX/dt
then the entire coupled system of equations is,
dU/dt= -c/m(U)
dV/dt= - c/m(V)+g
dX/dt= U
dY/dt = V
The parameters for this system of ODEs are c = 0.5 kgs^−1, m = 2kg and g = −9.81 ms^−2.
initialising the variables as (U0, V0, X0, Y0) = (173, 100, 0, 0)
which launches the projectile from the origin at an angle of ∼ 30 degrees from the horizontal.
how to I write a new function in python using rk4 (I want to know how to code this) that implements the system of four ODEs above that solve the 2D projectile motion problem....? please help I am very new to ODEs AND CODING. THANKS
I have got the following so far...and its not working and I really don't know what to do for this specific problem, I am meant to obtain a projectile graph as well...can someone please improve my code pls thanks
import numpy as np
import matplotlib.pyplot as plt
def projectileMotion_V(t, M, g, c):
return -c/M * V0 + g
def projectileMotion_U(t, c, M):
return -c/M * U0
V0 = 100
U0 = 173
ang = 30.0
c = 0.5
dt = 0.1
M = 2.0
g = -9.81
h = 0.1
t = [0]
x = [0]
y = [0]
vx = [V0*np.cos((ang*np.pi)/180)]
vy = [U0*np.sin((ang*np.pi)/180)]
ax = [-(c*V0*np.cos((ang*np.pi)/180))/M]
ay = [g-(c*U0*np.sin((ang*np.pi)/180))/M]
def solveODEsWithR4Method(t, x, y, vx, vy, ax, ay):
t.append(t[0]+dt)
vx.append(vx[0]+dt*ax[0])
vy.append(vy[0]+dt*ay[0])
x.append(x[0]+dt*vx[0])
y.append(y[0]+dt*vy[0])
vel = np.sqrt(vx[0+1]**2 + vy[0+1]**2)
drag = c*vel
ax.append(-(drag*np.cos(ang/180*np.pi))/M)
ay.append(-g-(drag*np.sin(ang/180*np.pi)/M))
return -c/M * V0 + g
fig,ax = plt.subplots()
ax.plot(t, M, g, c)
plt.show()