The Matlab event function doesn't stop the integration
3 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a ode system that I solve with Matlab. I want to find the steady state of the system and to do so, I use the event function as described here .
But some times, the solver doesn't stop even if the criterium is achieved. For example, if you solve the following system with x0 = 10 the solver will stop before 2000 but with x0 = 0.0001 it won't.
The event function (eventfun_t.m)
function [x,isterm,dir] = eventfun_t(t,y)
dy = test_systeme(t,y);
x = norm(dy) - 1e-3;
isterm = 1;
dir = 0; %or -1, doesn't matter
end
The system (test_systeme.m)
function dx = test_systeme(t,x)
v = x./(x+1);
dx = -v;
end
Solve the system
x0 = 10;
eventfonction = @(t,y) eventfun_t(t,y);
optionsode=odeset('Events',eventfonction);
[t x]=ode15s(@(t,x) test_systeme(t,x),[0 2000],x0,optionsode);
I suppose it's because with x0 = 0.0001 norm(dy) is already lower than 1e-3 but in that case, how can I stop the solver without checking by myself ?
4 个评论
Jan
2018-10-19
编辑:Jan
2018-10-19
dy = test_systeme(t,y);
if t~= 0 && x < 1e-3
x = 0
This cannot work, because x is undefined. Even if you mean dy instead of x, this is not a useful event function. Remember, that the event is triggered, when the 1st output of this functions becomes 0. Because it is very unlikely, that 0 is hit exactly, the integrators detect a change of the sign of this value and adjust the step size, to locate the exact zero crossing. Then "if t~= 0 && x < 1e-3, x = 0;" is not useful, because this is not a zero-crossing at t==0 even for tiny dy.
I did not test it, but Andrew's comment sounds reasonable: The ODE integrators do not detect an event at the initial step. So run the event function manually before starting the integration. This is cheap and easy, so I do not see a reason to implement any further tricks.
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulation Setup 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!