我想写一个程序简单描述苹果通过地球的运动,或者我们称之为重力列车 ,参见(维基百科的重力训练)Gravity train by hyperphysics
我想在当时绘制移动速度和位置。
这是我制作的代码,但答案错误(速度和时间错误)。虽然我使用了下降体falling body Equations wikipedia 的方程式,加速度数值很好,但保持速度和距离的值非常大 我错了什么? 提前致谢 最大
**我必须具备的价值 时间必须等于2530.30秒(接近42.2分钟) 最高速度约为每秒7 900米(28440公里/小时)。
import math
import numpy as np
import matplotlib.pyplot as plt
# Max Oceaan 2015 10 5
# Gravity train program
# Please notice that we assume ::
# -- air friction is neglected
# -- gravitational constant = 6.67408 × 10-11
# -- Earth Mass M 5.972 × 10^24 kg
# -- the apple speed in begin is 0 (free fall)
v=0
# -- the begin time is zero
t=0
L_t=[] # list for time step
L_h=[] # list for postion (hight)
L_v=[] # list for Velocity
L_g=[] # list for acceleration
r=6371000 #Earth radius per meter
delta=100 # Length step that we walk in for_loop
for h in np.arange(0.1,2*r,delta):
# c is the correction factor for g is
# distance of apple from center of earth / earth radius
#notice:: when the apple reach the earth center c will be negativ en -1
c=float((r-h)/r)
# acceleration is according to law of Newten
g= float(6.67408 * (10**-11) * 5.972 * (10**24)*(c) )/(r**2)
# time is t=t+math.sqrt(2*(delta)/g)
# en speed is v=v+math.sqrt(2*(delta)g)
# But must g positive and if g negative ,the laws are as follows:
# t= t + math.sqrt(2*(delta)/(abs(g))) v=v - math.sqrt(2*(delta)*(abs(g)))
if g>0:
t=t+math.sqrt(2*delta/g)
v=v+math.sqrt(2*delta*g)
else:
t=t + math.sqrt(2*delta/(abs(g)))
v=v - math.sqrt(2*delta*(abs(g)))
L_h.append(h)
L_t.append(t)
L_v.append(v)
L_g.append(g)
plt.subplot(121)
plt.title("Velocity ")
plt.xlabel("Time ")
plt.ylabel("Velocity m/s")
plt.plot(L_t, L_v, 'g-')
plt.subplot(122)
plt.title("distance ")
plt.xlabel("Time ")
plt.ylabel("distance m")
plt.plot(L_t, L_h, 'b-')
plt.tight_layout()
plt.figure(2)
plt.plot(L_t, L_g , 'b-')
plt.title("acceleration in tijd")
plt.xlabel(" Time s")
plt.ylabel("acceleration (a) m/ss")
plt.show()
我试图改进它但是我在图形上得到了更糟糕的结果 新代码
# -*- coding: utf-8 -*-
import math
import numpy as np
import matplotlib.pyplot as plt
# Please notice that we assume :air friction is neglected
G = 6.67408 * (10**-11) # gravitational constant
M = 5.972 * (10**24) # Earth Mass in kg
r = 6371000 # Earth radius per meter
p = 5514 # Mean Earth density in kg\m3
time = 0 # the begin time is zero
a=0 # the begin acceleration g
v=0
t=0
L_h = [] # list for postion (hight)
L_a = [] # list for acceleration
L_v = [] # list for speed
L_t = [] # list for time
# for_loop in the path that we walk
delta=1000
for h in np.arange(1,2*r,delta):
# (M_effective) is The effective mass of earth that changes when h changes
# (M_effective) = new sphere volume * density
# M_effective = (4./3) * (math.pi) * ((r-h)**3) * p
M_effective = (4./3) * (math.pi) * p * (math.pow((r-h),3))
a = G * M_effective / (r**2) # (a) is the acceleration
if a>0:
a=a
t=t+math.sqrt(2*delta/a)
v=v+math.sqrt(2*delta*a)
else:
a=a
t=t + math.sqrt(2*delta/(abs(a)))
v=v - math.sqrt(2*delta*(abs(a)))
L_h.append(h)
L_t.append(t)
L_v.append(v)
L_a.append(a)
plt.subplot(121)
plt.title("Velocity ")
plt.xlabel("Time ")
plt.ylabel("Velocity m/s")
plt.plot(L_t, L_v, 'g-')
plt.subplot(122)
plt.title("distance ")
plt.xlabel("Time ")
plt.ylabel("distance m")
plt.plot(L_t, L_h, 'b-')
plt.tight_layout()
plt.figure(2)
plt.plot(L_t, L_a , 'b-')
plt.title("acceleration in tijd")
plt.xlabel(" Time s")
plt.ylabel("acceleration (a) m/ss")
plt.show()
#Gravity train
#The travel time equals 2530.30 seconds (nearly 42.2 minutes),
#For a train that goes directly through the center of the Earth, the maximum speed is about 7 900 metres per second (28440 km/h).