100% found this document useful (2 votes)
3K views

Python Code For Three Body Probem

This document contains code to simulate the three body problem using Newton's laws of motion. It defines functions to calculate acceleration, velocity, and position as a function of time by numerically integrating Newton's equations. Initial positions and velocities are defined for three masses. In a loop, the code calculates the acceleration on each mass from the other two at each time step, then uses this to update the velocity and position of each mass. The trajectories are plotted.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
3K views

Python Code For Three Body Probem

This document contains code to simulate the three body problem using Newton's laws of motion. It defines functions to calculate acceleration, velocity, and position as a function of time by numerically integrating Newton's equations. Initial positions and velocities are defined for three masses. In a loop, the code calculates the acceleration on each mass from the other two at each time step, then uses this to update the velocity and position of each mass. The trajectories are plotted.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

##Date: Feb 1 2013

## Programmer: Hemata Bhattarai


## Institution : Central Department of Physics , Tribhuvan University , Nepal
## e-mail: [email protected]

#three body problem simply using newtons equation of motion


#v=u+at and r=ut +.5 a t^2
import pylab as lab
n=500
prob=[1,1,1] ###[1,1,1] three body problem and [1,1,0] for two body problem note
: make v3=[0,0,0] for two body problem
m=[1,2,1]
r1=[1,0]
r2=[5,0]
#######defining initial postion and velocity of the there masses
r3=[-3,-3]
v1=[0,.5]
v2=[0,-.1]
v3=[-.5,0]
r1x=[]
r2x=[]
r3x=[]
r1y=[]
r2y=[]
r3y=[]
#################### empty list to store successive position and
velocity
v1x=[]
v2x=[]
v3x=[]
v1y=[]
v2y=[]
v3y=[]
dt=.1
r1x.append(r1[0]),v1x.append(v1[0]),v2x.append(v2[0]),v3x.append(v3[0]),
r1y.append(r1[1]),v1y.append(v1[1]),v2y.append(v2[1]),v3y.append(v3[1]),
r2x.append(r2[0])
######33 Defining initial velocity and position of three particles.
r2y.append(r2[1])
r3x.append(r3[0])
r3y.append(r3[1])
def vec(x1,y1,x2,y2):
# calculate vect
or connecting pt 1 and 2 directed towards 1
return [x1-x2,y1-y2]
def dist_s(x):
return float((x[0]*x[0]+x[1]*x[1])**(3/2.))

def accln(x1,y1,x2,y2,x3,y3,m2,m3,prob):
ation on m1 due to m2 and m3
x_2=vec(x1,y1,x2,y2)
x_3=vec(x1,y1,x3,y3)

# accler

a2x=-1*m2*x_2[0]/float(dist_s(x_2))
a2y=-1*m2*x_2[1]/float(dist_s(x_2))
a3x=-1*m3*x_3[0]/float(dist_s(x_3))
r2)/|r1-r2|^3 -m3(r1-r3)/|r1-r3|^3
a3y=-1*m3*x_3[1]/float(dist_s(x_3))
ax=prob*(a2x+a3x)
ay=prob*(a2y+a3y)
return [ax,ay]

##Newtons law a1=-m2(r1-

def velocity(vx,vy,a,t):
vxf=vx+a[0]*t
vyf=vy+a[1]*t
return[vxf,vyf]

#### vf=vi+a*t

def pos(x,y,vx,vy,a,t):
xf=x+vx*t+.5*a[0]*t*t
yf=y+vy*t+.5*a[1]*t*t
return[xf,yf]

##xf=xi+vi*t+.5*a*t*t

time=range(0,n)
for i in time:
a1=accln(r1x[i],r1y[i],r2x[i],r2y[i],r3x[i],r3y[i],m[1],m[2],prob[0])
v1=velocity(v1x[i],v1y[i],a1,dt)
v1x.append(v1[0])
v1y.append(v1[1])
pos1=pos(r1x[i],r1y[i],v1x[i],v1y[i],a1,dt)
r1x.append(pos1[0])
r1y.append(pos1[1])
################################################################################
###########################
a1=accln(r2x[i],r2y[i],r1x[i],r1y[i],r3x[i],r3y[i],m[0],m[2],prob[1])
v1=velocity(v2x[i],v2y[i],a1,dt)
v2x.append(v1[0])
v2y.append(v1[1])
pos1=pos(r2x[i],r2y[i],v2x[i],v2y[i],a1,dt)
r2x.append(pos1[0])
r2y.append(pos1[1])
################################################################################
#################################
a1=accln(r3x[i],r3y[i],r2x[i],r2y[i],r1x[i],r1y[i],m[1],m[0],prob[2])
v1=velocity(v3x[i],v3y[i],a1,dt)
v3x.append(v1[0])
v3y.append(v1[1])
pos1=pos(r3x[i],r3y[i],v3x[i],v3y[i],a1,dt)
r3x.append(pos1[0])
r3y.append(pos1[1])

lab.figure(1)
lab.plot(r1x,r1y,label="1")

lab.plot(r2x,r2y,label="2")
lab.plot(r3x,r3y,label="3")
lab.plot(r1x[0],r1y[0],'bo',r1x[-1],r1y[-1],'b*')
lab.plot(r2x[0],r2y[0],'go',r2x[-1],r2y[-1],'g*')
lab.plot(r3x[0],r3y[0],'ro',r3x[-1],r3y[-1],'r*'),lab.xlabel("x"),lab.ylabel("y"
),lab.title("Configuration space")
lab.legend()
lab.figure(2)
lab.plot(r1x,v1x,'r')
lab.xlabel("x"),lab.ylabel("v_x"),lab.title("Phase-space plot")
lab.show()

You might also like