実現したいこと
matplotlibで3次元のグラフを作成しました。
ソースコードでは、4つの点の軌道が描かれており、4つの点が時刻tの時のx座標、y座標、z座標をprintで出力させたいです。
時刻tに関しては始点から終点までの時間であり、始点から終点までのx座標、y座標、z座標をprintで出力させる方法を教えていただきたいです。
例えば、
r1_sol
t=1, x=0, y=0, z=0
t=2, x=1, y=1, z=1
...
r2_sol
t=1, x=0, y=0, z=0
t=2, x=1, y=1, z=1
...
r3_sol
t=1, x=0, y=0, z=0
t=2, x=1, y=1, z=1
...
r4_sol
t=1, x=0, y=0, z=0
t=2, x=1, y=1, z=1
...
のような感じです。
該当のソースコード
Python
1%matplotlib notebook 2 3import numpy as np 4import matplotlib.pyplot as plt 5from mpl_toolkits.mplot3d import Axes3D 6import matplotlib.animation as animation 7 8#万有引力定数を定義 9G=6.67e-11 #N-m2/kg2 10#参照数量 11m_nd=1.989e+30 #kg #太陽の質量 12r_nd=5.326e+12 #m #アルファケンタウリの星間距離 13v_nd=30000 #m/s #太陽の周りの地球の相対速度 14t_nd=79.91*365*24*3600*0.51 #s #アルファケンタウリの軌道期間 15#正味定数 16K1=G*t_nd*m_nd/(r_nd**2*v_nd) 17K2=v_nd*t_nd/r_nd 18 19#質量の定義 20m1=1 #質量が太陽の5倍の質点 21m2=1 #質量が太陽の質点 22#位置の初期条件 23r1=[-0.5,0,0] #m 24r2=[0.5,0,0] #m 25r1=np.array(r1,dtype="float64") 26r2=np.array(r2,dtype="float64") 27r_com=(m1*r1+m2*r2)/(m1+m2) 28#速度の初期条件 29v1=[0.01,0.01,0] #m/s 30v2=[-0.05,0,-0.1] #m/s 31v1=np.array(v1,dtype="float64") 32v2=np.array(v2,dtype="float64") 33v_com=(m1*v1+m2*v2)/(m1+m2) 34 35#運動方程式を定義 36def TwoBodyEquations(w,t,G,m1,m2): 37 r1=w[:3] 38 r2=w[3:6] 39 v1=w[6:9] 40 v2=w[9:12] 41 r=np.linalg.norm(r2-r1) 42 dv1bydt=K1*m2*(r2-r1)/r**3 43 dv2bydt=K1*m1*(r1-r2)/r**3 44 dr1bydt=K2*v1 45 dr2bydt=K2*v2 46 r_derivs=np.concatenate((dr1bydt,dr2bydt)) 47 derivs=np.concatenate((r_derivs,dv1bydt,dv2bydt)) 48 return derivs 49 50#パッケージの初期パラメータ 51init_params=np.array([r1,r2,v1,v2]) #create array of initial params 52init_params=init_params.flatten() #flatten array to make it 1D 53time_span=np.linspace(0,8,500) #8 orbital periods and 500 points 54#odeを実行 55from scipy.integrate import odeint 56two_body_sol=odeint(TwoBodyEquations,init_params,time_span,args=(G,m1,m2)) 57 58r1_sol=two_body_sol[:,:3] 59r2_sol=two_body_sol[:,3:6] 60 61rcom_sol=(m1*r1_sol+m2*r2_sol)/(m1+m2) 62r1com_sol=r1_sol-rcom_sol 63r2com_sol=r2_sol-rcom_sol 64 65#図を作成 66fig=plt.figure(figsize=(5,5)) 67ax=fig.add_subplot(111,projection="3d") 68ax.plot(r1_sol[:,0],r1_sol[:,1],r1_sol[:,2],color="blue") 69ax.plot(r2_sol[:,0],r2_sol[:,1],r2_sol[:,2],color="red") 70ax.plot(r3_sol[:,0],r3_sol[:,1],r3_sol[:,2],color="green") 71ax.plot(r4_sol[:,0],r4_sol[:,1],r4_sol[:,2],color="orange") 72plt.savefig("FourBodyEquation10.pdf") 73#さらにいくつかのベルとホイッスルを追加する 74ax.set_xlabel("x-coordinate",fontsize=5) 75ax.set_ylabel("y-coordinate",fontsize=5) 76ax.set_zlabel("z-coordinate",fontsize=5) 77 78steps = r1_sol.shape[0] 79artists = [ 80 [ax.scatter(*np.c_[r1_sol[i], r2_sol[i], r3_sol[i], r4_sol[i]], 81 color=["blue", "red","green","orange"], marker="o", s=30)] 82 for i in range(0, steps, 2) 83] 84ani = animation.ArtistAnimation(fig, artists, interval=50) 85ani.save('FourBodyEquations2.gif', writer='imagemagick')`` 86 87
回答1件
あなたの回答
tips
プレビュー