-
Notifications
You must be signed in to change notification settings - Fork 1
/
3-Body-Problem.py
321 lines (192 loc) · 7.35 KB
/
3-Body-Problem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import numpy as np
from numpy.linalg import norm
from numpy.fft import rfft, rfftfreq
import matplotlib.pyplot as plt
import matplotlib.animation as an
import math
#------------------------- PHYSICAL PARAMETERS -------------------------
# Define force of Gravity
def F(M,m,pos1,pos2):
r = norm(pos2-pos1)
return #input the correct function
#------------------------- PHYSICAL PARAMETERS -------------------------
G = 6.7e-11 # Universal gravitational constant
Msun = 1.989e30 # Mass of sun (kg)
Me = 5.972e24 # Mass of Earth (kg)
Mm = 7.348e22 # Mass of Moon (kg)
esd = 1.496e11 # Earth-Sun distance (m)
emd = 3.844e8 # Earth-Moon distance (m)
Ve = 2.93e4 # Magnetude of velocity of Earth around the Sun (m/s)
Vm = 1.02e3 # Magnetude of velocity of Moon around the Earth (m/s)
dt = 100. # Time step (s)
t_end = 3.154e7 #one year in seconds
t = np.linspace(0.,t_end,t_end/dt)
#Create Arrays to Store the Position of Each Starts Respectively
Xsun = np.zeros(len(t))
Ysun = np.zeros (len(t))
Xearth = np.zeros(len(t))
Yearth = np.zeros(len(t))
Xmoon = np.zeros(len(t))
Ymoon = np.zeros(len(t))
#Initial Position of Earth / Sun
Xsun [0] = 0
Ysun [0] = 0
Xearth[0] = esd
Yearth[0] = 0
Xmoon[0] = esd
Ymoon[0] = emd
#Create Arrays to Store the Velocity of Each Starts Respectively
VxSun = np.zeros (len(t))
VySun = np.zeros (len(t))
Vxearth = np.zeros(len(t))
Vyearth = np.zeros(len(t))
Vxmoon = np.zeros(len(t))
Vymoon = np.zeros(len(t))
#Initial Velocity of Earth / Sun
Vxearth [0] = 0
Vyearth [0] = Ve
Vxmoon[0] = -Vm
Vymoon[0] = Ve
#VySun [0] = -Vyearth [0] * Me /Msun
VxSun [0] = (-(Vxearth [0] * Me) - (Mm * -Vm)) /Msun
VySun [0] = (-(Vyearth [0] * Me) - (Mm * Vymoon [0])) /Msun
#Force Functions (x,y)
def forcefuncx(x1,x2,y1,y2,m1,m2):
return -(G*m1*m2*(x2-x1))/ (math.sqrt ((x2-x1)**2 + (y2-y1)**2))**3
def forcefuncy (x1,x2,y1,y2,m1,m2):
return -(G*m1*m2*(y2-y1))/ (math.sqrt ((x2-x1)**2 + (y2-y1)**2))**3
#calculate distance between two planets
#calculate the force
## TUESDAY AM: create a plot of the gravitational force as a function of distance
## TUESDAY PM: solve the ODE
for i in range (0,len(t)-1):
#Update:Velocity of the Earth (Affected by both the Moon and the Sun)
Vxearth[i+1] = Vxearth[i] + (forcefuncx(Xsun[i], Xearth[i], Ysun[i], Yearth[i], Msun, Me) + forcefuncx(Xmoon[i], Xearth[i], Ymoon[i], Yearth[i], Mm, Me)) /Me*dt
Vyearth[i+1] = Vyearth[i] + (forcefuncy(Xsun[i], Xearth[i], Ysun[i], Yearth[i], Msun, Me) + forcefuncy(Xmoon[i], Xearth[i], Ymoon[i], Yearth[i], Mm, Me)) /Me*dt
#Update:Velocity of the Sun (Affected by both the Earth and the Moon)
VxSun[i+1] = VxSun[i] + (forcefuncx(Xearth[i], Xsun[i], Yearth[i], Ysun[i], Me, Msun) + forcefuncx(Xmoon[i], Xsun[i], Ymoon[i], Ysun[i], Msun, Me)) /Msun*dt
VySun[i+1] = VySun[i] + (forcefuncy(Xearth[i], Xsun[i], Yearth[i], Ysun[i], Me, Msun) + forcefuncy(Xmoon[i], Xsun[i], Ymoon[i], Ysun[i], Msun, Me)) /Msun*dt
#Update: Velocity of the Moon (Affected by both the Earth and the Sun)
Vxmoon[i+1] = Vxmoon[i] + (forcefuncx(Xsun[i], Xmoon[i], Ysun[i], Ymoon[i], Msun, Mm) + forcefuncx(Xearth[i], Xmoon[i], Yearth[i], Ymoon[i], Me, Mm)) /Mm*dt
Vymoon[i+1] = Vymoon[i] + (forcefuncy(Xsun[i], Xmoon[i], Ysun[i], Ymoon[i], Msun, Mm) + forcefuncy(Xearth[i], Xmoon[i], Yearth[i], Ymoon[i], Me, Mm)) /Mm*dt
#Update:Position of the Earth
Xearth[i+1]= Xearth[i] + Vxearth[i+1] *dt
Yearth[i+1]= Yearth[i] + Vyearth[i+1] *dt
#Update:Position of the Sun
Xsun[i+1]= Xsun[i] + VxSun[i+1] *dt
Ysun[i+1]= Ysun[i] + VySun[i+1] *dt
#Update:Position of the Moon
Xmoon[i+1] = Xmoon[i] + Vxmoon[i+1] *dt
Ymoon[i+1] = Ymoon[i] + Vymoon[i+1] *dt
f = rfftfreq(len(t),3.171e-6)
XFearth = rfft(Xearth)
XFmoon = rfft(Xmoon)
XFsun = rfft(Xsun)
#Total Energy of the System = Potential Energy + Kinetic Energy
#Relative Distance Between the Earth, the Sun, and the Moon
rms = np.sqrt((Xmoon - Xsun)**2 + (Ymoon - Ysun)**2)
rem = np.sqrt((Xmoon - Xearth)**2 + (Ymoon - Yearth)**2)
res = np.sqrt((Xearth - Xsun)**2 + (Yearth - Ysun)**2)
#Magnitude of the Velocity of the Earth
MagE = np.sqrt(Vxearth**2 + Vyearth**2)
#Magnitud of the Velocity of the Sun
MagS = np.sqrt(VxSun**2 + VySun**2)
#Magnitude of the Velocity of the Moon
MagM= np.sqrt(Vxmoon**2 + Vymoon**2)
#Potential Energy of the Earth & Sun
Pes = -(G*Msun*Me /res)
#Potential Energy of the Earth & Moon
Pem = -(G*Me*Mm / rem)
#Potential Energy of the Moon & Sun
Pms = -(G*Msun*Mm / rms )
#Kinetic Energy of the Earth
Ke = (0.5*Me*MagE**2)
#Kinetic Energy of the Sun
Ks = (0.5*Msun*MagS**2)
#Kinetic Energy of the Moon
Km = (0.5*Mm*MagM**2)
#Total Energy of the System
Te = Pes + Pem + Pms + Ke + Ks + Km
## WEDNESDAY: calculate the frequency of the orbit using Fourier analysis
## THURSDAY
fig = plt.figure()
plt.title ('Orbits of the Earth, the Sun, and the Moon')
plt.plot(Xearth, Yearth,)
plt.plot(Xmoon,Ymoon)
plt.plot(Xsun,Ysun,)
fig2 = plt.figure()
plt.title ('Total Energy Over Time')
plt.plot(t,Te)
plt.title('XEarth V.S Time')
plt.plot(t,Xearth)
fig4 = plt.figure()
plt.title('XMoon V.S Time')
plt.plot(t,Xmoon)
fig6 = plt.figure()
axf6 = fig6.add_subplot(131)
plt.title ('Fourier Transform for the Freq. of the Earth Orbiting Around the Sun')
plt.plot(f,np.abs(XFearth))
plt.xlim((0,30))
ax2f6 = fig6.add_subplot(132)
plt.title('FT for the Moon Orbiting Around the Sun')
plt.plot(f,np.abs(XFmoon))
plt.xlim((0,30))
ax3f6 = fig6.add_subplot(133)
plt.title('FT for the Sun')
plt.plot(f,np.abs(XFsun))
plt.xlim((0,30))
fig7 = plt.figure()
plt.title('XSun V.S Time')
plt.plot(t,Xsun)
plt.show()
###ax1 = fig.add_subplot(131)
###plt.title ('Position of the Earth')
##plt.plot(Xearth, Yearth)
##
###ax2 = fig.add_subplot(132)
###plt.title ('Position of the Sun')
##plt.plot(Xsun, Ysun)
##
###ax3 = fig.add_subplot(133)
##plt.title ('Position of the Moon')
##plt.plot(Xmoon, Ymoon)
##
##fig2 = plt.figure()
# code to animate plot
# create figure
#fig = plt.figure()
# define axes
#ax = plt.axes(xlim=(0.,t_end),ylim=(0.,t_end))
#line1, = ax.plot([],[],lw=2)
# function to initialize lines for animation
##def init():
## line1.set_data()
## return line1,
##
##def animate_single(i):
## line1.set_data()
## return line1,
##
###animate and plot
##num_frames = 1000
##anim = an.FuncAnimation(fig, animate_single, init_func=init, blit=True, interval=1, frames=num_frames)
##for i in range (0, len(t)- 1):
##
## rms[i] = (math.sqrt((Xmoon[i]- Xsun[i])**2) + (Ymoon[i] - Ysun[i])**2)
## rem[i] = (math.sqrt((Xmoon[i] - Xearth[i])**2 + (Ymoon[i] - Yearth[i])**2)
## res[i] = (math.sqrt((Xearth[i] - Xsun[i])**2 + (Yearth[i] - Ysun[i])**2)
##
#print Te.size
## if i%10==0:
## plt.clf()
## plt.plot(Xsun[i], Ysun[i],".")
##
## #ax3 = fig.add_subplot(133)
## plt.title ('Position of the Moon')
## plt.plot(Xmoon[i], Ymoon[i],".")
## plt.xlim(-esd, esd)
## plt.ylim(-esd, esd)
## plt.show(block=False)
## plt.pause(0.01)
#ax1 = fig.add_subplot(131)
#ax2 = fig.add_subplot(132)