Catching a warning to avoid matrix singularity

10 ビュー (過去 30 日間)
Mostafa Nakhaei
Mostafa Nakhaei 2020 年 10 月 12 日
コメント済み: Mostafa Nakhaei 2020 年 10 月 14 日
My program calls a nested function. That nested fanction gets the 'number of iterations' as the input. Sometimes, the number of iterations is high and leads to singular matrix. I want to reduce the number of iterations if and only if the Matlab shows a singularity warning otherwise I want to leave it as high as possible. The reason is singluarity warning reduce the running time signifincatly (I prefer iteration as high as possible to increase the accuracy of my program).
Here is an example code
function some_out_put = main_function(x)
global iteration
some_out_put = my_nested_function(x, iteration) % the nested fuction that gets singular sometimes
end
%%%%%%%%% my main implementation %%%%%%%
global iteration
iteration = 100;
for i=1:1:1000
x = X(i) % X is a array of numbers
y(i) = main_function(x)
% I want to reduce iteration if warning occurs (iteration = iteration - 1) but I need a block to catch it.
end

採用された回答

Robert U
Robert U 2020 年 10 月 12 日
Hi Mostafa Nakhaei,
quite similar to what you describe is shown in the documentation: https://de.mathworks.com/help/matlab/ref/lastwarn.html?s_tid=srchtitle
In order to clear the lastwarn before checking your particular warning case, use
lastwarn('');
More important for further work: Do not use global variables if not absolutely necessary. There are hundreds of threads arguing global variable troubles that are easily avoided by not using global variables.
Kind regards,
Robert
  1 件のコメント
Mostafa Nakhaei
Mostafa Nakhaei 2020 年 10 月 14 日
Thank you Robert

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeParticle & Nuclear Physics についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by