微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在代码中修复“ IndexError:标量变量的无效索引”

如何解决如何在代码中修复“ IndexError:标量变量的无效索引”

import numpy as np

import matplotlib.pyplot as plt
M = 1.0 # Mass of discus in kg
g = 9.81 # acceleration due to gravity (m/s^2)
V = 30 # Initial veLocity in m/s
ang = 30 # Angle of initial veLocity in degrees
Cd = 0.54 # Drag coefficient
Cl = 0.87
dt = 0.01 # time step in s
t = [0]
vx = [V*np.cos(ang/180*np.pi)]
vy = [V*np.sin(ang/180*np.pi)]
x = [0]
y = [1.8]
rho = 1.2 #density in kg/m^3
a_ref = 0.025 #area in m^2
gamma = 35/180*np.pi


drag=0.5 * rho * V**2 * a_ref * Cd
lift = 0.5 * rho * V**2 * a_ref * Cl
ax = ((rho*V**2*a_ref)/(2*M))*(-Cl *np.sin(gamma) - Cd * np.cos(gamma))
ay = ((rho*V**2*a_ref)/(2*M))*(Cl* np.cos(gamma) - Cd* np.sin(gamma)) -g

counter = 0

while (y[counter] >= 0):
    t.append(t[counter]+dt)
    vx.append(vx[counter]+dt*ax[counter])
    vy.append(vy[counter]+dt*ay[counter])
    x.append(x[counter]+dt*vx[counter])
    y.append(y[counter]+dt*vy[counter])
    vel = np.sqrt(vx[counter+1]**2 + vy[counter+1]**2)
    drag = 0.5 * rho * V**2 * a_ref * Cd
    lift = 0.5 * rho * V**2 * a_ref * Cl
    ax.append(((rho*V**2*a_ref)/(2*M))*(-Cl *np.sin(gamma) - Cd * np.cos(gamma)))
    ay.append(((rho*V**2*a_ref)/(2*M))*(Cl* np.cos(gamma) - Cd* np.sin(gamma)) -g)
    counter = counter +1

plt.plot(x,y,'ro')
plt.title("Womens discus!")
plt.ylabel("y (m)")
plt.xlabel("x (m)")

我需要在“ vx.append(vx[counter]+dt*ax[counter])”行中修复此错误,但是对于如何并且一直盯着这个问题已经好几个小时了,我还是一无所知。我不允许附加我的整个代码(太长),所以我只删除了变量和一些方程式。

请告诉我是否需要添加任何内容来帮助解决此问题 任何帮助将不胜感激。 TIA

解决方法

该错误是由于您试图索引变量ax而引起的。此变量是一个numpy标量,因此无法编制索引。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。