MATLAB: Does ODE45 return an exit flag in MATLAB 7.8 (R2009a)

Statistics and Machine Learning Toolbox

I have a system of 30 differential equations compiled to a DLL. I would like to know if there is an exit flag for ODE45 which can be returned as an output in case bad data is passed to the DLL and the algorithm fails to converge.

Best Answer

ODE45 does not return an exit flag in MATLAB 7.8 (R2009a). It performs the integration until it has converged on a solution and the tolerances have been met. However, a warning is generated if the integration tolerances are not met when the algorithm terminates.
The message identifier for the warning generated by ODE45 when the integration tolerances are not met is MATLAB:ode45:IntegrationTolNotMet. You may use the LASTWARN function to return the last warning message generated by MATLAB. For example, to check whether ODE45 generated the warning above, execute the following:
[msgstr, msgid] = lastwarn;
if strcmp(msgid, 'MATLAB:ode45:IntegrationTolNotMet')
%execute desired code
end
To ensure that you do not detect the same warning multiple times, use the following syntax to change the warning message and identifier, so that subsequent invocations of lastwarn return the message and identifier strings specified by new_msgstr and new_msgid, respectively.
[msgstr, msgid] = lastwarn('new_msgstr', 'new_msgid')
Alternatively, you may check if the largest value of the first output argument 'T' is 'close enough' to the largest value of the second input argument 'tspan'. If the solution time vector 'T' does not cover the entire time span, the integration failed.
Related Question