Supress Warning in a Loop

5 次查看(过去 30 天)
ercan duzgun
ercan duzgun 2021-2-24
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

回答(1 个)

Walter Roberson
Walter Roberson 2021-2-24
X=X-inv(j)*f;
replace with
if rank(j)<length(j)
X = nan;
else
X = X - j\f;
end
  3 个评论
Walter Roberson
Walter Roberson 2021-2-24
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by