简体   繁体   English

错误:索引超出范围

[英]Error:Index out of bounds

As can be seen, I am having 1000 divisions each of theta, sigma. 可以看出,我每个θ,sigma都有1000个划分。 But when I am iterating from 1 to 1000 for theta as can be seen, I am getting an error. 但是,当我从1迭代到1000到theta时,我发现一个错误。

import time
start_time = time.clock()

import numpy as np
theta=np.linspace(3,100,1000)
sigma=np.linspace(0,10,1000)
Re=5

import os
completeName = os.path.abspath("New Volume (F:)/New Innings 3/Sigma at Re=5 @100 .txt")
file = open("Sigma at Re=5 @100.txt", "w")

for i in np.arange(0,K,1):         //K=1000
    mu=np.sqrt(Re*sigma)
    A=(mu-1)*np.exp(mu)+(mu+1)*np.exp(-mu)
    B=2*mu*(theta[i])
    C=(A/B)

    D1=np.exp(mu)/(2*(mu+sigma))
    D2=np.exp(-mu)/(2*(mu-sigma))
    D3=mu**2
    D4=np.exp(-sigma)
    D5=sigma
    D6=(mu**2)-(sigma**2)
    D7=D3*D4
    D8=D5*D6
    H=D7/D8
    D9=(1/sigma)
    D=D1-D2+H-D9
    K1=C-D
    K2=np.delete(K1,0)
    K3=np.nonzero(K2>0)
    K4=sigma[K3]
    K5=K4[0]
    K55=np.array(K5)

    file.write("%g\n" % K55)

file.close()

print time.clock() - start_time, "seconds"

The output is the following: 输出如下:

 33     K3=np.nonzero(K2>0)
 34     K4=sigma[K3]
 35     K5=K4[0]
 36     K55=np.array(K5)

IndexError: index out of bounds corresponding to line 35.

Kindly help. 请帮助。

Because your K4 list becomes empty in the loop, you can debug with the IDE debugger or just write print statement; 因为您的K4列表在循环中变为空,所以您可以使用IDE调试器进行调试,也可以只编写print语句。

import time
start_time = time.clock()

import numpy as np
theta=np.linspace(3,100,1000)
sigma=np.linspace(0,10,1000)
Re=5

import os
completeName = os.path.abspath("New Volume (F:)/New Innings 3/Sigma at Re=5 @100 .txt")
file = open("Sigma at Re=5 @100.txt", "w")

for i in np.arange(0,1000,1):         # K=1000
    mu=np.sqrt(Re*sigma)
    A=(mu-1)*np.exp(mu)+(mu+1)*np.exp(-mu)
    B=2*mu*(theta[i])
    C=(A/B)

    D1=np.exp(mu)/(2*(mu+sigma))
    D2=np.exp(-mu)/(2*(mu-sigma))
    D3=mu**2
    D4=np.exp(-sigma)
    D5=sigma
    D6=(mu**2)-(sigma**2)
    D7=D3*D4
    D8=D5*D6
    H=D7/D8
    D9=(1/sigma)
    D=D1-D2+H-D9
    K1=C-D
    K2=np.delete(K1,0)
    K3=np.nonzero(K2>0)
    if len(K4) == 0:
        print "i:",i
        print "K4:",K4
        break 
    K4=sigma[K3]
    K5=K4[0]
    K55=np.array(K5)

    #file.write("%g\n" % K55)

file.close()

#print time.clock() - start_time, "seconds"

It is about the K3=np.nonzero(K2>0) statement, it becomes 0 when i == 121 , check your algorithm again. 关于K3=np.nonzero(K2>0)语句,当i == 121时它变为0,再次检查算法。

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

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