得到“IndexError:列表索引超出范围”,显然没有理由

时间:2016-05-26 03:14:51

标签: python-2.7 sys

我正在尝试解决Schrodinger equtaion并编写程序正常,仍然收到此错误,我无法在代码中看到错误...如果我的代码中有任何其他错误,请通知我..

""" SEsolve.py.......................
uses shootingmethod t find even-parity solutions to the inite square well. Takes two guesse of energy from the command line, finds a bracketed energy eigenalue E, and plots the corresponding waavefunctions..
h_bar, m and L are all taken to be 1

the parameter b is the pointin units of L at which to check if the wavefunction dierges, b must be greater than 1 , of course...

Solutions are not normalized"""

from pylab import *
from scipy.integrate import odeint
from scipy.optimize import brentq
import sys

b = 2.0
V0 = 20.0                      # potenial outside square well
steps = 1000 
E = 0.0                         # global variable, changed by finlvalue

def V(x):                       #potential inwhich the partice exists
    if x < 1.0:
        return 0
    else:
        return V0        # definition of square well

def SE(y,x):
    """ returns derivatives for the 1-D TISE, for use in oeint, requires globl value  to be set esewhere, Note that we are using x stime here.."""
    g0 = y[1]
    g1 = -2.0 * (E-V(x)) * y[0]
    return array ([g0, g1])

def Final_Value(energy):
    """calculates wave funcion for thisvalue of E, and returns the value ofpsi at point b to check divergence"""
    global y
    global E
    E = energy
    y = odeint ( SE, y0, x)
    return y[-1,0]


y = zeros ([steps, 2])
y0 = array ([1.0, 0.0])
x = linspace (0,b, steps)

E1 =float (sys.argv[1])
E2 = float(sys.argv[2])

answer = brentq (Final_Value, E1, E2)


print "Eigenvalue found at E = %0.8f" % answer

plot (x, y[:,0])
xlabel("poition (units of L)")
show()

错误也附加

Traceback (most recent call last):
  File "PDE.py", line 44, in <module>
    E1 =float (sys.argv[1])
IndexError: list index out of range

1 个答案:

答案 0 :(得分:0)

运行此脚本时,是否从命令行运行它?根据这个extremely detailed answer,sys.argv [1]将返回一个超出范围的错误,除非在命令参数中放置了一个值。

要运行它而不出错,您需要使用当前工作目录中的脚本运行以下命令:

  

python PDE.py(您的第一个浮点值)(您的第二个浮点值)

希望这有帮助!