Wenn ich meinen Code ausführen ich den FehlerFehler mit Index. Index 0 ist außerhalb der Grenzen für Achse 0 mit Größe 0
IndexError: index 0 is out of bounds for axis 0 with size 0 on line 42 for v[0]=v0.
ich mit Python nicht vertraut bin und weiß nicht, wie dies zu debuggen.
""" Density at height y """
def rho(rh, y):
rho = rh*math.exp(-y/7249)
return rho
""" Acceleration of skydiver """
def accl(m, g, rho, v, As, Cs, Ap, Cp):
if y[i+1] >= 1200:
accl = (1/(2*m) * rho(rh, y) * v**2 * As * Cs - g)
else:
accl = (1/(2*m) * rho(rh, y) * v**2 * (As * Cs + Ap * Cp) - g)
return accl
h0 = 4000 # initial hieght
dt = 0.1 #timestep
n = int(180/dt)
#lists for displacement, time, and velocities
v= np.array([])
v[0] = v0
t= np.array([])
t[0] = t0
y= np.array([])
y[0] = h0
#calculation of time, velocity and displacement
for i in range(n):
t[i+1] = t[i] + dt
v[i+1] = v[i] + accl(m, g, rho, v[i], As, Cs, Ap, Cp) * dt
y[i+1] = y[i] + v[i+1] * dt
if y[i+1] < 0:
#ends plot once skydiver has landed
break
# Plot
plt.plot(t, y, label = 'Position m')
plt.plot(t, v, label = 'Speed m/s')
plt.plot(t, accl, label = 'Acceleration m/s^2', color = 'green')
plt.xlim([t[0], t[-1]])
plt.xlabel("Time (seconds)")
plt.legend()
Dies erzeugt einen Fehler, wenn er auf weitere Indizes von v zugreift. Er muss v mit der Größe n initialisieren. – wit221
@ wit221 Nein: 'list' benötigt keine vordefinierte Größe. – Julien
Ja, ich bezog mich auf Ihre Lösung, v = np.array ([v0]) zu verwenden. Dies führt zu einem Fehler beim Zugriff auf das Array numpy in v [i + 1] = v [i] + ... – wit221