在这个物体轨迹的Matplotlib图中,我的物理方程出了什么问题



我正在尝试使用matplotlib在python中绘制对象轨迹。我已经使x速度恒定,y速度受重力的影响。我使用方程Vx=Vo(cos(ϴ((和Vy=Vo(sin(\1012((来找到初始X和Y速度。我正在使用for循环来找到每十分之一秒炮弹所处的每个点。问题是,我的方程式肯定在某个地方不正确,因为图表不准确。这是我的密码。

import matplotlib.pyplot as plt
import math
def plot():
Position = [0,0] #Physical X and Y coordinates
BulletVelocity = 961 #m/s
Gravity = 9.8 #m/s^2
GraphList = [[],[]] # PlottedX and Y coodinates
frametime = 0.1  #Time between iterations in the for loop
theta = 44       #Angle at which projectile is launched
XVelocity = BulletVelocity * math.cos(theta) #initial X velocity
YVelocity = BulletVelocity * math.sin(theta) #initial Y velocity
time = 0
while Position[1] >= -1: #check for if the projetile has hit the ground
GraphList[0].append(Position[0])
GraphList[1].append(Position[1])
YVelocity = YVelocity - Gravity * time #Y Velocity changes due to gravity
Position[1] = YVelocity * time
Position[0] = XVelocity * time
time += frametime #time passes
plt.xlim(0,2000)
plt.ylim(0,2000)
plt.plot(GraphList[0],GraphList[1],'r') #Graph the list of points
plt.grid()
plt.show()
plot() #call the plot() function

这是我的图形

我没有足够的信誉点来发布图片,所以你必须在这里查看

您的问题是math.sinmath.cos需要弧度,而不是度数。通过将度数乘以π/180,可以将度数转换为弧度。如果将theta的定义更改为:

theta = (math.pi/180)*44

然后您的代码似乎可以工作。

弧度是主要问题,但我也想添加以下内容:

# s = vt - 0.5 * (a * t**2)
Position[1] = YVelocity * time - 0.5 * (-Gravity * time ** 2)

你只是错过了计算位移的加速度部分。不过,由于加速度为0(即(,因此x也可以

Position[0] = XVelocity * time - (0)

最新更新