DifferentialEquations.jl中的回调函数选择问题

问题描述

我有一个对象,当它达到阈值时,它将进入一个静默期,我使用一个在1到0之间翻转的参数(我称之为 ode_status )来确定是否执行ODE是否。

该阈值由ContinuousCallback实现。

fucntion condition(u,t,integrator)
    u[1] - threshold
end

function affect!(integrator)
    integrator.p[1] = 0  # integrator.p[1] represents ode_status
    flip_back_time[1] = integrator.t + 5  # define silence period = 5s
end

ContinuousCallback(condition,affect!)

接下来,我想在5s后回退ode_status,所以我使用DiscreteCallback

function condition(u,integrator)
    integrator.p[1] == 0 &&
    integrator.t >= flip_back_time[1]
end

function affect!(integrator)
    integrator.p[1] = 1
end

DiscreteCallback(condition,affect!)

但是,结果并非我所想。 ode_status向后翻转的时间并非完全在5s之后。在5.107 ...或在另一项试用中是5.879。

我认为我滥用了这些回调函数。谁能告诉我如何解决这个问题?预先感谢!

解决方法

这是因为下一步并不完全是在 5 秒后的时间。请记住,DiscreteCallback 仅在步骤时间触发,因此您需要插入一个 tstop 以告诉它在未来 5 秒内停止。

function affect!(integrator)
    integrator.p[1] = 0  # integrator.p[1] represents ode_status
    flip_back_time[1] = integrator.t + 5  # define silence period = 5s
    add_tstop!(integrator,integrator.t + 5)
end

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...