IndexError:列表分配索引超出范围。我该如何解决?

时间:2019-09-21 04:23:09

标签: python python-3.x math

我正在尝试Newmark的恒定平均加速方法。我收到此错误。如何从此错误中恢复?

  

IndexError Traceback(最近一次通话最后一次)

     

i在范围(len(t))中为41:

     

42 pn [i + 1] = p [i + 1] + a1 * u [i] + a2 * v [i] + a3 * a [i]

     

43 u [i + 1] = pn [i + 1] / kn

     

44 v [i + 1] = y *(u [i + 1] -u [i])/(b * dt)+(1-y / b) v [i] + dt < / em>(1-y /(2 * b))* a [i]

     

IndexError:列表分配索引超出范围

this

1 个答案:

答案 0 :(得分:1)

您的pn, a, u, v被定义为长度为1的列表,因此没有诸如pn[1]之类的索引。您可以使用append或定义所需长度的列表。

for i in range(len(t)):
    pn.append(p[i+1] + a1*u[i] + a2*v[i] + a3*a[i])
    u.append(pn[i+1]/kn)
    v.append(y*(u[i+1]-u[i])/(b*dt) + (1-y/b)*v[i] + dt* (1-y/(2*b))*a[i])
    a.append((u[i+1]-u[i])/(b*dt*dt) - v[i]/(b*dt)-(1/(2*b)-1)*a[i])

pn, a, u, v = [0]*11, [0]*11, [0]*11 [0]*11
pn[0], u[0], v[0] = 0, 0, 0
a[0] = (p[0]-c*v[0]-k*u[0])/m

...

for i in range(len(t)-1):
    pn[i+1] = p[i+1] + a1*u[i] + a2*v[i] + a3*a[i]
    u[i+1] = pn[i+1]/kn
    v[i+1] = y*(u[i+1]-u[i])/(b*dt) + (1-y/b)*v[i] + dt* (1-y/(2*b))*a[i]
    a[i+1] = (u[i+1]-u[i])/(b*dt*dt) - v[i]/(b*dt)-(1/(2*b)-1)*a[i]