了解 odeint 结果 - 与解析解不一致
Understanding odeint results - disagreement with analytical solution
几天前我在玩 odeint,觉得有些结果很奇怪。所以,我想 运行 一些非常简单的东西来测试我的代码(如下所示)。
如果 (dy/dt) = -x,对其进行积分应该得到 (-x^2)/2 + C。我得到的图肯定没有显示出来。很明显我做错了什么,但我无法弄清楚缺少什么。有人可以指点一下吗?
代码如下 -
import scipy.integrate as scint
import numpy as np
import matplotlib.pyplot as plt
def odefunc(y0, t):
x = y0
dxdt = -1*x
return dxdt
plt.close('all')
t = np.arange(0, 5, 0.02)
y0 = 1
soln = scint.odeint(odefunc, y0, t)
fig1 = plt.figure(1, figsize = (9, 6))
plt.grid(color = 'grey', linestyle = ':', linewidth = 1)
plt.plot(t, soln, marker = 'd', linestyle = 'none', color = 'black')
plt.xlabel('Time')
plt.ylabel('Event')
plt.tight_layout()
这是情节 -
Plot of the solution
我刚开始使用 python。这不是作业问题或任何问题。我学习 python 主要是为了好玩。
你写的是 "If (dy/dt) = -x, integrating it should yield (-x^2)/2 + C",但我认为你的意思是 "If (dy/dt) = -t, integrating it should yield (-t^2)/2 + C"。这不是您在 Python 代码中实现的等式。
您对 odefunc
、
的实施
def odefunc(y0, t):
x = y0
dxdt = -1*x
return dxdt
对应微分方程dy/dt = -y。的解决方案
该等式是 y(t) = y(0)*exp(-t)。 那个 是您图表中绘制的函数。
如果要用odeint
解dy/dt = -t,那么odefunc
应该是
def odefunc(y, t):
return -t
几天前我在玩 odeint,觉得有些结果很奇怪。所以,我想 运行 一些非常简单的东西来测试我的代码(如下所示)。
如果 (dy/dt) = -x,对其进行积分应该得到 (-x^2)/2 + C。我得到的图肯定没有显示出来。很明显我做错了什么,但我无法弄清楚缺少什么。有人可以指点一下吗?
代码如下 -
import scipy.integrate as scint
import numpy as np
import matplotlib.pyplot as plt
def odefunc(y0, t):
x = y0
dxdt = -1*x
return dxdt
plt.close('all')
t = np.arange(0, 5, 0.02)
y0 = 1
soln = scint.odeint(odefunc, y0, t)
fig1 = plt.figure(1, figsize = (9, 6))
plt.grid(color = 'grey', linestyle = ':', linewidth = 1)
plt.plot(t, soln, marker = 'd', linestyle = 'none', color = 'black')
plt.xlabel('Time')
plt.ylabel('Event')
plt.tight_layout()
这是情节 -
Plot of the solution
我刚开始使用 python。这不是作业问题或任何问题。我学习 python 主要是为了好玩。
你写的是 "If (dy/dt) = -x, integrating it should yield (-x^2)/2 + C",但我认为你的意思是 "If (dy/dt) = -t, integrating it should yield (-t^2)/2 + C"。这不是您在 Python 代码中实现的等式。
您对 odefunc
、
def odefunc(y0, t):
x = y0
dxdt = -1*x
return dxdt
对应微分方程dy/dt = -y。的解决方案 该等式是 y(t) = y(0)*exp(-t)。 那个 是您图表中绘制的函数。
如果要用odeint
解dy/dt = -t,那么odefunc
应该是
def odefunc(y, t):
return -t