Using a Single Event Function to Account for Multiple Events

24 views (last 30 days)
Hi All,
I have been using event functions in my simulation and they are great. Just ran into a case where I would like to stop the numerical integration after two of my event functions trigger... Can I cherry pick event functions and make a single event function to take care of all?
I am assuming that the event value starts positive and crosses zero at some point. Then, I could probably call another event function inside my current event function, correct? And then, may be I can use max function to choose the largest event value to make sure that all the events are triggered? Will this method work? If so, could this method suffer computationally?
The problem here is that, for the case having multiple events, I do not necessarily know in advance which event would occur first!
Many thanks in advance! :)
Sincerely,
Taehun Kim

Accepted Answer

Taehun Kim
Taehun Kim on 24 Apr 2024
Thank you, Hari, for your help in this! I appreciate your helpful comment in this!

More Answers (1)

Hari
Hari on 24 Apr 2024
Hi,
I understand that you are working with event functions in your simulation and want to stop the numerical integration after two specific events trigger. You are considering combining multiple event functions into a single one to manage all events together, despite not knowing which event will occur first.
I assume you are using MATLAB's ODE solvers, like ode45, which support event handling functionality.
To address this problem, you can indeed use a single event function to handle multiple events. Here's how you can approach it:
  1. Define a Persistent Counter: Use a persistent variable within your event function to count the number of times an event condition has been met. This counter will help you track when two events have occurred.
  2. Combine Event Conditions: Within your single event function, include conditions for both events. You can return the value of the condition that is closest to triggering (i.e., closest to zero) or use a logical operation to combine them.
  3. Stopping the Integration: To stop the integration after two events, use the "isterminal" output of your event function. Once your counter indicates that two events have occurred, set isterminal to 1.
  4. Computational Considerations: Combining multiple events into a single function should not significantly impact computational performance in a negative way. MATLAB's ODE solvers are designed to efficiently handle event functions. However, ensure that your event conditions are not overly complex to avoid unnecessary computational overhead.
References for further exploration:
Hope this helps!

Community Treasure Hunt

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

Start Hunting!