从plotting orbital trajectories,我们有以下代码。这些值已更改为可用的已知IC。
如果这段代码是正确的(虽然不是这样),它会生成
运行此代码只会冻结我的电脑或输出绝对错误的图。有人可以帮我找到解决这个问题的方法吗?
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from numpy import linspace
from mpl_toolkits.mplot3d import Axes3D
mu = 398600
# r0 = [-149.6 * 10 ** 6, 0.0, 0.0] # Initial position
# v0 = [29.9652, -5.04769, 0.0] # Initial velocity
u0 = [-4069.503, 2861.786, 4483.608, -5.114, -5.691, -5]
def deriv(u, dt):
n = -mu / np.sqrt(u[0] ** 2 + u[1] ** 2 + u[2] ** 2)
return [u[3], # dotu[0] = u[3]'
u[4], # dotu[1] = u[4]'
u[5], # dotu[2] = u[5]'
u[0] * n, # dotu[3] = u[0] * n
u[1] * n, # dotu[4] = u[1] * n
u[2] * n] # dotu[5] = u[2] * n
dt = np.arange(0.0, 24 * 3600, .01) # Time to run code in seconds'
u = odeint(deriv, u0, dt)
x, y, z, x2, y2, z2 = z.T
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x, y, z)
plt.show()
答案 0 :(得分:4)
由于以下几个原因,您遇到了数字问题: 首先,您不应要求ODE求解器返回8640000点的数据。 其次,您的参数和初始条件包含大量数字,您可以通过引入适当的non-dimensional quantities来解决这些问题。
如果设置,下面的代码会产生合理的输出:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from numpy import linspace
from mpl_toolkits.mplot3d import Axes3D
u0 = [0, 0, 1, 0, -1, 0]
mu = .1
def deriv(u, dt):
n = -mu / np.sqrt(u[0] ** 2 + u[1] ** 2 + u[2] ** 2)
return [u[3], # dotu[0] = u[3]'
u[4], # dotu[1] = u[4]'
u[5], # dotu[2] = u[5]'
u[0] * n, # dotu[3] = u[0] * n
u[1] * n, # dotu[4] = u[1] * n
u[2] * n] # dotu[5] = u[2] * n
times = np.linspace(0.0, 200, 100)
u = odeint(deriv, u0, times)
x, y, z, x2, y2, z2 = u.T
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x, y, z)
plt.show()
结果为