问题描述
我有一个对象,当它达到阈值时,它将进入一个静默期,我使用一个在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