Python:odeint用于求解变系数常微分方程(QHO)

Python:odeint用于求解变系数常微分方程(QHO),python,differential-equations,odeint,Python,Differential Equations,Odeint,我试图用odeint数值解方程y'+(epsilon-x^2)y=0。我知道解(QHO的波函数),但odeint的输出与它没有明显的关系。我可以很好地解常系数的常微分方程,但一旦我转到可变系数,我就无法解任何我尝试过的常微分方程。这是我的密码: #!/usr/bin/python2 from __future__ import division import numpy as np import matplotlib.pyplot as plt import scipy.integrate as

我试图用odeint数值解方程
y'+(epsilon-x^2)y=0
。我知道解(QHO的波函数),但odeint的输出与它没有明显的关系。我可以很好地解常系数的常微分方程,但一旦我转到可变系数,我就无法解任何我尝试过的常微分方程。这是我的密码:

#!/usr/bin/python2
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as spi

x = np.linspace(-5,5,1e4)

n = 0
epsilon = 2*n+1 

def D(Y,x):
    return np.array([Y[1], (epsilon-x**2)*Y[0]])

Y0 = [0,1]

Y = spi.odeint(D,Y0,x)
# Y is an array with the first column being y(x) and the second y'(x) for all x
plt.plot(x,Y[:,0],label='num')
#plt.plot(x,Y[:,1],label='numderiv')

plt.legend()
plt.show()
情节是: [没有足够的代表:]


看看这里的解图:

看起来你的方程没有得到正确的解释。你有一个微分方程
y'+(epsilon-x^2)y=0
,但是你忘记了向量形式中的减号。尤其应该是

y[0]' = y[1]
y[1]' = -(epsilon-x^2)y[0]
所以(在ε项前面加上减号)

def D(Y,x):
    return np.array([Y[1], -(epsilon-x**2)*Y[0]])

事实上,您拥有的绘图与DE
y''+(epsilon-x^2)y=0
是一致的。请查看:

就是这样,谢谢!我想我已经盯着这个太久了:)