在自由落体运动的基础上,还加入了初速度的因素。这就一般化了二维的落体运动,其中当然也包括自由落体。
想怎么抛都行,都能计算结果,并能按单位时间画出轨迹的散点图。
Python的range不能使用浮点数,所以相应代码只能设以单位时间1。
不多说了,话都在代码里。
代码如下:
import math
import time
import matplotlib.pyplot as plt
def ask_original_velocity_and_angle():
global v_orig, orig_angle
# original velocity_and_angle
v_orig = float(input("Original velocity:"))
orig_angle = math.radians(
float(input("Original angel(Degree Measure):")))
def get_move_times():
global t, total_time
method = input('''Which method do you like?
type in integer directly,
or anyword except 'w' to
type in integer or *decimal* next,
or type in 'w' to wait for a moment.
Your choice:''')
if method.isdigit():
t = float(method)
elif method == 'w':
print("nStart!")
start = time.time()
while True:
final = input("Enter to finish.")
if final == '':
break
print("Finished!")
end = time.time()
t = (end - start)
else:
t = float(input("How long:"))
total_time = int(t) + 1
def usual_movement():
global v_hor_norm, v_ver_norm, s_hor_norm, s_ver_norm
# horizontal:
v_hor_norm = v_orig * math.sin(orig_angle)
s_hor_norm = v_hor_norm * t
# vertical:
v_ver_norm = v_orig * math.cos(orig_angle)
s_ver_norm = v_ver_norm * t
def falling_body_movement():
global fall_v, fall_s
G = 9.80
fall_v = G * t
fall_s = 0.5 * G * (t ** 2)
def total_movement():
global s_hor_total, s_ver_total, speed
# velocity
v_hor_total = v_hor_norm
v_ver_total = v_ver_norm + fall_v
# distance
s_hor_total = s_hor_norm
s_ver_total = s_ver_norm + fall_s
# speed
speed = math.sqrt(
(v_hor_total ** 2)
+ (v_ver_total ** 2))
# distance(have not)
ask_original_velocity_and_angle()
get_move_times()
usual_movement()
falling_body_movement()
total_movement()
print("""ndistance_hor_total: %.3f.
distance_ver_total:%.3f.
speed:%.3f"""
% (s_hor_total, s_ver_total, speed))
print('total_time:', t)
print('''nNow draw the picture
of this movement
by unit time.''')
s_hor = []
s_ver = []
for t in range(0, total_time):
usual_movement()
falling_body_movement()
total_movement()
s_hor.append(s_hor_total)
s_ver.append(s_ver_total)
# print(s_hor, s_ver)
plt.plot(s_hor, s_ver, 'ro')
plt.gca().invert_yaxis()
plt.axis("equal")
plt.show()
选几张图形结果,分享出来: