Do ODE solvers use location of root of event function in its solution even if integration isn't specified to stop?

Say I want to solve an ODE with a piece-wise continuous function and I want to accrurately capture the corner point in the solution. Do I need to set the solver to stop when it reaches the corner (which is detected as an event), or can I let it keep integrating and the solver will use the presice location of the corner as a step in its solution?

 Accepted Answer

You will have to tell the solver to return control to the calling program once the corner point is reached. From there, you can continue integration.
If the corner point is known in advance (e.g. a certain time t*), you can integrate (without the use of an event function) from tstart to t* and restart the solver from t* to tend.

5 Comments

I'm not sure I understand what you mean. Are you describing how the event detection works? Where the "solver" is the ODE solver and the "calling program" is the event finder (i.e., root solver)? I understand how this works. I'm just trying to understand how the ODE solver and event finder behave with each other. If an event is found by the event finder AND when integration is specifed to not stop, does the event finder return this precise location to the ODE solver to use as a step in its calculations? Or does the ODE solver just perform integration to its specified precision, potentially "skipping" the precise location of the event?
The corner point will not be known in advance.
The event locations are saved.
But usually you want the solver to stop if an event occurs and start anew because you want to make changes to your equations. What should be the purpose to detect events if you just let the solver integrate over them ?
You will see that "te" is not part of the t - array - thus the basic integration steps don't include the event. This means that the events are just documented "in passing", but don't influence the step sizes of the integrator.
fun = @(t,y) -y;
y0 = 1;
tspan = [0 1];
options = odeset('Events',@event);
[t,y,te,ye,ie] = ode45(fun,[0 1],y0,options);
t(t>0.25)
ans = 30x1
0.2750 0.3000 0.3250 0.3500 0.3750 0.4000 0.4250 0.4500 0.4750 0.5000
te
te = 2x1
0.2877 0.6931
-log(0.75)
ans = 0.2877
-log(0.5)
ans = 0.6931
function [position,isterminal,direction] = event(t,y)
position(1) = y(1) - 0.75;
isterminal(1) = 0;
direction(1) = 0;
position(2) = y(1) - 0.5;
isterminal(2) = 0;
direction(2) = 0;
end
Ok I see. The reason I would not want the integration to stop is so that I can code the piece-wise continuous function "as is" in the ODE function file. For example, if my ODEs had the function on if , if , I would want to be able to just code this directly maybe with some logic or some other way as opposed to stopping integration, changing the value of , and then restarting integration.
My understanding based on what you said is that I do in fact need to stop integration because the solver will not necessarily use the location where as provided by the event detector in the solution of the ODEs. It will just integrate past it which may result in imprecise solutions since may change values at the wrong time. Is this understanding correct?
Your understanding is correct.
Each time your equations change (in your case even discontinuously), you have to stop integration and restart from the solution obtained so far. So you should just pass a flag to your ode function that indicates if you are in the regime before or after the event. Depending on this flag, you can set f = 0 or f = 1.
So don't use an if statement concerning sin(x) < 0 or >=0 to control the value for f, but the flag you set in the calling program which indicates whether you are before or after the event location.

Sign in to comment.

More Answers (0)

Asked:

on 4 Mar 2024

Commented:

on 4 Mar 2024

Community Treasure Hunt

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

Start Hunting!