Does event location terminate ODE solver at tspan?
2 次查看(过去 30 天)
显示 更早的评论
I just wonder what's the behavior of this edge case: a event location is setup to terminate the intergration, however the event coincide with the end of tspan.
options = odeset('Events',@myEventsFcn)
[t,y,te,ye,ie] = odeXY(odefun,tspan,y0,options)
function [value,isterminal,direction] = myEventsFcn(t,y)
...
isterminal=1;
end
If value(1) becomes 0 exactly on t==tspan(end)
Will te,ye,ie contain values, or be empty in this case?
0 个评论
回答(1 个)
Harsh Sharma
2024-9-22
Hi Yi-xia Liu,
The values of variables “te”,”ye” and “ie” are empty when the event is set to terminate the integration at “t=tspan(end)”. I have created an example to verify this for which the code is provided below:
function ballode2
tstart = 0;
tfinal = 4;
y0 = [0; 20];
refine = 4;
options = odeset('Events',@events,'OutputFcn',@odeplot,'OutputSel',1,...
'Refine',refine);
fig = figure;
ax = axes;
ax.XLim = [0 5];
ax.YLim = [0 25];
box on
hold on;
tout = tstart;
yout = y0.';
teout = [];
yeout = [];
ieout = [];
% Solve until the first terminal event.
[t,y,te,ye,ie] = ode23(@f,[tstart tfinal],y0,options);
nt = length(t);
tout = [tout; t(2:nt)];
yout = [yout; y(2:nt,:)];
teout = [teout; te];
yeout = [yeout; ye];
ieout = [ieout; ie];
disp(yeout)
plot(teout,yeout(:,1),'ro')
xlabel('time');
ylabel('height');
title('Ball trajectory and the events');
hold off
odeplot([],[],'done');
% -------------------------------------------------------------
function dydt = f(t,y)
dydt = [y(2); -10];
% --------------------------------------------------------------
function [value,isterminal,direction] = events(t,y)
% Locate the time when height passes through zero in a decreasing direction
% and stop integration.
value = y(1);
isterminal = 1;
direction = -1;
The example models a bouncing ball with exactly one bounce and the ball touches the ground at t=4. The event function is configured to terminate the integration when height=0 which occurs at t=4. The tspan(end) is also set to 4.
The output from the code is:
By putting a breakpoint in the code after calling “ode23” we can check that the variables “te”, “ye” and “ie” are empty.
I hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!