如何在不查看显示警告的情况下了解ode45的故障?

时间:2013-07-08 22:48:10

标签: matlab ode

当ODE45的解决方案发生偏离(无关紧要的原因和方式)时,将显示以下警告,并且解算器无法继续:

  

警告:t = 8.190397e + 01时失败。无法满足整合   公差而不会将步长减小到最小值以下   允许(2.273737e-13)在时间t。

我在一个矩阵(很多输入)上运行ode45,所以我想找出自动哪些输入发生了上述情况(失败)。我的意思是,ode45 返回的这个条件是否有任何其他迹象可以自动写入数组?可以在if声明中使用的内容:

if {some variable is returned/is equal to ...} then {the solver has failed}

自动识别错误的输入,而不查看显示的警告。

2 个答案:

答案 0 :(得分:3)

您可以将warning变为errortry/catch块可以捕获错误

一个例子:

% 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);

您可以通过warnId命令获取任何警告的lastwarn。如有错误,请参阅lasterr

答案 1 :(得分:0)

另一种方法是检查ode45能够运行多长时间。例如,如果你运行

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

然后执行此行后,只需检查t(end)的值。如果t(结束)== tf,则ode45完成其工作,否则出现故障