当ODE45的解发散时(不管为什么发散,如何发散),会出现如下警告,求解器无法继续:
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.
我在矩阵(大量输入)上运行 ode45,所以我想自动找出发生上述情况(失败)的输入。我的意思是,ode45 返回的这种情况是否有任何其他迹象可以自动写入数组?可以在 if
语句中使用的东西:
如果{返回某个变量/等于...},则{求解器失败}
自动识别那些错误的输入,而无需查找显示的警告。
最佳答案
您可以将那个警告
变成一个错误
,错误可以被try/catch
block 捕获.
一个例子:
% 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
。
关于matlab - 如何在不查看显示的警告的情况下意识到 ode45 的故障?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17537254/