python - 索引错误 : index 10000 is out of bounds for axis 0 with size 10000

标签 python

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

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

最佳答案

这很简单。当您的大小为 10000 时,元素索引 10000 超出范围,因为索引以 0 而非 1 开头>。因此,第 10,000 个元素是索引 9999,任何大于它的元素都是越界的。

关于python - 索引错误 : index 10000 is out of bounds for axis 0 with size 10000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34316403/

相关文章:

python - 如何在机器学习中训练连续输出(回归)

python - 快速排序代码中的问题

python - 使用 python/ElementTree 和命名空间创建 xml 文档

Python内存消耗导致网络套接字连接崩溃

python - 如何使用 Glade 和 Gtk 创建 Treeview?

python - PyQt4 中的 Q_ENUMS

python - 在 Python 中,如何找到排序列表中第一个大于阈值的值的索引?

python - Django 模型继承问题。怎么解决?

python - 如何在 sage 中精确获取小数点后 2 位数字

python - 从数据框中删除列中的某些特定关键字并将其保存到 json