How can i fix the error index 976 is out of bounds for axis 0 with size 976

66 views Asked by At

I get the indexerror: index 976 is out of bounds for axis 0 with size 976 with my code. Some background info for the code: It calculates the kenetic energy(= energy input - energy loss due to friction, etc) and plots this kenetic energy against the distance travelled.

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()

i expected to obtain a graph with E_k against s

1

There are 1 answers

0
phœnix On

verify if ds = 0.001 if that correct len(s)=9760 not 967. i remplace it with len(s) so the code is correct just verify ds and execute.

 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 # to verify
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(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 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()