gpt4 book ai didi

执行 3d 图时 python 卡住

转载 作者:太空宇宙 更新时间:2023-11-04 10:47:57 29 4
gpt4 key购买 nike

来自 plotting orbital trajectories ,我们有以下代码。这些值已更改为可以工作的已知 IC。

如果这段代码是正确的(虽然不可能),它会生成 enter image description here

运行此代码只会卡住我的计算机或输出绝对错误的图。有人可以帮我找到解决此问题的方法吗?

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()

最佳答案

您遇到数值问题的原因有以下几个:首先,您不应要求 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()

结果是result

关于执行 3d 图时 python 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16051523/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com