如何在不查看显示的警告的情况下意识到ode45的故障? [英] How to become aware of failure of ode45 without looking at the displayed warning?

查看:246
本文介绍了如何在不查看显示的警告的情况下意识到ode45的故障?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当ODE45的解出现分歧时(无论原因和方式如何),将显示以下警告,并且求解器无法继续:

警告:在t = 8.190397e + 01时失败.无法满足整合 公差不降低步长小于最小值 在时间t允许(2.273737e-13).

我正在矩阵(大量输入)上运行ode45,因此我想自动找出 发生上述条件(失败)的输入.我的意思是,ode45 返回的这种情况是否还有其他迹象可以自动写入数组?可以在if陈述中用作的内容:

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

自动识别那些故障输入,而无需查找显示的警告.

解决方案

您可以将warning转换为error,并且错误可以被try/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);

您可以通过lastwarn命令获得任何警告的warnId.有关错误,请参见lasterr.

When the solution of ODE45 diverges (doesn't matter why and how), the following warning will be displayed, and the solver can not continue:

Warning: Failure at t=8.190397e+01. Unable to meet integration tolerances without reducing the step size below the smallest value allowed (2.273737e-13) at time t.

I am running ode45 on a matrix (lots of inputs), so I want to find out automatically for which inputs the above condition(failure) happens. I mean, Is there any other sign of this condition returned by ode45 that can be written in an array automatically? Something that can be used in a if statment as:

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

to identify those faulty inputs automatically without looking for the displayed warning.

解决方案

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.

这篇关于如何在不查看显示的警告的情况下意识到ode45的故障?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆