前提
Pythonで3次元のグラフを作成したのですが、アニメーション化させたいです。
ソースコードのどこを変更または追加すれば良いのかわからなくて困っています。
Pythonを始めたばかりなので、有識者の方に教えていただきたいです。
よろしくお願い致します。
該当のソースコード
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(r1com_sol[:,0],r1com_sol[:,1],r1com_sol[:,2],color="darkblue") 69ax.plot(r2com_sol[:,0],r2com_sol[:,1],r2com_sol[:,2],color="tab:red") 70#plt.savefig("TwoBodyEquation2.pdf") 71ax.scatter(r1com_sol[-1,0],r1com_sol[-1,1],r1com_sol[-1,2],color="darkblue",marker="o",s=30) 72ax.scatter(r2com_sol[-1,0],r2com_sol[-1,1],r2com_sol[-1,2],color="tab:red",marker="o",s=30) 73#さらにいくつかのベルとホイッスルを追加する 74ax.set_xlabel("x-coordinate",fontsize=5) 75ax.set_ylabel("y-coordinate",fontsize=5) 76ax.set_zlabel("z-coordinate",fontsize=5)```
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/07/25 13:57
2022/07/25 15:52
2022/07/26 13:09