gpt4 book ai didi

python - solve_ivp 从 -x 到 +x

转载 作者:太空宇宙 更新时间:2023-11-03 11:59:34 27 4
gpt4 key购买 nike

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

这里是精简代码

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

最佳答案

不确定是否有帮助,但它可能会给您一些提示。并不是说 solve_ivp 对 -x 到 0 不起作用,而是你的函数 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

关于python - solve_ivp 从 -x 到 +x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53431271/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com