Gauss-Seidel method not converging/throwing error?
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I wrote a code for the Gauss-Seidel iterative method. I need to use the method to solve two different systems. For system 1, the code works perfectly. For system 2, I get the following error:
Warning: Rank deficient, rank = 0, tol = NaN.
> In gauss2 at 10
and then it has Inf then NaN for some of entries in the output matrix. Can anyone tell me why this is happening?! I also attached the matrices. Here's my code.
function F=gauss2(A,b,x0,tol,imax)
B=length(b);
x=zeros(length(b),1);
for j=1:imax
for k=1:B
x(k)=1/A(k,k)*(-A(k,1:k-1)*x(1:k-1)-A(k,k+1:B)*x0(k+1:B)+b(k));
end
if all(abs((x-x0)./x))<tol;
break
else
x0=x;
end
end
soln=x
end
0 comentarios
Respuestas (1)
John D'Errico
el 20 de Feb. de 2016
Is x a vector? (Yes. I know it is. I want you to think about the implications of that fact.)
What do you think this test as you wrote it does, when x and x0 are vectors?
if abs((x-x0)/x)<tol
You want to learn about the difference between / and ./, for that read
help slash
doc slash
I think you wanted to use element-wise division (i.e., ./), not the slash (/) that uses linear algebra.
Finally, you need to consider why this test did what it did:
if 0 == [2 0 1]
disp('they sky is falling')
else
disp('all is good in the world')
end
The result will be:
all is good in the world
Since one of the elements in that vector WAS identically zero, the first brach should have been taken, or NOT? Why not?
An if statement, when applied to a vector like that, uses only the very first element of that vector. If you want to test if all of any of the elements of the vector are true, then you need to use the functions ALL or ANY. For example, write it like this:
if any(0 == [2 0 1])
disp('they sky is falling')
else
disp('all is good in the world')
end
Now the first branch will execute because at least ONE of the elements tested to be zero.
help all
help any
Finally, as for your actual question, as to why inf or nan is occurring at line 21, your function does not actually have 21 lines in it. So you have not even given us the function you are actually calling. The crystal ball is so foggy on that question, since I cannot read your mind.
2 comentarios
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!