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

Risposte (1)

Walter Roberson
Walter Roberson il 18 Feb 2017
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.

Categorie

Scopri di più su Mathematics in Centro assistenza e File Exchange

Prodotti

Richiesto:

il 18 Feb 2017

Risposto:

il 18 Feb 2017

Community Treasure Hunt

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

Start Hunting!

Translated by