Does event location terminate ODE solver at tspan?

조회 수: 6 (최근 30일)
Yi-xiao Liu
Yi-xiao Liu 2021년 3월 30일
답변: Harsh 2024년 9월 22일
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?

답변 (1개)

Harsh
Harsh 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!

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by