当 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);
Run Code Online (Sandbox Code Playgroud)
您可以warnId通过该lastwarn命令获取任何警告。对于错误,请参阅lasterr。
| 归档时间: |
|
| 查看次数: |
3022 次 |
| 最近记录: |