Using a Single Event Function to Account for Multiple Events
24 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
更多回答(1 个)
Hari
2024-4-24
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:
- 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.
- 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.
- 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.
- 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:
- For understanding how to use event functions with MATLAB ODE solvers, refer to the documentation of ODE event location: https://www.mathworks.com/help/matlab/math/ode-event-location.html
- To get more insights into writing efficient MATLAB code, especially for simulations and numerical computations, see: https://www.mathworks.com/help/matlab/matlab_prog/techniques-for-improving-performance.html
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!