Rank deficient error?
이전 댓글 표시
I'm trying to write a code for iterative Jacobi method. I thought I had worked out all the bugs but now I'm getting the following error when I try to run my function. > In jacobi (line 8) Warning: Rank deficient, rank = 0, tol = 0.000000e+00. Any help would be appreciated. Here's my code
function y=jacobi(a,b)
x=zeros(length(b),1);
xprime=x;
amod=diag(diag(a));
for ii=1:length(b)
x=xprime;
x=amod*b-(amod*(a-amod)*x);
if abs(((x-xprime)/xprime))<=0.001
break
end
end
답변 (1개)
Walter Roberson
2017년 2월 18일
You initialize xprime as a vector of 0.
You do not change xprime anywhere in your code.
You have
(x-xprime)/xprime)
but xprime is still that vector of 0.
Remember that / is the mrdivide operator, which is similar to multiplying by the inverse matrix, similar to
(x-xprime) * inv(xprime)
but your xprime is all 0, so it has no inverse -- it is "rank deficient". (I am ignoring for a moment that xprime is not square and so inv() itself does not apply, as the / operator does not actually use inv() itself.)
If your xprime had been updated to contain a vector of non-trivial values, then the result of the / operator would be a length(b) by length(b) matrix. You take abs() of that matrix, and you test <=0.001 . The result of that is going to be a length(b) by length(b) matrix of true and false values. When you "if" a matrix of values, the result is only considered true if all of the entries are non-zero. So like
temp = abs(((x-xprime)/xprime)) <= 0.001;
if all(temp(:))
If you are certain that you want to be testing a matrix of values in an "if" then it is highly recommended that you specifically use all() so that people reading the code know that really is what you wanted and that you did not simply overlook the fact that you are testing a matrix.
카테고리
도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!