IndexError:索引10000超出轴0的范围,大小为10000

时间:2015-12-16 15:49:21

标签: python

对于我的物理学位,我必须学习一些Python课程。我是一个绝对的初学者,因此我无法理解其他答案(不幸的是,我真的很忙:/)。代码是用空气阻力绘制物体的轨迹。我真的很感激快速修复 - 我认为它与时间变量太小有关但增加它并没有帮助。

import matplotlib.pyplot as plt
import numpy as np
import math # need math module for trigonometric functions

g = 9.81 #gravitational constant
dt = 1e-3 #integration time step (delta t)
v0 = 40 # initial speed at t = 0

angle = math.pi/4 #math.pi = 3.14, launch angle in radians

time = np.arange(0, 10, dt) #time axis
vx0 = math.cos(angle)*v0 # starting velocity along x axis
vy0 = math.sin(angle)*v0 # starting velocity along y axis

xa = vx0*time # compute x coordinates
ya = -0.5*g*time**2 + vy0*time # compute y coordinates

def traj_fric(angle, v0): # function for trajectory

    vx0 = math.cos(angle) * v0 # for some launch angle and starting velocity
    vy0 = math.sin(angle) * v0 # compute x and y component of starting velocity

    x = np.zeros(len(time))   #initialise x and y arrays
    y = np.zeros(len(time))

    x[0], y[0], 0 #projecitle starts at 0,0
    x[1], y[1] = x[0] + vx0 * dt, y[0] + vy0 * dt # second elements of x and
                                              # y are determined by initial 
                                              # velocity
    i = 1
    while y[i] >= 0: # conditional loop continuous until
    # projectile hits ground
        gamma = 0.005 # constant of friction
        height = 100 # height at which air friction disappears
        f = 0.5 * gamma * (height - y[i]) * dt
        x[i + 1] = (2 * x[i] - x[i - 1] + f * x[i - 1])/1 + f # numerical integration to find x[i + 1]                                       
        y[i + 1] = (2 * y[i] - y[i - 1] + f * y[i - 1] - g * dt ** 2)/ 1 + f # and y[i + 1]

        i = i + 1 # increment i for next loop

    x = x[0:i+1] # truncate x and y arrays                                                
    y = y[0:i+1]
    return x, y, (dt*i), x[i] # return x, y, flight time, range of projectile

x, y, duration, distance = traj_fric(angle, v0)

fig1 = plt.figure()
plt.plot(xa, ya) # plot y versus x
plt.xlabel ("x")
plt.ylabel ("y")
plt.ylim(0, max(ya)+max(ya)*0.2)
plt.xlim(0, distance+distance*0.1)
plt.show()

print "Distance:" ,distance
print "Duration:" ,duration

n = 5
angles = np.linspace(0, math.pi/2, n)
maxrange = np.zeros(n)

for i in range(n):
    x,y, duration, maxrange [i] = traj_fric(angles[i], v0)

angles = angles/2/math.pi*360 #convert rad to degress

print "Optimum angle:", angles[np.where(maxrange==np.max(maxrange))]

错误是:

  File "C:/Python27/Lib/site-packages/xy/projectile_fric.py", line 43, in traj_fric
x[i + 1] = (2 * x[i] - x[i - 1] + f * x[i - 1])/1 + f # numerical integration to find x[i + 1]

IndexError: index 10000 is out of bounds for axis 0 with size 10000

3 个答案:

答案 0 :(得分:6)

这非常简单。当您的大小为10000时,元素索引10000超出范围,因为索引从0开始,而不是1。因此,第10,000个元素是索引9999,任何大于该元素的元素都超出范围。

答案 1 :(得分:2)

Mason Wheeler的回答告诉了你Python告诉你的内容。问题出现在这个循环中:

while y[i] >= 0: # conditional loop continuous until
# projectile hits ground
    gamma = 0.005 # constant of friction
    height = 100 # height at which air friction disappears
    f = 0.5 * gamma * (height - y[i]) * dt
    x[i + 1] = (2 * x[i] - x[i - 1] + f * x[i - 1])/1 + f # numerical integration to find x[i + 1]                                       
    y[i + 1] = (2 * y[i] - y[i - 1] + f * y[i - 1] - g * dt ** 2)/ 1 + f # and y[i + 1]

    i = i + 1 # increment i for next loop

简单的解决方法是将循环更改为(我不知道Python语法,所以请耐心等待):

while (y[i] >= 0) and (i < len(time)):

当你的阵列耗尽时,这将停止使用sim卡,但它会(可能)在空中悬挂射弹的情况下停止使用sim卡。

这里有一个非常简单的弹道弹丸模拟,将大气摩擦模拟为高度的线性函数。定性地说,发生的事情是你的射弹在你允许的时间内没有击中地面,并且你试图超越你的跟踪阵列。这是由于未能允许足够的飞行时间造成的。观察到当大气摩擦为零时发生最大可能的飞行时间,然后计算飞行时间的封闭形式上限是微不足道的。然后使用该上限作为您的时间,您将分配足够的阵列空间来模拟射弹以一直影响。

答案 2 :(得分:0)

enter code heredef data_to_array(total):
random.shuffle(total)
X = np.zeros((len(total_train), 224, 224, 3)).astype('float')
y = []
for i, img_path in enumerate(total):
    img = cv2.imread('/content/gdrive/My Drive/PP/Training/COVID/COVID-19 (538).jpg')
    img = cv2.resize(img, (224, 224))
    X[i] = img - 1
    if len(re.findall('covid', '/content/gdrive/My Drive/PP/Training/COVID/COVID-19 (538).jpg')) == 3:
        y.append(0)
    else:
        y.append(1)
y = np.array(y)
return X, y

X_train,y_train = data_to_array(total_train) X_test,y_test = data_to_array(total_val)