如何解决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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。