@zhuchunqin
2016-12-04T16:02:19.000000Z
字数 4443
阅读 200
import matplotlib.pyplot as plt
import numpy as np
from pylab import *
import math
class hyperion:
def init(self,GM=4*math.pi**2,dt=0.0001,time=10):
self.GM=GM
self.e=e
self.x1=[1.05]
self.y1=[0]
self.vx1=[0]
self.vy1=6
self.x2=[1.05]
self.y2=[0]
self.vx2=[0]
self.vy2=6
self.dt=dt
self.time=time
self.r1=[math.sqrt(self.x1[0]**2+self.y1[0]**2)]
self.r2=[math.sqrt(self.x2[0]**2+self.y2[0]**2)]
self.t=[0]
self.w1=[0]
self.theta1=[0]
self.w2=[0]
self.theta2=[0.01]
self.Deltatheta=[0.01]
self.L=[0]
def calculate(self):
for i in range(int(self.time//self.dt)):
self.vx1.append(self.vx1[i]-self.GM*self.x1[i]*self.dt/self.r1[i]**3)
self.vy1.append(self.vy1[i]-self.GM*self.y1[i]*self.dt/self.r1[i]**3)
self.x1.append(self.x1[i]+self.vx1[i+1]*self.dt)
self.y1.append(self.y1[i]+self.vy1[i+1]*self.dt)
self.r1.append(math.sqrt(self.x1[i+1]**2+self.y1[i+1]**2))
self.w1.append(self.w1[i]-3*self.GM/self.r1[i]**5*(self.x1[i]*math.sin(self.theta1[i])-self.y1[i]math.cos(self.theta1[i]))(self.x1[i]*math.cos(self.theta1[i])+self.y1[i]*math.sin(self.theta1[i]))*self.dt)
self.theta1.append(self.theta1[i]+self.w1[i+1]*self.dt)
self.vx2.append(self.vx2[i]-self.GM*self.x2[i]*self.dt/self.r2[i]**3)
self.vy2.append(self.vy2[i]-self.GM*self.y2[i]*self.dt/self.r2[i]**3)
self.x2.append(self.x2[i]+self.vx2[i+1]*self.dt)
self.y2.append(self.y2[i]+self.vy2[i+1]*self.dt)
self.r2.append(math.sqrt(self.x2[i+1]**2+self.y2[i+1]**2))
self.w2.append(self.w2[i]-3*self.GM/self.r2[i]**5*(self.x2[i]*math.sin(self.theta2[i])-self.y2[i]math.cos(self.theta2[i]))(self.x2[i]*math.cos(self.theta2[i])+self.y2[i]*math.sin(self.theta2[i]))*self.dt)
self.theta2.append(self.theta2[i]+self.w2[i+1]*self.dt)
self.Deltatheta.append(abs(self.theta2[i])-abs(self.theta1[i]))
self.L.append(abs(math.log(abs(self.Deltatheta[i]))))
self.t.append(self.t[i]+self.dt)
if self.theta1[i+1]<-math.pi:
self.theta1[i+1]=self.theta1[i+1]+2*math.pi
if self.theta1[i+1]>math.pi:
self.theta1[i+1]=self.theta1[i+1]-2*math.pi
if self.theta2[i+1]<-math.pi:
self.theta2[i+1]=self.theta2[i+1]+2*math.pi
if self.theta2[i+1]>math.pi:
self.theta2[i+1]=self.theta2[i+1]-2*math.pi
def show_results(self,color):
subplot(121)
plt.plot(self.t,self.Deltatheta,'m',label=r'Elliptical orbit')
plt.title(r'Hyperion versus time',fontsize=14)
plt.xlabel(u'time(yr)',fontsize=14)
plt.ylabel(u'(radians)',fontsize=14)
plt.xticks([0,2,4,6,8,10])
plt.xlim(0,10)
plt.ylim(0.0001,10)
plt.legend(fontsize=14,loc='best')
plt.semilogy(self.Deltatheta)
subplot(122)
plt.plot(self.t,self.L,color="blue",label="log ",linewidth=2)
plt.title(r'Hyperion versus time',fontsize=14)
plt.xlabel(u'time(yr)',fontsize=14)
plt.ylabel(u'(radians)',fontsize=14)
plt.xticks([0,2,4,6,8,10])
plt.xlim(0,10)
plt.ylim(0.0001,10)
plt.legend(fontsize=14,loc='best')
plt.semilogy(self.Deltatheta)
a=hyperion()
a.calculate()
a.show_results('b')
plt.show()
if self.theta1[i+1]<-math.pi:
self.theta1[i+1]=self.theta1[i+1]+2*math.pi
if self.theta1[i+1]>math.pi:
self.theta1[i+1]=self.theta1[i+1]-2*math.pi
if self.theta2[i+1]<-math.pi:
self.theta2[i+1]=self.theta2[i+1]+2*math.pi
if self.theta2[i+1]>math.pi:
self.theta2[i+1]=self.theta2[i+1]-2*math.pi
Thanks to 2014301510065 for reference.