我有一个大学项目,我们被要求使用ODE和SciPy的odeint函数模拟卫星进入火星的方法。
我设法通过将二阶ODE分成两个一阶ODE来在2D中模拟它。但是我陷入时间限制,因为我的代码使用SI单位,因此在几秒钟内运行,Python的linspace限制甚至不能模拟一个完整的轨道。
我尝试将变量和常量转换为小时和公里,但现在代码仍然存在错误。
我遵循了这个方法:
http://bulldog2.redlands.edu/facultyfolder/deweerd/tutorials/Tutorial-ODEs.pdf
代码是:
import numpy
import scipy
from scipy.integrate import odeint
def deriv_x(x,t):
return array([ x[1], -55.3E10/(x[0])**2 ]) #55.3E10 is the value for G*M in km and hours
xinit = array([0,5251]) # this is the velocity for an orbit of period 24 hours
t=linspace(0,24.0,100)
x=odeint(deriv_x, xinit, t)
def deriv_y(y,t):
return array([ y[1], -55.3E10/(y[0])**2 ])
yinit = array([20056,0]) # this is the radius for an orbit of period 24 hours
t=linspace(0,24.0,100)
y=odeint(deriv_y, yinit, t)
我不知道如何从PyLab复制/粘贴错误代码,所以我拿了一个错误的PrintScreen:
t = linspace(0.01,24.0,100)和xinit = array([0.001,5251])的第二个错误:
如果有人对如何改进代码有任何建议,我将非常感激。
非常感谢!
答案 0 :(得分:5)
odeint(deriv_x, xinit, t)
使用xinit
作为x
的初始猜测。评估x
时会使用deriv_x
的此值。
deriv_x(xinit, t)
引发零除错误,因为x[0] = xinit[0]
等于0,而deriv_x
除以x[0]
。
看起来你正试图解决二阶ODE
r'' = - C rhat
---------
|r|**2
其中rhat是径向的单位向量。
您似乎将x
和y
坐标分隔为单独的二阶ODES:
x'' = - C y'' = - C
----- and -----
x**2 y**2
初始条件x0 = 0且y0 = 20056。
这是非常有问题的。问题包括当x0 = 0
,x''
爆炸时。 r''
的原始二阶ODE没有这个问题 - x0 = 0
时分母不会爆炸,因为y0 = 20056
,因此r0 = (x**2+y**2)**(1/2)
远非零。
结论:您将r''
ODE分成x''
和y''
的两个ODE的方法不正确。
尝试搜索另一种解决r''
ODE的方法。
提示:
z = [x, y, x', y']
z'
,x
写下y
的一阶ODE,
x'
和y'
?integrate.odeint
来解决此问题吗?