如何找到 ODE 的导数在一次调用中被调用的次数。

How to find the number of times the ODE's derivative is called in one invocation.

我正在使用 Python 做有关 ODE 数值积分的练习。我遇到了这个问题。

“在 ODE 积分器的每个 运行 期间(即一次调用 integrate(t) 方法),积分器在内部将时间间隔划分为许多离散的步骤,并且 运行 使用这些步骤指定方案(例如 Runge-Kutta)。让我们调查一下 ODE 的导数函数在此过程中被调用了多少次。"

有没有什么方法可以找到次数? 谢谢。

在传递给集成器的函数中,您可以编写一些代码来为您收集统计信息。例如,而不是

def f(t, x):
    return -2 * x

class F:

    def __init__(self):
        self.calls = 0

    def __call__(t, x):
        self.calls += 1
        return -2 * x

然后将此 class 的对象传递给集成器,例如(示意图)

f = F()
r = ode(f, jac)
r.integrate(tmax)
print(f.calls)