我正在尝试创建一个程序,该程序绘制自由落体中球的速度与暴露于阻力的球的速度,使得 F_drag = -Cv^2 其中 C 是常数 (m*g)/100。我的输入是 5 代表 m,5 代表 tf,0.1 代表 dt。
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
m = float(input("Input the mass of the ball in kilograms: "))
tf = float(input("Input a specified time of fall in seconds: "))
dt = float(input("Input the time step for each calculation in seconds: "))
imaxFloat = tf/dt # The total number of time steps as a floating point number with remainder
imax = int(round(imaxFloat)) # Converts the float to the nearest integer
v0 = 0 # Velocity at t = 0
g = 9.8 # Accleration due to gravity
i = 0 # Initial counter value
i2 = 0 # Initial counter value 2
c = (m*g)/100 # Constant in drag equation
t1 = np.array([0])
v1 = np.array([v0])
t2 = np.array([0])
v2 = np.array([v0])
drag_a = ((m*g)-(c*v1*v1))/m # Acceleration of ball with drag
while i < imax:
t1 = np.append(t1, t1[i] + dt)
v1 = np.append(v1, v1[i] - g * dt )
i = i + 1
while i2 < imax:
t2 = np.append(t2, t2[i] + dt)
v2 = np.append(v2, v2[i] - drag_a * dt)
i2 = i2 + 1
plt.plot(t1, v1, label = "Neglecting air resistance")
plt.plot(t2, v2, label = "With air resistance")
Python 抛出此错误:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-6-10c7e3224e87> in <module>
30
31 while i2 < imax:
---> 32 t2 = np.append(t2, t2[i] + dt)
33 v2 = np.append(v2, v2[i] - drag_a * dt)
34 i2 = i2 + 1
IndexError: index 50 is out of bounds for axis 0 with size 1
我一般需要帮助解决这个问题,但也需要找到解决这个错误的方法。谢谢!
白猪掌柜的
慕仙森
相关分类