[英]How do I solve a 2nd order differential equation for projectile motion with air resistance?
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.其中r是弹丸的position,c是阻力系数,m是弹丸的质量,g是重力加速度。
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,如果我们采用上述方法并将 X 和 Y 中的速度明确定义为,
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.该 ODE 系统的参数为 c = 0.5 kgs^-1,m = 2kg 和 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.将变量初始化为
(U0, V0, X0, Y0) = (173, 100, 0, 0)
,它从原点以与水平方向成 ∼ 30 度角发射弹丸。
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....?如何使用 rk4(我想知道如何编写代码)在 python 中编写一个新的 function (我想知道如何编写代码),它实现了解决二维弹丸运动问题的上述四个 ODE 系统......? please help I am very new to ODEs AND CODING.
请帮助我对 ODE 和编码非常陌生。 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()
As nothing else happens in these times of a pandemic of delusions about phantasmic killer viruses...在这个关于幻想杀手病毒的错觉大流行的时代,没有其他任何事情发生……
Please do not call it air resistance, that is specifically k*|v|*v
.请不要将其称为空气阻力,即
k*|v|*v
。 As for this to be a force the coefficient k
would need to have units kg/m
, which is not what you are given, your formulas for the resistance are probably correct.至于这是一个力,系数
k
需要有单位kg/m
,这不是你给出的,你的阻力公式可能是正确的。 Call it "medium resistance" instead, water resistance would behave that way.相反,将其称为“中等阻力”,防水性能会如此。
Then code the acceleration然后编码加速度
c = 0.5; m = 2; g = -9.81;
def motion(x,v):
x,y,vx,vy = v
return np.array([vx,vy, -c/m * vx + g, -c/m * vy ])
Copy the RK4 code from somewhere that is geared towards vector states从适合向量状态的地方复制 RK4 代码
def RK4step(f,u,dt):
k1 = dt*f(u)
k2 = dt*f(u+0.5*k1)
k3 = dt*f(u+0.5*k2)
k4 = dt*f(u+k3)
return u + (k1+2*k2+2*k3+k4)/6
def RK4integrate(f, u0, tspan):
u = np.zeros([len(tspan),len(u0)])
u[0,:]=u0
for k in range(1, len(tspan)):
u[k,:] = RK4step(f, u[k-1], tspan[k]-tspan[k-1])
return u
and apply both codes together to compute a trajectory并将两个代码一起应用以计算轨迹
dt = .1
t = np.arange(0,10,dt)
u0 = np.array([0, 0, 173, 100])
sol_RK4 = RK4integrate(motion, u0, t)
x,y,vx,vy = sol_RK4.T
plt.plot(x,y)
Set v = dr/dt
.设置
v = dr/dt
。 Then the equations become:那么方程变为:
dv/dt = - (c/m) * v + g
Multiply both sides of the equation with exp((c/m)*t)
:将等式两边与
exp((c/m)*t)
相乘:
exp((c/m)*t) * dv/dt = - exp((c/m)*t) * (c/m) * v + exp((c/m)*t) * g
Move the first column of terms from the right-hand side to the left one:将第一列术语从右侧移到左侧:
exp((c/m)*t) * dv/dt + exp((c/m)*t) * (c/m) * v = exp((c/m)*t) * g
Then by the product rule of differentiation, applied to exp((c/m)*t) * v
, one gets然后通过微分的乘积规则,应用于
exp((c/m)*t) * v
,得到
d/dt( exp((c/m)*t) * v ) = exp((c/m)*t) * g
d/dt( exp((c/m)*t) * v ) = d/dt( (m/c) * exp((c/m)*t) * g )
Now you can integrate both sides with respect to t
and there exists a vector u0
such that现在您可以对
t
的两边进行积分,并且存在一个向量u0
使得
exp((c/m)*t) * v = u0 + (m/c) * exp((c/m)*t) * g
Multiply both sides by exp(-(c/m)*t)
:两边乘以
exp(-(c/m)*t)
:
v = exp(-(c/m)*t) * u0 + (m/c) * g
If the initial velocity is v0, then you have to set u0 = v0 - (m/c)*g
so the final formula for the velocity is:如果初始速度为 v0,则必须设置
u0 = v0 - (m/c)*g
,因此速度的最终公式为:
v = exp(-(c/m)*t) * ( v0 - (m/c)*g ) + (m/c) * g
Integrate v
one more time with respect to t
and you get the formula for the position:将
v
对t
再积分一次,得到 position 的公式:
r = r0 - (m/c) * ( v0 + (m/c)*g ) - (m/c) * exp(-(c/m)*t) * ( v0 - (m/c)*g ) + (m/c) * t * g
where r0
is the initial position.其中
r0
是初始 position。
So the final formulas for the solution are所以解决方案的最终公式是
r = r0 - v0 + (m/c)*g + exp(-(c/m)*t) * ( v0 - (m/c)*g ) + (m/c) * t * g
v = exp(-(c/m)*t) * ( v0 - (m/c)*g ) + (m/c) * g
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.