odeint从整合在Python中给出错误的结果?

2024-04-27 07:30:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用以下代码求解ivp y'=-y-5*exp(-t)*sin(5t),y(0)=1:

%pylab inline
%matplotlib inline
from scipy.integrate import odeint

def mif(t, y):
    return -y-5*exp(-t)*sin(5*t)

tspan = np.arange(0, 3, 0.000001)
y0 = 1.0
y_result = odeint(mif, y0, tspan)
y_result = y_result[:, 0]  # convert the returned 2D array to a 1D array
plt.figure()
plt.plot(tspan, y_result)
plt.show()

然而,我得到的图是错误的,它与我所得到的不匹配,比如说,用Matlab或Mathematica。它实际上不同于以下替代集成:

^{pr2}$

结果是正确的。我拿奥迪恩怎么了?在


Tags: 代码frommatplotlibinlinepltsinresultarray
1条回答
网友
1楼 · 发布于 2024-04-27 07:30:38

函数参数在ode和odeint之间变化。你需要什么

def mif(y, t):

为了颂歌

^{pr2}$

例如

%pylab inline
%matplotlib inline
from scipy.integrate import odeint

def mif(t,y):
    return y

tspan = np.arange(0, 3, 0.000001)
y0 = 0.0
y_result = odeint(mif, y0, tspan)
plt.figure()
plt.plot(tspan, y_result)
plt.show()

以及

from scipy.integrate import ode

def mif(y, t):
    return y

# initialize the 4th order Runge-Kutta solver
solver = ode(mif).set_integrator('dop853')

# initial value
y0 = 0.000000
solver.set_initial_value([y0], 0.0)

values = 1000
t = np.linspace(0.0000001, 3, values)
y = np.zeros(values)

for ii in range(values):
    y[ii] = solver.integrate(t[ii]) #z[0]=u
plt.figure()
plt.plot(t, y)
plt.show()

相关问题 更多 >