错误:索引越界

时间:2014-06-27 04:06:56

标签: python

可以看出,我每个人都有1000个师,西格玛。但是当我可以看到从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"

输出如下:

 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.

请帮助。

1 个答案:

答案 0 :(得分:0)

因为循环中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"

关于K3=np.nonzero(K2>0)语句,i == 121时变为0,再次检查算法。