我想寫一個代碼,將繪製一個球從高度h下降的模擬,並使用運動方程y = y_0 我的代碼是這樣的:隨着時間的推移隨着時間的推移球的位置下降的情節
從matplotlib.pylab進口展,xlabel,ylabel,散佈,情節 從numpy的進口空
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()
但是我的圖形繪製三個點之一的圖時的每個角落它應該看起來像一條曲線,我的圖形每次都會改變,因爲不是ne的變量正在變化