ODE integration with discretized values

折月煮酒 提交于 2019-12-05 19:11:28

Yes, the callable needs to be a function which returns the derivative for any value that is provided to the function. If you have a function interp which does the interpolation, you can define the callable as follows:

f = lambda t,y: interp(y, yvalues, fvalues)

In case your system is scalar, you can use the numpy.interp function like in the following example:

import numpy
from scipy import integrate
yvalues = numpy.arange(-2,3,0.1)
fvalues = - numpy.sin(yvalues)
f = lambda t,y: numpy.interp(y, yvalues, fvalues)
r = integrate.ode(f)
r.set_initial_value(1)
t1 = 10
dt = 0.1
while r.successful() and r.t < t1:
    r.integrate(r.t+dt)
    print r.t, r.y

For a multidimensional system, interpolation is very involved. If there is any way to compute the derivative on the fly at a given point, it is probably easier to implement than using interpolation.

As unutbu points out in the comment, you will get a wrong solution for large enough time with a chaotic system if you interpolate. However, since the same is true for any numerical solution algorithm, it is hard to do anything about it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!