gpt4 book ai didi

python - IndexError : list assignment index out of range. 如何解决这个问题?

转载 作者:行者123 更新时间:2023-12-01 07:14:38 25 4
gpt4 key购买 nike

我正在尝试纽马克恒定平均加速度法。我收到这个错误。如何从该错误中恢复?

IndexError Traceback (most recent call last)

41 for i in range(len(t)):

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 (1-y/(2*b))*a[i]

IndexError: list assignment index out of range

y = 1/2
b = 1/4

u = []
v = []

t = []
p = [0,25,43.3013,50,43.3013,25,0,0,0,0,0,0]

a = []

pn = []
pn.append(0)



x = 0.0
for i in range(11):
z = 0.0 + x
t.append(z)
x = x + 0.1

m = 0.45594
k = 18
c = 0.2865

u.append(0)
v.append(0)


a.append((p[0]-c*v[0]-k*u[0])/m)

dt = 0.1

a1 =(m/(b*dt*dt)+y*c/(b*dt))
a2 = (m/(b*dt)+(y/b-1)*c)
a3 = (((1/(2*b))-1)*m + dt*((y/(2*b))-1)*c)
kn = k + a1


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]

最佳答案

你的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]

关于python - IndexError : list assignment index out of range. 如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58037258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com