【问题标题】:projectile motion simple simulation using numpy matplotlib python弹丸运动简单模拟使用numpy matplotlib python
【发布时间】:2016-03-17 21:43:14
【问题描述】:

我正在尝试以不同的角度绘制弹丸随时间变化的图形。角度范围从 25 到 60,每个初始角度在图表上应该有自己的线。 “弹丸在空中的总时间”的公式是 t 的公式。我不确定这个总时间是如何发挥作用的,因为我应该以不同的初始角度绘制不同时间的弹丸。我想我需要 x,x1,x2,x3,x4,x5 和 y 等价物来绘制所有六个不同角度的图形。但我很困惑如何处理所花费的时间。

import numpy as np 
import matplotlib.pylab as plot 

#initialize variables 
#velocity, gravity
v = 30 
g = -9.8 
#increment theta 25 to 60 then find  t, x, y
#define x and y as arrays 
theta = np.arange(25,65,5)   

t = ((2 * v) * np.sin(theta)) / g #the total time projectile remains in the #air
t1 = np.array(t) #why are some negative 



x = ((v * t1) * np.cos(theta))
y = ((v * t1) * np.sin(theta)) - ((0.5 * g) * (t ** 2))

plot.plot(x,y)
plot.show()

【问题讨论】:

  • np.cos 和它的表亲np.sin 和其他人期望以弧度为单位的角度,所以首先将thetanp.pi / 180 相乘或使用np.radians()。此外,无需将 t 转换为 np.array t1,因为它已经是一个 numpy 数组。

标签: python numpy matplotlib


【解决方案1】:

首先g是积极的!修正后,让我们看看一些方程式:

您已经知道这一点,但让我们花点时间讨论一下。为了得到粒子的轨迹,你需要知道什么?

初始速度和角度,对吧?问题是:在给定初始速度为v=somethingtheta=something 的情况下,在一段时间后找到粒子的位置。 首字母很重要!那是我们开始实验的时候。所以时间是连续参数!您不需要飞行时间。

还有一点:角度不能只写成60, 45, etc,python需要其他东西才能工作,所以你需要用数字来写它们,(0,90) = (0,pi/2)。

让我们看看代码:

import numpy as np
import matplotlib.pylab as plot
import math as m
#initialize variables
#velocity, gravity
v = 30
g = 9.8
#increment theta 25 to 60 then find  t, x, y
#define x and y as arrays

theta = np.arange(m.pi/6, m.pi/3, m.pi/36)

t = np.linspace(0, 5, num=100) # Set time as 'continous' parameter.

for i in theta: # Calculate trajectory for every angle
    x1 = []
    y1 = []
    for k in t:
        x = ((v*k)*np.cos(i)) # get positions at every point in time
        y = ((v*k)*np.sin(i))-((0.5*g)*(k**2))
        x1.append(x)
        y1.append(y)
    p = [i for i, j in enumerate(y1) if j < 0] # Don't fall through the floor                          
    for i in sorted(p, reverse = True):
        del x1[i]
        del y1[i]

    plot.plot(x1, y1) # Plot for every angle

plot.show() # And show on one graphic

【讨论】:

  • 打败我!:) 我认为如果你想精确地绘制直到下降点,则需要最大 t。而且你不需要mathnp.pi 完全没问题。或者反过来,因为您的解决方案并没有真正使用 numpy:删除 numpy 并使用 math 和 range for theta :)
  • :D 感谢您的建议和建议。如您所见,我的菜鸟力量很强xD
  • 别担心,1337ness 是随着时间和实践而来的;)不管怎样,我也只是在学习 python/numpy。
  • 没错。我用它来获得结果,我对编码/编程不感兴趣。我目前的问题是网络理论,使用 Networkx :/ ,我必须为我的研究做这件事。这意味着,从堆栈流中搜索 #@!% 并找到解决方案! :D
【解决方案2】:

你犯了很多错误。

首先,错误较少​​,但matplotlib.pylab 应该用于同时访问matplotlib.pyplotnumpy(以获得更多类似matlab 的体验),我认为更建议在脚本中使用matplotlib.pyplot as plt(另见this Q&A)。

其次,您的角度以度为单位,但默认情况下数学函数需要弧度。在将角度传递给三角函数之前,您必须将它们转换为弧度。

第三,您当前的代码将t1 设置为每个角度都有一个时间点。这不是您所需要的:您需要计算每个角度的最大时间 t(您在 t 中所做的),然后为每个角度创建一个从 0t 的时间向量 用于绘图!

最后,您需要在 y 的两个术语中使用相同的绘图时间向量,因为这是您的力学问题的解决方案:

y(t) = v_{0y}*t - g/2*t^2

这假设g 是肯定的,这在您的代码中又是错误的。除非你将y 轴设置为向下,但“射弹”这个词让我觉得不是这样。

这就是我要做的:

import numpy as np 
import matplotlib.pyplot as plt 

#initialize variables 
#velocity, gravity
v = 30 
g = 9.81  #improved g to standard precision, set it to positive
#increment theta 25 to 60 then find  t, x, y
#define x and y as arrays 
theta = np.arange(25,65,5)[None,:]/180.0*np.pi #convert to radians, watch out for modulo division

plt.figure()

tmax = ((2 * v) * np.sin(theta)) / g
timemat = tmax*np.linspace(0,1,100)[:,None] #create time vectors for each angle

x = ((v * timemat) * np.cos(theta))
y = ((v * timemat) * np.sin(theta)) - ((0.5 * g) * (timemat ** 2))

plt.plot(x,y) #plot each dataset: columns of x and columns of y
plt.ylim([0,35])
plot.show()

我利用plt.plot 将绘制两个矩阵输入的列相对于彼此的事实,因此不需要循环角度。我还使用 [None,:][:,None] 将 1d numpy arrays 分别转换为 2d 行向量和列向量。通过将行向量和列向量相乘,数组广播可确保生成的矩阵按照我们想要的方式运行(即 timemat 的每一列从 0 到对应的 tmax 需要 100 步)

结果:

【讨论】:

  • 谢谢!直到知道才看到这个,但我有一个问题。它如何知道以单独的颜色打印每个轨迹?
  • @mmb_rach 这是matplotlib.pyplot 的默认行为:随后的绘图用不同的颜色绘制。颜色是根据所谓的颜色循环选择的,您可以通过ax.set_color_cycle 为给定轴设置ax,例如plt.gca().set_color_cycle 用于当前轴。这必须在 绘图命令之前发出。上图还显示了默认的颜色循环:蓝-绿-红-青-紫-黄-黑(-重复)。我相信这与 matlab 的旧默认 colororder 相同,在 R2012b 中更改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多