尝试使用matplotlib创建太阳系时出现问题

时间:2018-12-04 00:07:56

标签: animation matplotlib

我真的是Python编程的新手,在我的最终项目中,我需要在Jupyter上创建此Solar System动画。该项目的实质是基于Cleve Moler撰写的MATLAB实验第17章。实际上,我从https://www.mathworks.com/matlabcentral/fileexchange/37977-experiments-with-matlab?focused=5980069&tab=function

收集了所有数据

我不明白我得到的错误,我不确定这是因为我的代码错误还是因为我使用的数字很小。

如果我不能正确表达自己的意思,但是英语不是我的母语,我将不胜感激。

%matplotlib notebook
from math import *
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import matplotlib.animation as animation

fig = plt.figure()
ax = plt.axes(projection='3d')

t = np.linspace(0.0,100.0,101)

sol_p= np.array([0,0,0])
sol_v= np.array([0,0,0])
sol_m= np.array(1.9891e+30)
sol= np.array([sol_p, sol_v, sol_m])

mer_p= np.array([-1.02050180e-2,  3.07938393e-1,  2.60947941e-2])
mer_v= np.array([-3.37623365e-2,  9.23226497e-5,  3.10568978e-3])
mer_m= np.array(3.302e+23)
mer= np.array([mer_p, mer_v, mer_m])

ven_p= np.array([-6.29244070e-1,  3.44860019e-1,  4.10363705e-2])
ven_v= np.array([-9.80593982e-3, -1.78349270e-2,  3.21808697e-4])
ven_m= np.array(4.8685e+24)
ven= np.array([ven_p, ven_v, ven_m])

tie_p= np.array([5.28609710e-1, -8.67456608e-1,  1.28811732e-5])
tie_v= np.array([1.44124476e-2,  8.88154404e-3, -6.00575229e-7])
tie_m= np.array(5.9736e+24)
tie= np.array([tie_p, tie_v, tie_m])

mar_p= np.array([-1.62489742e+0, -2.24489575e-1,  3.52032835e-2])
mar_v= np.array([2.43693131e-3, -1.26669231e-2, -3.25240784e-4])
mar_m= np.array(6.4185e+23)
mar= np.array([mar_p, mar_v, mar_m])

jup_p= np.array([1.64800250e+0, -4.90287752e+0, -1.65248109e-2])
jup_v= np.array([7.06576969e-3,  2.76492888e-3, -1.69566833e-4])
jup_m= np.array(1.8986e+27)
jup= np.array([jup_p, jup_v, jup_m])

sat_p= np.array([-8.77327303e+0,  3.13579422e+0,  2.94573194e-1])
sat_v= np.array([-2.17081741e-3, -5.26328586e-3,  1.77789483e-4])
sat_m= np.array(5.6846e+26)
sat= np.array([sat_p, sat_v, sat_m])

ura_p= np.array([1.97907257e+1, -3.48999512e+0, -2.69289277e-1])
ura_v= np.array([6.59740515e-4,  3.69157117e-3,  5.11221503e-6])
ura_m= np.array(8.6832e+25)
ura= np.array([ura_p, ura_v, ura_m])

nep_p= np.array([2.38591173e+1, -1.82478542e+1, -1.74095745e-1])
nep_v= np.array([1.89195404e-3,  2.51313400e-3, -9.54022068e-5])
nep_m= np.array(1.0243e+26)
nep= np.array([nep_p, nep_v, nep_m])

cuerpos= np.array([sol, mer, ven, tie, mar, jup, sat, ura, nep])

P=np.array([sol_p, mer_p, ven_p, tie_p, mar_p, jup_p, sat_p, ura_p, nep_p])
V=np.array([sol_v, mer_v, ven_v, tie_v, mar_v, jup_v, sat_v, ura_v, nep_v])
M=np.array([sol_m, mer_m, ven_m, tie_m, mar_m, jup_m, sat_m, ura_m, nep_m])

M= M/sol_m

V= V*365.25/(2*np.pi)

def G(x):
    g=6.674 * 10**(-11)
    S=0
    for k in cuerpos:
        if k==x:
            S+=0
        else:
            S+= (k[2] * (k[0]-x[0])) / (((k[0][0] - x[0][0])**2 + (k[0][1] 
x[0][1])**2 + (k[0][2] - x[0][2])**2)**1.5)
    return np.array(g*S)

GP= np.array([G(sol), G(mer), G(ven), G(tie), G(mar), G(jup), G(sat), 
G(ura), G(nep)])


def frame(w):
    ax.clear()

    a= [x[0] for x in P]
    b= [x[1] for x in P]
    c= [x[2] for x in P]


    mensaje="Sistema Solar"
    plt.title(mensaje)
    ax.set_xlim3d(-50.0,50.0)
    ax.set_ylim3d(-50.0,50.0)
    ax.set_zlim3d(-50.0,50.0) 

    plot=ax.scatter3D(a, b, c, c='r')

    V+=GP
    P+=V

    return plot


anim = animation.FuncAnimation(fig, frame, frames=len(t), blit=False)
plt.show()

0 个答案:

没有答案