有没有办法在Python中定义浮点数组?
在我的天文学作业中,我需要模拟行星绕太阳的椭圆轨道。为此,我需要使用for循环来重复计算行星的运动。但是,每次尝试运行该程序时,都会出现以下错误:有没有办法在Python中定义浮点数组?,python,arrays,numpy,physics,astronomy,Python,Arrays,Numpy,Physics,Astronomy,在我的天文学作业中,我需要模拟行星绕太阳的椭圆轨道。为此,我需要使用for循环来重复计算行星的运动。但是,每次尝试运行该程序时,都会出现以下错误: RuntimeWarning: invalid value encountered in power r=(x**2+y**2)**1.5 Traceback (most recent call last): File "planetenstelsel3-4.py", line 25, in <module> ax[i] = a(x[i]
RuntimeWarning: invalid value encountered in power
r=(x**2+y**2)**1.5
Traceback (most recent call last):
File "planetenstelsel3-4.py", line 25, in <module>
ax[i] = a(x[i],y[i])*x[i]
ValueError: cannot convert float NaN to integer
前几行只是一些起始参数
提前谢谢你的帮助 当你进行物理模拟时,你绝对应该对所有事情都使用浮动
0
在Python中是一个整数常量,因此np.tile
创建整数数组;使用0.0
作为np.tile
的参数来执行浮点数组;或者最好使用np.zero(N)
:
您可以从其dtype
成员中检查任何数组变量的数据类型:
>>> np.tile(0, 10).dtype
dtype('int64')
>>> np.tile(0.0, 10).dtype
dtype('float64')
>>> np.zeros(10)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
>>> np.zeros(10).dtype
dtype('float64')
您需要
x=np.zero(N)
,等等:这将数组声明为浮点数组
这是在数组中放入零的标准方法(
np.tile()
便于使用固定数组创建平铺)。python中的int只受内存限制,而
是浮动的。从查看代码看,我不明白为什么,但是由于某种原因,您的a(x,y)
函数正在接收x或y的nan
值。是的,您可能应该将数组定义为浮点数。如果您使用的是tile,那么只需指定0即可。
而不是0
作为重复值:x=np.tile(0,N)
>>> np.tile(0, 10).dtype
dtype('int64')
>>> np.tile(0.0, 10).dtype
dtype('float64')
>>> np.zeros(10)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
>>> np.zeros(10).dtype
dtype('float64')