Does event location terminate ODE solver at tspan?

1 view (last 30 days)
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?

Answers (1)

Harsh Sharma
Harsh Sharma on 22 Sep 2024
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!

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!