【发布时间】:2014-06-29 22:25:13
【问题描述】:
这是我在这里的第一篇文章。 所以我正在尝试使用可视化python制作一个模型太阳系。我将每个行星定义为一个球体,具有半径、与太阳的距离、质量和动量变量。然后将每个行星(或天体)放入一个列表结构中。正如你所看到的,目前我有一个列表,其中包含 [earth, moon, mars] 太阳被排除在外,原因我稍后会解释。
所以当我试图计算每个身体对另一个身体造成的力时,我的问题就来了。我在这里所做的是,对于身体列表中的每个第 i 个值,计算它和第 n 个身体之间的力,列表身体中第 i 个身体上的力是第 i 个身体和每个第 n 个身体之间的力的总和正文从 0 到列表末尾。 (即列表中所有其他物体的所有力的总和)
这适用于月球和火星(列表中的第 2 和第 3 项),但不适用于地球。下面代码的输出是,
<3.57799e+022, 0, 0>
<4.3606e+020, 0, 0>
<1.64681e+021, 0, 0>
<-1.#IND, -1.#IND, -1.#IND> - this is the total force on earth.
<0, 2.07621e+027, 0>
<0, 9.83372e+027, 0>
from visual import *
AU = 149.6e9
MU = 384.4e6 # moon - earth orbital - radius
MarU = 227.92e9
G =6.673e-11
sun_mass =2e30
sun_radius =6.96e8
earth_mass =6e24
earth_radius =6.37e6
moon_mass =7.35e22
moon_radius =1.74e6
mars_mass = 6.41e23
mars_radius = 3390000
sun = sphere ( pos =(0 , 0 ,0) , velocity = vector (0 ,0 ,0) ,mass = sun_mass , radius =0.1* AU , color = color . yellow )
earth = sphere ( pos =( AU , 0 ,0) ,mass = earth_mass , radius =63170000, color = color . cyan ,make_trail=True )# create a list of gravitating objects
moon = sphere ( pos =( AU+MU , 0 ,0) ,mass = moon_mass , radius =17380000 , color = color . white, make_trail=True )
mars = sphere ( pos =( MarU , 0 ,0) ,mass = mars_mass , radius = mars_radius , color = color . red, make_trail=True )
#initialise values:
we = 1.9578877e-7
wm = sqrt(G*earth.mass/3.38e8**3)
wma = 9.617e-5
dt = 3*60
earth.mom = vector(0,1.5e11*earth.mass*we,0)
mars.mom = vector(0, 9.833720638948e+27,0)
moon.mom = moon.mass*(earth.mom/earth.mass+vector(0,-3.48e8*wm,0))
bodies = [earth, moon, mars]
*N = 0
initialdiff = 0
for i in bodies:
initialdiff = i.pos - sun.pos
TotalForce = (G * i. mass * sun. mass * norm ( initialdiff )/ initialdiff . mag2)
print TotalForce
while N < len(bodies):
if N!=i:
diff = i.pos - bodies[N].pos
Force = (G * i. mass * bodies[N]. mass * norm ( diff )/ diff . mag2)
TotalForce = TotalForce + Force
i.mom = i.mom+TotalForce*dt
N = N+1
else:
N = N+1
print earth.mom
print moon.mom
print mars.mom*
感谢您提供的任何帮助。
【问题讨论】:
-
您能否更具体地了解“正确”工作的含义? IE。您的期望是什么,您的代码与您的期望有何不同?
-
您好,很抱歉。所以有问题的部分是计算列表中每个对象的净力。向量列表是代码的输出。前三个正确地显示了列表中每个物体由于太阳而受到的力(初始力),然后接下来的三个向量显示了总合力(太阳的力加上列表中其他物体的力)然而,地球的矢量输出并没有输出数字,我对#IND 的实际含义感到困惑。我也很困惑为什么会出现这个结果,我看不到循环结构在哪里中断。
-
-1.#IND 是一个特殊的结果,通常表示零除以零...这可能发生在您的代码中的某处吗?
-
我初始化了 initialdiff 的值,该值用于计算力,其中一组值的乘积除以初始 diff。然而,就在“for i in body”之后,我将初始差异的值设置为“initialdiff = i.pos - sun.pos”,所以这应该不是问题
-
我认为这可能与 earth 在列表中的索引为 0 的事实有关,并且我已将 N 初始化为 0。 if 语句的条件是 if N != i然后。但是对于地球 N 为 0 且 i 为 0 的情况,所以我的 if else 语句的意图是它应该被忽略并且 N = N + 1 并且循环继续,但我认为这里可能存在潜在错误,我就是看不到。
标签: python list vector model physics