ODE集成离散值 [英] ODE integration with discretized values

查看:98
本文介绍了ODE集成离散值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用scipy.integrate.ode求解器.我只能将可调用函数f定义为离散点的数组(因为它取决于先前迭代的积分结果).但是从文档看来,集成商似乎希望callable是一个连续函数.我想需要进行某种插值.求解器可以自己解决这个问题,还是需要编写一些插值例程?有一些解释性的文档/教程吗?

I want to use scipy.integrate.ode solver. I can define the callable function f only as an array of discrete points (because it depends on results of integration from previous iterations). But from the documentation it seems that the integrator expects the callable to be a continuous function. I suppose some sort of interpolation needs to be done. Can the solver deal with this on its own, or do I need to write some interpolation routine? Is there some scipy documentation/tutorial that explains it?

推荐答案

是的,可调用对象必须是一个函数,该函数返回提供给该函数的任何值的导数.如果您有一个函数interp进行插值,则可以如下定义可调用对象:

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)

如果您的系统是标量,则可以使用numpy.interp函数,如以下示例所示:

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.

正如unutbu在评论中指出的那样,如果进行插值,那么对于足够长的时间,如果使用混沌系统,您将得到错误的解决方案.但是,由于任何数值解算法都是如此,因此很难对其做任何事情.

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.

这篇关于ODE集成离散值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆