Supress Warning in a Loop

I have a matrix table. For each row in the table, I am using Newton-Raphson iteration. And for some variables of the table, the iteration doesn't converge, and it gives warning errors. How can I supress these warning messages?
I receive this kind of message in command window :
Warning: Matrix is singular,
close to singular or badly
scaled. Results may be
inaccurate. RCOND = NaN.
> In
SP_FK_NewtonRaphson_function
(line 8)
In fonk_cemberYont_FK (line
72)
My Newton-Raphson function file is:
function [X,i]=SP_FK_NewtonRaphson_function(verilen_L,X0)
maxIter=1000; % maximum iteration
tolX=1e-6; % Tolerance for error
X=X0;
Xold=X0;
for i=1:maxIter
[f,j]=myfunction2(verilen_L,X);
X=X-inv(j)*f;
err(:,i)=abs(X-Xold);
Xold=X;
Tablo(:,i)=X;
if (err(:,i)<tolX)
break;
end
end
end

Respuestas (1)

Walter Roberson
Walter Roberson el 24 de Feb. de 2021

1 voto

X=X-inv(j)*f;
replace with
if rank(j)<length(j)
X = nan;
else
X = X - j\f;
end

3 comentarios

ercan duzgun
ercan duzgun el 24 de Feb. de 2021
If I write as you say, I have "error" message, in state of "warning " message.
I receive this eror message:
Index exceeds the number of array elements (1).
Error in myfunction2 (line 4)
ty=X(2);
Error in SP_FK_NewtonRaphson_function (line 8)
[f,j]=myfunction2(verilen_L,X);
Error in fonk_cemberYont_FK (line 72)
[Tablo_NR_FK_xyzABG(s,:),Tablo_NR_iter(s,:)]=SP_FK_NewtonRaphson_function(Tablo_L(:,1),[G_0(1:3,s);ABG_0(1:3,s)]);
But if I write my previous command, it execute the iteration well, however there are lots of warning messages for several iterations on the screen. Displaying the warning messages each time on sceen seems to be time consuming. Therefore I wanted to supress the warning messages.
@Walter Roberson, If I change it with your suggested code, the program stop with that error message.
And I want to ask isn't it very time consuming for the program to check your "if else" command. Because it is in a function file, and function file is in a loop for 1000 iterations. Checking the condition of X with if-else in a 1000 loop cycle isn't bad for the program? I am trying to learn, and know your comment. Thanks in advance.
Walter Roberson
Walter Roberson el 24 de Feb. de 2021
function [X,i]=SP_FK_NewtonRaphson_function(verilen_L,X0)
maxIter=1000; % maximum iteration
tolX=1e-6; % Tolerance for error
X=X0;
Xold=X0;
for i=1:maxIter
[f,j]=myfunction2(verilen_L,X);
if rank(j)<length(j)
X = nan(size(X0));
else
X = X - j\f;
end
err(:,i)=abs(X-Xold);
Xold=X;
Tablo(:,i)=X;
if (err(:,i)<tolX)
break;
end
end
end
And I want to ask isn't it very time consuming for the program to check your "if else" command.
You could improve the efficiency by quiting the for i loop when the situation is detected. Once you encounter an instability, you can never recover.
ercan duzgun
ercan duzgun el 24 de Feb. de 2021
@Walter Roberson Thank you very much .

Iniciar sesión para comentar.

Categorías

Más información sobre Networks en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Preguntada:

el 24 de Feb. de 2021

Comentada:

el 24 de Feb. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by