从-x到+ x的solve_ivp

时间:2018-11-22 12:41:05

标签: python scipy

我一直在尝试使用solve_ivp或solve_bvp解决我遇到的问题,但没有取得任何进展。我认为我在这里使用的代码可以正常工作,但是我无法获得正确的范围。出于某种原因,我无法理解范围始终是从0到x,而不是从-x到x,有人可以帮我解决这一问题的问题吗?

这是减少到最小的代码

from pylab import *
from scipy.integrate import solve_ivp
from scipy.optimize import brentq
import numpy as np
import itertools

a=1
B=4
L= B+a
Vmax= 50
Vpot = False

N = 1000                  # number of points to take
psi = np.zeros([N,2])     # Wave function values and its derivative (psi and psi')
psi0 = array([0,1])   # Wave function initial states
Vo = 50
E = 0.0                   # global variable Energy  needed for Sch.Eq, changed in function "Wave function"
b = L                     # point outside of well where we need to check if the function diverges
x = linspace(-B-a, L, N)    # x-axis
def V(x):
    '''
    #Potential function in the finite square well.
    '''
    if -a <=x <=a:
        val = Vo
    elif x<=-a-B:
        val = Vmax
    elif x>=L:
        val = Vmax
    else:
        val = 0
    return val

def SE(z, p):
    state0 = p[1]
    state1 = 1.0*(V(z) - E)*p[0]
    return array([state0, state1])

def Wave_function(energy):
    global E
    E = energy
    #        odeint(func, y0, t)
    #     solve_ivp(fun, t_span, y0)
    psi = solve_ivp(SE, [-B-a, L], psi0, max_step = ((B+a+L)/(N)))
    return psi.y

def main():
    # main program        
    f2 = figure(2)
    plot(x, Wave_function(9.8)[0][:1000])
    grid()
    f2.show()

if __name__ == "__main__":
    main()

这是代码最后给我的。右侧可以,但左侧是错误的。我依靠双方的努力,而不是为了视觉。 enter image description here

编辑: 对于慈善机构,这是潜在功能应具有的外观: enter image description here

最终图形应类似于此: enter image description here

2 个答案:

答案 0 :(得分:0)

不确定是否有帮助,但可能会给您提示。 不是说-x为0时resolve_ivp不起作用,但是函数V可能是错误的。我注意到在V从Vmax减小到0之后,波开始出现。

此代码:

%matplotlib inline
from pylab import *
from scipy.integrate import solve_ivp
from scipy.optimize import brentq
import numpy as np
import itertools

a=1.
B=4.
L= B+a
Vmax= 50.

N = 10000
E = 9.8

def V(x):
    if -L <= x <= -B:
        return Vmax
    else:
        return 0

def SE(z, p):
    state0 = p[1]
    state1 = (V(z) - E)*p[0]
    return array([state0, state1])

def Wave_function():
    return solve_ivp(SE, [-L, L], [0., 1.], max_step=2*L/N)

result = Wave_function()
plot(result.t, result.y[0], color='tab:blue')

为您提供“预期”输出:

enter image description here

答案 1 :(得分:0)

您的代码总体来说还可以。但是,根据您的势能数字, Vo 的值应为 * Vo = 10 。另外,在您的 main 函数中,您仅绘制了 wave函数作为Schrodinger方程的解。贝娄,是我建议您作为一种可能的解决方案,前提是我已正确理解您的担忧:

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

a=1
B=4
L= B+a
Vmax= 50


N = 1000                  # number of points to take
psi = np.zeros([N,2])     # Wave function values and its derivative (psi and psi')
psi0 = np.array([0,1])   # Wave function initial states
Vo = 10     # Not 50, in order to conform your figure of the potential energy
E = 0.0         # global variable Energy  needed for Sch.Eq, changed in    
                # function "Wave function"
b = L           # point outside of well where we need to check if the function diverges
x = np.linspace(-L, L, N) # linspace(-B-a, L, N)    # x-axis


def V(x):
    '''
    Potential function in the finite square well.
    '''
    if -a <=x <=a:
        val = Vo
    elif x<= -L:  # -a-B:
        val = Vmax
    elif x>=L:
        val = Vmax
    else:
        val = 0
    return val

def SE(z, p):
    state0 = p[1]
    state1 = 1.0*(V(z) - E)*p[0]
    return array([state0, state1])

def Wave_function(energy):
    global E
    E = energy
    psi = solve_ivp(SE, [-B-a, L], psi0, max_step = ((B+a+L)/(N)))
    return psi.y

def main():
    # main program       
    plt.figure()
    plt.subplot(121)
    plt.plot(x, Wave_function(9.8)[0][:1000])
    plt.grid()
    plt.title("Wave function")
    plt.xlabel(r"$ x $")
    plt.ylabel(r"$\psi(x)$")
    plt.subplot(122)

    potential = np.vectorize(V) # Make the function 'V(x)' to also work on array

    pot = potential(x) # Potential ernergy in the defined domain of 'x'
    t = [-L, -a, a, L] # the singular value of x
    y = potential(t)   # the potential energy at thos singular value of 'x'
    # But to conform your figure I'll just do y = 0 * y
    plt.plot(x, pot, t, 0*y, 'ko')
    plt.title("Potential Energy")
    plt.xlabel(r"$ x $")
    plt.ylabel(r"$V(x)$")
    plt.show()

if __name__ == "__main__":
    main()

输出图如下:

enter image description here