如何修复代码中‘索引976超出轴0大小976’的IndexError
解决IndexError: index 976 is out of bounds错误问题
运行代码时触发错误:IndexError: index 976 is out of bounds for axis 0 with size 976,该代码用于计算动能(输入能量减去摩擦等损耗),并绘制动能随移动距离变化的关系图。
原始代码如下:
import numpy as np import matplotlib.pyplot as plt import pandas as pd g = 9.81 m_tot = 2 m_gewichtje = 0.5 ds = 0.001 s = np.arange(0, 9.76, ds) A = 0.001 * 0.08 * 2 + 0.001 * 0.05 + 0.015 * 0.11 * 0.01* 32 *2 print(A) rho = 1.29 #kg/m3 C_f = 0.5 mu = 0.005 Fn = m_tot *g phi_max = 142 #degrtees k = 0.02052 #Nm/degree alpha = np.degrees(np.arccos((-0.5*(((0.008/0.11)**2)*(s**2))/(0.08**2))+1))+38 phi = 180 - alpha df = pd.DataFrame({'s': s, 'phi' : phi}) E_k = np.zeros(976) E_aandrijf = np.zeros(976) E_luchtweerstand = np.zeros(976) E_rolweerstand = np.zeros(976) E_lagerwrijving = np.zeros(976) F_veer = np.zeros(976) for i, afstand in enumerate(s): if phi[i] >= 0 and np.isnan(phi[i]) == False : E_veer_max = 0.5 * k * phi_max ** 2 E_veer = 0.5 * k * phi[i] ** 2 E_aandrijf[i] = E_veer_max - E_veer F_veer[i] = E_aandrijf[i]/ds else: E_aandrijf = 0 F_veer = 0 F_rol = mu * Fn F_aandrijf = F_veer[i]- F_rol v = np.sqrt(E_k[i]/(0.5*m_tot)) E_luchtweerstand[i] = 0.5 * rho * v**2 * C_f * A * ds E_rolweerstand[i] = F_rol * ds E_lagerwrijving[i] = F_aandrijf * ds E_k[i]= E_aandrijf[i] - E_lagerwrijving[i] - E_rolweerstand[i]- E_luchtweerstand[i] fig, ax = plt.subplots() ax.plot(s,E_k) plt.show()
错误原因分析
- 数组长度不匹配:
s = np.arange(0, 9.76, ds)中,ds=0.001,所以s的实际长度是9760(9.76/0.001=9760),但所有能量、力数组(如E_k、E_aandrijf)都被硬编码初始化为长度976的数组。循环遍历s的9760个元素时,当索引i达到976,访问数组元素就会触发越界错误。 - 分支赋值错误:else分支中
E_aandrijf = 0和F_veer = 0会将整个数组变量覆盖为标量0,后续循环中访问E_aandrijf[i]会引发类型错误。
修正方案
- 所有数组初始化时,使用
len(s)代替硬编码的976,保证数组长度与s一致。 - else分支中,将标量赋值改为对数组对应索引位置赋值,即
E_aandrijf[i] = 0和F_veer[i] = 0。 - 可选:初始
E_k为0,第一次计算v时是合法的(sqrt(0)),添加判断可让逻辑更严谨。
修正后的代码
import numpy as np import matplotlib.pyplot as plt import pandas as pd g = 9.81 m_tot = 2 m_gewichtje = 0.5 ds = 0.001 s = np.arange(0, 9.76, ds) A = 0.001 * 0.08 * 2 + 0.001 * 0.05 + 0.015 * 0.11 * 0.01* 32 *2 print(A) rho = 1.29 #kg/m3 C_f = 0.5 mu = 0.005 Fn = m_tot *g phi_max = 142 #degrtees k = 0.02052 #Nm/degree alpha = np.degrees(np.arccos((-0.5*(((0.008/0.11)**2)*(s**2))/(0.08**2))+1))+38 phi = 180 - alpha df = pd.DataFrame({'s': s, 'phi' : phi}) # 用len(s)初始化数组,保证长度匹配 E_k = np.zeros(len(s)) E_aandrijf = np.zeros(len(s)) E_luchtweerstand = np.zeros(len(s)) E_rolweerstand = np.zeros(len(s)) E_lagerwrijving = np.zeros(len(s)) F_veer = np.zeros(len(s)) for i, afstand in enumerate(s): if phi[i] >= 0 and not np.isnan(phi[i]) : E_veer_max = 0.5 * k * phi_max ** 2 E_veer = 0.5 * k * phi[i] ** 2 E_aandrijf[i] = E_veer_max - E_veer F_veer[i] = E_aandrijf[i]/ds else: # 改为对对应索引赋值,不覆盖整个数组 E_aandrijf[i] = 0 F_veer[i] = 0 F_rol = mu * Fn F_aandrijf_val = F_veer[i] - F_rol # 处理初始速度为0的情况 if E_k[i] <= 0: v = 0 else: v = np.sqrt(E_k[i]/(0.5*m_tot)) E_luchtweerstand[i] = 0.5 * rho * v**2 * C_f * A * ds E_rolweerstand[i] = F_rol * ds E_lagerwrijving[i] = F_aandrijf_val * ds E_k[i] = E_aandrijf[i] - E_lagerwrijving[i] - E_rolweerstand[i] - E_luchtweerstand[i] fig, ax = plt.subplots() ax.plot(s, E_k) plt.show()
内容的提问来源于stack exchange,提问作者Chris
相关产品推荐
相关产品推荐

