我正在尝试编写一个代码,该代码将绘制从高度 h 落下的球的模拟,并使用运动学方程 y = y_0 绘制随时间变化的位置图我的代码是这样的:
from matplotlib.pylab import show, xlabel, ylabel, scatter, plot from numpy import empty
def drop():
"""
This function calculates and creates arrays for the velocity at eac time interval as well as the position and plots it. Assuming no drag.
"""
#Define the constants in the problem
h_0 = 10
g = -9.8 #gravitational constant N
dt = 0.1 #timestep
#Now need to create arrays to hold the positins, time, and velocities
vel = empty(1000,float)
time = empty(1000,float)
y = empty(1000,float)
time[0] = 0
vel[0] = 0
y[0] = h_0
#code for the kinematic equations for the calculation of time, velocity and position
for i in range of (1000-1):
time[i+1] = time[i] + dt
vel[i+1] = vel[i] + (g * dt)
y[i+1] = time[i] + (vel[i+1] * dt)
if y[i] > 0:
#ensures that the graph will not keep going when the ball hits the ground
break
plot(time,y, '.')
xlabel("Time(s)")
ylabel("Position")
show()
然而,当我的图表应该看起来像一条曲线时,我的图表在图表的每个角落绘制三个点,并且我的图表每次都会发生变化,因为没有任何变量发生变化