解决薛定inger方程:ValueError:设置具有序列的数组元素。

时间:2018-11-14 17:50:12

标签: python arrays equation odeint

我需要编写一个python脚本,以在一定时间间隔[0,L]内由抛物线给定的电势来求解schrodinger方程。

我对python不太了解,我只是想修改教授的脚本,以解决相同的问题,但潜力无限。我定义了新的潜力,但由于遇到了此错误,我可能使用的是错误的

Traceback (most recent call last):
  File "C:/Users/Utente/Desktop/schrodinge.py", line 43, in <module>
    out=odeint(schrodinger,init,x,(E,))
  File "C:\Users\Utente\AppData\Local\Programs\Python\Python37-32\lib\site-packages\scipy\integrate\odepack.py", line 233, in odeint
    int(bool(tfirst)))
  File "C:/Users/Utente/Desktop/schrodinge.py", line 18, in schrodinger
    return np.array([g0,g1])
ValueError: setting an array element with a sequence.

我的代码如下:

    import numpy as np 
    import matplotlib.pyplot as plt
    from scipy.integrate import odeint

    def schrodinger(state,x,k):
        ''' system of 1st order differential equations to integrate.
            The Schrodinger equation for a free particle with wave vector,
            k**2 = 2m(E-U)/hbar**2

           args:
           state - array: psi[0] - Psi(x),  psi[1]= d/dx Psi(x).  Wave function and it's derivative.   
        '''
        g0 = state[1]
        g1 = -2*(E-Vx)*state[0]
        return np.array([g0,g1])

    L = 25
    U0 = (L/2)**2
    x1= np.arange(0., L, 0.01)
    x2= np.arange(-L, 0, 0.01)
    x3= np.arange(L, 2*L, 0.01)

    V1 = 0*x1
    V2 = U0 - (x1-(L/2))**2

    Vx = np.concatenate((V1,V2,V1))
    x = np.concatenate((x2,x1,x3))

    fig = plt.figure(num=1, figsize=(10,6))
    fig.clf()
    ax1= fig.add_subplot(111)

    ax1.plot(x,Vx)


    init=[1.,0] # the initial condition, [ Psi, d/dx Psi] at x=0.
    E_values = [U0/2,U0,2*U0]

    for E in E_values:   
        out=odeint(schrodinger,init,x,(E,))
        ax2 = fig.add_subplot(112)
        ax2.plot(x,out[:,0])


fig.canvas.draw()

fig.show()

你知道我在做什么错吗?谢谢您的帮助!

0 个答案:

没有答案