Python 弹丸运动图给我一条直线
Python projectile motion graph gives me a straight line
这是应该计算运动的代码,但它产生的是一条直线而不是抛物线,我们将不胜感激。
import math as m
import matplotlib.pyplot as plt
import numpy as np
class Projectile_motion:
def __init__(self, V_x, V_y, g, delta_time):
self.gravity = g
self.V_x = V_x
self.V_y = V_y
self.delta_time = delta_time
def velocity(self, angle):
self.angle = angle
self.V_x= m.ceil(self.V_x *m.cos(self.angle))
self.V_y = m.ceil((self.V_y * m.sin(self.angle))) - (self.gravity * self.delta_time)
def distance(self, x, y):
self.x = x
self.y = y
self.x = self.x + (self.V_x * self.delta_time)
self.y = self.y + (self.V_y * m.sin(self.angle)) + (0.5 * self.gravity * ((self.delta_time)**2))
return self.x, self.y
ww = np.linspace(0, 50, num=5)
for i in ww:
attempt1 = Projectile_motion(30, 30, 9.8, i)
attempt1.velocity(1.042)
ss=attempt1.distance(0, 0)
plt.plot(ss)
plt.show()
输出:
您在这里有了一个良好的开端,但您需要清理模型中的一些物理特性。
如果你有 V_x 和 V_y,你不清楚你在用角度做什么,因为那已经由 V_x 和 V_y.
我建议你摆脱角度作为首发。然后,你只需要用 V_y:
做几件事
- 当你 运行 时,重复更新 V_y。重力应该在增加 V_y,对吧?现在您的 V_y 没有更新。它应该增加 9.8m/s^2,所以这是你的每一个时间步的更新
- 使用这个更新后的 V_y 来计算距离。所以在你更新 V_y 之后,只需使用它来改变 y 位置。你不应该在那里做更多的数学,只是
y += V_y*dt
如果你成功了,你可以转换回使用角度(我假设这是你的初始方向)并通过标准应用余弦和正弦计算 V_x 和 V_y角度。另外,请记住,数学模块需要 angular 在 弧度 中输入。
这是应该计算运动的代码,但它产生的是一条直线而不是抛物线,我们将不胜感激。
import math as m
import matplotlib.pyplot as plt
import numpy as np
class Projectile_motion:
def __init__(self, V_x, V_y, g, delta_time):
self.gravity = g
self.V_x = V_x
self.V_y = V_y
self.delta_time = delta_time
def velocity(self, angle):
self.angle = angle
self.V_x= m.ceil(self.V_x *m.cos(self.angle))
self.V_y = m.ceil((self.V_y * m.sin(self.angle))) - (self.gravity * self.delta_time)
def distance(self, x, y):
self.x = x
self.y = y
self.x = self.x + (self.V_x * self.delta_time)
self.y = self.y + (self.V_y * m.sin(self.angle)) + (0.5 * self.gravity * ((self.delta_time)**2))
return self.x, self.y
ww = np.linspace(0, 50, num=5)
for i in ww:
attempt1 = Projectile_motion(30, 30, 9.8, i)
attempt1.velocity(1.042)
ss=attempt1.distance(0, 0)
plt.plot(ss)
plt.show()
输出:
您在这里有了一个良好的开端,但您需要清理模型中的一些物理特性。
如果你有 V_x 和 V_y,你不清楚你在用角度做什么,因为那已经由 V_x 和 V_y.
我建议你摆脱角度作为首发。然后,你只需要用 V_y:
做几件事- 当你 运行 时,重复更新 V_y。重力应该在增加 V_y,对吧?现在您的 V_y 没有更新。它应该增加 9.8m/s^2,所以这是你的每一个时间步的更新
- 使用这个更新后的 V_y 来计算距离。所以在你更新 V_y 之后,只需使用它来改变 y 位置。你不应该在那里做更多的数学,只是
y += V_y*dt
如果你成功了,你可以转换回使用角度(我假设这是你的初始方向)并通过标准应用余弦和正弦计算 V_x 和 V_y角度。另外,请记住,数学模块需要 angular 在 弧度 中输入。