I am trying to do this homework exercise: Orbit of the Earth
My plot does not show the whole trajectory. I don’t know if it is something wrong with my equations or if it is a plotting matter.
Cheers!
JavaScript
x
53
53
1
import matplotlib.pyplot as plt
2
import numpy as np
3
import scipy.integrate as spi
4
5
G = 6.6738*10**-11
6
M = 1.9891*10**30
7
h = 3600
8
y = 1.4710*10**11
9
vx = 3.0287*20**4
10
11
def LeapFrog(f, t_start, t_stop, z0, h):
12
13
t_vec = np.arange(t_start, t_stop, h)
14
n = len(t_vec)
15
d = len(z0)
16
z_vec = np.zeros((n, d))
17
z_vec[0,:] = z0
18
z_half_step=z_vec[0 , :] + (1/2)*h*f(z0,t_vec[0])
19
20
21
for i in range(0, n - 1):
22
z_vec[i+1,:]=z_vec[i,:] + h*f(z_half_step, t_vec[i])
23
z_half_step += h*f(z_vec[i+1,:], t_vec[i])
24
25
return t_vec, z_vec,
26
27
28
def f(z,t):
29
30
x=z[0]
31
y=z[1]
32
vx=z[2]
33
vy=z[3]
34
r=np.sqrt(x**2+y**2)
35
36
dz=np.zeros(4)
37
38
dz[0]=vx
39
dz[1]=vy
40
dz[2]=-G*M*x/r**3
41
dz[3]=-G*M*y/r**3
42
43
return dz
44
45
t_start = 0
46
t_stop = 24*365*5
47
z0 = np.array([0,y,vx,0])
48
t_vec, z_vec = LeapFrog(f, t_start, t_stop, z0, h)
49
50
plt.plot(z_vec[:,0],z_vec[:,1], 'g', markersize=1, label='Earth trajectory')
51
plt.plot(0,0,'yo', label = 'Sun positon')
52
plt.show()
53
Advertisement
Answer
Okay, so there’s a pretty funny typo:
JavaScript
1
2
1
vx = 3.0287*20**4
2
That’s 16 times larger than what you most likely wanted to write and just 20% short of the solar escape velocity.
t_stop
is also written as if in hours, but the rest of the code assumes normal SI seconds (as can be verified by using t_stop = 24 * 365 * 0.99 * h
, which results in almost a complete orbit). It needs multiplying by h
. Sidenote: I like using astropy.units
to keep track of physical quantities!
The code itself works nicely, though!