如何在代码中修复“ 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标量,因此无法编制索引。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...