How to use Matlab ODE solver events to stop an integration

Click For Summary
SUMMARY

This discussion focuses on utilizing Matlab's ODE solver, specifically the ode15s function, to implement event handling for stopping integration based on convergence criteria. The user, ryan, encountered challenges with the 'Events' feature but ultimately devised a solution by creating a global variable shared between the Tprime_converger and Tprime functions. The modified event function effectively checks for convergence, resulting in a rapid convergence rate with minimal difference compared to a traditional loop-based convergence checker.

PREREQUISITES
  • Familiarity with Matlab programming and syntax
  • Understanding of ODE solvers, particularly Matlab's ode15s
  • Knowledge of event handling in numerical methods
  • Basic concepts of convergence criteria in mathematical modeling
NEXT STEPS
  • Explore Matlab's documentation on the ode15s solver and its event handling capabilities
  • Learn about global variables in Matlab and their implications on function scope
  • Investigate alternative methods for convergence checking in ODE systems
  • Study performance comparisons between event-driven and loop-based ODE integration techniques
USEFUL FOR

Matlab users, numerical analysts, and researchers working with ordinary differential equations who seek to optimize integration processes and implement effective convergence criteria.

ryan.j
Gold Member
Messages
8
Reaction score
0
How to use Matlab ODE solver "events" to stop an integration

I'm using Matlab's ODE solver (specifically ode15s) to solve a system of equations. The sum of the values of the equations eventually arrive at a steady state, but the time at which that occurs is dependent on several things, not known beforehand, and is one of the things I'm studying.

Consequently, I'd like to set tf in the call to the ODE solver to a value that I'm confident is beyond that time, and cause the integration to stop once a certain convergence criteria is met.

I have been unable to use 'Events' to do this effectively. Here is the function that is called by 'Events':

function [value,isterminal,direction] = Tprime_converger(t,T)

if length(t) == 1 %convergence can only be checked for t>1
value = 1;
else
value = abs(sum(T(length(t)-1,;)) - sum(T(length(t),;))) - 2*eps;
end

isterminal = 1;
direction = 0;

endAny help would be greatly appreciated.

Thanks.
-ryan
 
Last edited:
Physics news on Phys.org


After ruminating on it through the night I came up with a solution. If someone knows of a better way to do this, by all means share it.

What I did was create a variable that was global to both the "Tprime_converger" function and the "Tprime" function, which is the function where the system of ODEs is defined.

At the end of "Tprime", I added the computation :

Taco = T - dT;

and I changed "Tprime_converger" to :

function [value,isterminal,direction] = Tprime_converger(t,T)

value = abs( sum(Taco(:)) - sum(T(:)) ) - 2*eps;

isterminal = 1;
direction = -1;

end

I checked the values against computations using the same functions without implementing any 'Events' where the ODE solver is part of a loop with its own convergence checker built in at each time step the loop iterates on, and the solutions differed with a relative difference on the order of 10^-14; essentially no difference. The upshot is that the implementation using 'Events' converges significantly more rapidly.

-ryan
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K