python – odeint < - > interp1d interplay中可能存在的错误?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – odeint < - > interp1d interplay中可能存在的错误?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1912字,纯文字阅读大概需要3分钟。
内容图文
![python – odeint < - > interp1d interplay中可能存在的错误?](/upload/InfoBanner/zyjiaocheng/783/24031f5691d2426d9cd19a00380aa04e.jpg)
我是python和scipy的新手,是MATLAB的转换.我正在对scipy.integrate中的odeint函数进行快速测试,并遇到了这个潜在的bug.请考虑以下代码段:
from scipy import *
from scipy.integrate import odeint
from scipy.interpolate import interp1d
from pylab import *
# ODE system with forcing function u(t)
def sis(x,t,u):
return [x[1], u(t)]
# Solution time span
t = linspace(0, 10, 1e3)
# Profile for forcing function u(t)
acel = lambda t: 3*(t<2)-3*(t>8)
# Interpolator for acelerator
acel_interp = interp1d(t, acel(t), bounds_error=False, fill_value=0)
# ODE integration with u(t) = acel, a lambda function
x_1 = odeint(sis, [0,0], t, args=(acel,) ) # Correct result
# ODE integration with u(t) = acel_interp, an interpolator
x_2 = odeint(sis, [0,0], t, args=(acel_interp,) ) # Incorrect result
我制作了一个图表,说明了两个结果的差异,click here.
你对此有何看法,至少在我看来,结果有何不公平的差异?我在Python 2.6.6上使用NumPy版本1.5.0和SciPy版本0.8.0
解决方法:
这不是一个错误.问题在于您已将bound_error转为False并用零填充这些值.如果在原始代码中将bound_error设置为True,则可以看到超出了插值的范围,因此在积分中放入零(因此产生的值不同于在外部的那些点处评估函数的值).与lambda for x_1相同的范围.
尝试以下操作,您将看到事情正常运行.基本上我只是扩展了t来覆盖一个足够大的值范围,以覆盖你使用插值的范围.
from scipy import *
from scipy.integrate import odeint
from scipy.interpolate import interp1d
from pylab import *
# ODE system with forcing function u(t)
def sis(x,t,u):
return [x[1], u(t)]
# Solution time span
t = linspace(0, 10, 1e3)
t_interp = linspace(0,20,2e3)
# Profile for forcing function u(t)
acel = lambda t: 3*(t<2)-3*(t>8)
# Interpolator for acelerator
acel_interp = interp1d(t_interp, acel(t_interp))
# ODE integration with u(t) = acel, a lambda function
x_1 = odeint(sis, [0,0], t, args=(acel,) )
# ODE integration with u(t) = acel_interp, an interpolator
x_2 = odeint(sis, [0,0], t, args=(acel_interp,) )
内容总结
以上是互联网集市为您收集整理的python – odeint < - > interp1d interplay中可能存在的错误?全部内容,希望文章能够帮你解决python – odeint < - > interp1d interplay中可能存在的错误?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。