gpt4 book ai didi

python - 如何在 scipy.integrate.solve_ivp 中使用事件

转载 作者:太空狗 更新时间:2023-10-30 01:25:05 26 4
gpt4 key购买 nike

我不确定 scipy.integrate.solve_ivp 中的事件处理是否正常工作。在下面的示例中,我集成了一个导数,该导数应生成具有三个根的三次多项式,分别位于 x=-6、x=-2 和 x=2。我设置了一个返回 y 的事件函数,它在那些 x 值处将为零。我希望在解决方案的 t_events 属性中看到三个条目,但我只看到一个,即使很明显解决方案与 x 轴交叉三次。

我是不是做错了什么?

重现代码示例:

def fprime(x, y):
return 3 * x**2 + 12 * x - 4

def event(x, y):
return y

import numpy as np
from scipy.integrate import solve_ivp
sol = solve_ivp(fprime, (-8, 4), np.array([-120]), t_eval=np.linspace(-8, 4, 10), events=[event])

上面的代码导致:

message: 'The solver successfully reached the interval end.'
nfev: 26
njev: 0
nlu: 0
sol: None
status: 0
success: True
t: array([-8. , -6.66666667, -5.33333333, -4. , -2.66666667,
-1.33333333, 0. , 1.33333333, 2.66666667, 4. ])
t_events: [array([-6.])]
y: array([[-120. , -26.96296296, 16.2962963 , 24. ,
10.37037037, -10.37037037, -24. , -16.2962963 ,
26.96296296, 120. ]])

```

问题是从sol.y数组可以看出应该有3个0(有3个变号),但是只记录了1个事件。

Scipy/Numpy/Python 版本信息:

1.0.0 1.13.3 sys.version_info(major=3, minor=6, micro=0, releaselevel='final', serial=0)

[更新]:如果我对 solve_ivp 使用可选的 max_step 参数,并使其足够小,那么我会看到所有三个根。似乎事件函数没有在 t_eval 步骤中调用,而是仅在内部求解器步骤中调用,这些步骤远少于 t_eval 步骤,并且最终跳过了一些根。这似乎不是很有用,因为您必须知道如何设置 max_steps 以避免丢失根。

最佳答案

the event functions are not called at the t_eval steps, but rather only on internal solver steps

这是正确的。在新的 (t, y)is found 之后,事件是根据它们计算的 and passedfind_active_events,它将事件函数的符号与上一步进行比较。

t_eval 根本不被事件计算使用,它被处理为 later .因此,事件看到的符号变化是您在使用 t_eval=None 的输出中看到的那些变化,其中只有一个。

y: array([[-120.        , -110.49687882,  -35.93785936,   94.46893375,
120. ]])

似乎值得在 SciPy 跟踪器上提出问题;有a few open issues regarding solve_ivp ,这仍然很新。

关于python - 如何在 scipy.integrate.solve_ivp 中使用事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52175326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com