简体   繁体   English

IndexError:在for循环python中列出超出范围的索引

[英]IndexError: list index out of range in for loop python

I am trying to run a for loop which is below, unfortunately it throws me an error in the first time itself.我正在尝试运行下面的 for 循环,不幸的是它在第一次给我一个错误。 i managed to assigned a zero value to the variable but then have this error xest[i] = xprediction + K * y[i] - np.multiply(H, xprediction) ValueError: could not broadcast input array from shape (2,2) into shape (1,1)我设法为变量分配了一个零值,但随后出现此错误xest[i] = xprediction + K * y[i] - np.multiply(H, xprediction) ValueError: could not broadcast input array from shape (2,2) into shape (1,1)

x1 = np.zeros(len(t))
x2 = np.zeros(len(t))
K = np.zeros(len(t))
x1p_1 = np.zeros(len(t))
x2p_1 = np.zeros(len(t))
x1e = np.zeros(len(t))
x2e = np.zeros(len(t)) 
y = np.zeros(len(t))

for i in range (1,25):
    print("",i)
    if i < 10:
        u = 0.25
    else:
        u = 0.0

    x1[i] = x1[i] + (dt*x2[i]) + (dt**2/2)*u
    x2[i] = x2[i] + dt*u + math.sqrt(Q)*np.random.normal()

    y[i] = x1[i] + math.sqrt(R) * np.random.normal()

    PP = (A.dot(PE).dot(A_trans)) + (G.dot(Q).dot(G_trans))
    K = (PP.dot(H_trans)).dot(np.linalg.inv(H.dot(PP.dot(H_trans) + R)))
    ident = np.identity(2)
    PE = (ident - K.dot(H)).dot(PP)

    xprediction = A.dot(xest) + B.dot(u)
       xest[i] = xprediction + K * y[i] - np.multiply(H, xprediction)

    """This equations are basically   
    x1e(i+1) = [1 0] * xest
    x2e(i+1) = [0 1] * xest"""

    sol_1 = np.matrix('%s %s' % (1, 0))
    sol_2 = np.matrix('%s %s' % (0, 1))
    x1e[i] = np.multiply(sol_1, xest[i])
    x2e[i] = np.multiply(sol_2, xest[i])


    print("",x1[i])
  1
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-74-6ccc7c6deb33> in <module>
      6         u = 0.0
      7 
----> 8     x1[i] = x1[i] + (dt*x2[i]) + (dt**2/2)*u
      9     x2[i] = x2[i] + dt*u + math.sqrt(Q)*np.random.normal()
     10 

IndexError: list index out of range

Can somebody explain this to me, I really appreciate that.有人可以向我解释一下吗,我真的很感激。

Since the error is being thrown in the first run of the for loop, it seems like x1 and x2 have yet to be assigned values.由于错误是在 for 循环的第一次运行中抛出的,因此x1x2似乎尚未分配值。 You can fix this error by appending the arrays instead of using an assignment operator which can be done by writing the following您可以通过附加数组而不是使用赋值运算符来修复此错误,这可以通过编写以下内容来完成

for i in range (1,25):
    print("",i)
    if i < 10:
        u = 0.25
    else:
        u = 0.0

    x1.append((dt*x2[i]) + (dt**2/2)*u)
    x2.append(dt*u + math.sqrt(Q)*np.random.normal())

    y.append(math.sqrt(R) * np.random.normal())

There isn't much more I can provide in terms of help, unless you edit your answer to include the declaration and assignment of x1 and x2除非您编辑答案以包含x1x2的声明和赋值,否则我无法提供更多帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM