@rfhongyi
2016-12-11T03:29:12.000000Z
字数 8992
阅读 2924





Created on Sat Nov 26 18:04:56 2016@author: RanFengimport mathimport matplotlib.pyplot as pltGM=4*(math.pi**2)perihelion=0.39*(1-0.206)class Precession:def __init__(self,e=0.206,time=1,dt=0.0001,vcoefficient=1.1,beta=2.1,alpha=0.01):self.alpha=alphaself.beta=betaself.vcoefficient=vcoefficientself.e=eself.a=perihelion/(1-e)self.x0=self.a*(1+e)#self.x0=1self.y0=0self.vx0=0self.vy0=self.vcoefficient*math.sqrt((GM*(1-self.e))/(self.a*(1+self.e)))#self.vy0=2*math.piself.X=[self.x0]self.Y=[self.y0]self.Vx=[self.vx0]self.Vy=[self.vy0]self.T=[0]self.dt=dtself.time=timeself.ThetaPrecession=[]self.TimePrecession=[]def run(self):while self.T[-1]<self.time:r=math.sqrt(self.X[-1]**2+self.Y[-1]**2)newVx=self.Vx[-1]-(GM*(1+self.alpha/r**2)*self.X[-1]/r**(1+self.beta))*self.dtnewX=self.X[-1]+newVx*self.dtnewVy=self.Vy[-1]-(GM*(1+self.alpha/r**2)*self.Y[-1]/r**(1+self.beta))*self.dtnewY=self.Y[-1]+newVy*self.dtif abs(newX*newVx+newY*newVy)<0.0014 and r<self.a:theta=math.acos(self.X[-1]/r)*180/math.piif (self.Y[-1]/r)<0:theta=360-thetatheta=abs(theta-180)self.ThetaPrecession.append(theta)self.TimePrecession.append(self.T[-1])self.Vx.append(newVx)self.Vy.append(newVy)self.X.append(newX)self.Y.append(newY)self.T.append(self.T[-1]+self.dt)def show_results(self):plt.plot(self.X,self.Y,'g')plt.legend(loc='upper right',frameon=False,fontsize='small')plt.grid(True)plt.title('Simulation of the precession of Mercury')plt.xlabel('x(AU)')plt.ylabel('y(AU)')ax = plt.gca()ax.set_aspect(1)#plt.xlim(-1,1)#plt.ylim(-0.6,0.6)#plt.scatter(0,0)A = Precession()A.run()A.show_results()
First we can have a figure to see the normal result for Earth orbiting around the Sun.
Then we can plot the figure that with elliptical oribit.
When

Also we can chose different , such as .












With different ,

It's easy to know the difference.


we can plot them together.

We can see with bigger eccentricity, the trajecory becomes more elletical, as suggested by the definition of eccentricity.





Plot them together

We chose

We chose 
from visual import *#from Numeric import *print '''Simple solar system model showing the orbits of the planetsNote: In this version the orbits are not tilted sothe longitude of the ascending node is not correct.created by Lensyl Urbano, Jan. 2006.'''class orbit:def __init__(self,e=0.0,rad=1.0, mu=10.0, planet_size=1.0, period=365,inclination=0.0, lap=0.0, frate=10000000):self.orbit_path = curve(color=color.green,radius=0.075)self.planet = sphere(color=color.yellow,radius=planet_size)if e >= 1.0:e = 0.99if e < 0.0:e = 0.0fx = 2.0 - 2.0*min(abs((1-e*e)/(1-e)), abs(-(1-e*e)/(1+e)))self.fx = fxself.e = eself.rad = radself.dangle = 360./periodself.inclination = inclinationself.lap = lapfor i in range(int(period)+1):rate(frate)theta = radians(float(360.*i/period))#h_sq = a*(1-e*e)*mur = (1-e*e)/(1-e*cos(theta))nx = rad*(r*cos(theta) - fx)ny = rad*r*sin(theta)#print i, f, r, cos(theta), sin(theta), nx, ny#nz = rotate(vector(nx,ny,0.0), angle=inclination,posi = rotate(vector(nx,ny,0.0), angle=radians(inclination), axis=(0,1,0))posi = rotate(posi, angle=radians(lap), axis=(0,0,1))self.orbit_path.append(pos=posi)self.planet.pos=posiself.angle = 0.0def re_draw(self,e=0.0,rad=1.0,mu=10.0, frate=10000000):#redraw last pathif e >= 1.0:e = 0.99if e < 0.0:e = 0.0fx = 2.0 - 2.0*min(abs((1-e*e)/(1-e)), abs(-(1-e*e)/(1+e)))self.fx = fxself.e = eself.rad = radfor i in range(361):rate(frate)theta = radians(float(i))#h_sq = a*(1-e*e)*mur = (1-e*e)/(1-e*cos(theta))nx = rad*(r*cos(theta) - fx)ny = rad*r*sin(theta)self.orbit_path.pos[i]=vector(nx, ny, 0.0)def move_planet(self):self.angle = self.angle + self.dangle#to show the planet orbiting the Suntheta = radians(float(self.angle))r = (1-self.e*self.e)/(1-self.e*cos(theta))nx = self.rad*(r*cos(theta) - self.fx)ny = self.rad*r*sin(theta)posi=rotate(vector(nx,ny,0.0), angle=radians(self.inclination), axis=(0,1,0))posi = rotate(posi, angle=radians(self.lap), axis=(0,0,1))self.planet.pos = posi#class proton:origx = 0origy = 0w = 704 #+4+4h = 576 #+24+4scene.width=wscene.height=hscene.x = origxscene.y = origymu = 10.0 #gravitational acceleration#e = 0.6 #eccentricity#a = 10.0 #distance of sun from center of elipse# to draw an elipsenucleus = frame()p1 = sphere(color=color.white, frame=nucleus)#orbitor = sphere()e1 = orbit(e=0.50, rad=15.0, mu=mu,planet_size=0.5, period=365,lap=90)##e2 = orbit(e=0.0, rad=15.0, mu=mu,## planet_size=1.0, period=365,## lap=0, inclination=90)runtime = 0.0framerate = 60pick = Nonewhile 1:rate(framerate)runtime += 1.0/frameratee1.move_planet()## e2.move_planet()

