How to become aware of failure of ode45 without looking at the displayed warning?

假如想象 提交于 2019-12-06 09:39:29

You can turn that warning into an error, and errors can be caught by try/catch blocks.

An example:

% Change this particular warning into an error
warnId = 'MATLAB:ode45:IntegrationTolNotMet';
warnstate = warning('error', warnId);    

% That can be caught with try/catch
try     
    % Quick-failing integration, suggested by horchler
    [t,y] = ode45(@(t,y)[y(1)^2;y(2)],[0 1],[1;1]);

catch ME
    % Check if we indeed failed on meeting the tolerances
    if strcmp(ME.identifier, warnId)

        % DO YOUR STUFF HERE

    else
        % Something else has gone wrong: just re-throw the error
        throw(ME);
    end

end

% Don't forget to reset the warning status
warning(warnstate);

You can get the warnId of any warning by virtue of the lastwarn command. For errors, see lasterr.

Another method would be to just check for how much time ode45 was able to run. for example if you run

[x,t] = ode45(@some_ode,[t0,tf],x0);

then after this line is executed, simply check the value of t(end). If t(end)==tf, then ode45 did its job, otherwise there was a failure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!