Gauss- Seidel Error tolerance

18 views (last 30 days)
Muhammad Mubinur Rahman
Muhammad Mubinur Rahman on 10 Sep 2021
I am using this code below to implement gauss-seidel method equation solving. I did set a maximum error value i.e tol in my code. The code should run as long as current error is greater than maximum error. However for tol = 10^-5 , A = [8 -3 2; 4 11 -1; 6 3 12] and b = [20;33;35], the while loop breaks even when the condition is still not brached i.e current error is still greater than tol. Why is this happening so? It makes sense if I consider first 5 decimal points but I do not intend to limit it to five decimal points only.
function[X,err] = gauss_seidel(A,b)
tol = 10^-05;
[M,N] = size(A);
if(M~=N)
错误('Error');
end
fori=1:M
row = abs(A(i,:));
d = sum(row) - row(i);
ifd>=row(i)
错误("Given matrix is not diagonally dominant");
end
end
itr = 0;
X = zeros(M,1);
err = inf;
whileerr>tol
Xold = X;
fori = 1:M
total = 0;
forj = 1:i-1
总计=总 + a(i,j)*x(j);
end
forj = i+1 : N
total = total + A(i,j)*Xold(j);
end
X(i) = (1/A(i,i)) * (b(i)-total);
end
itr = itr+1;
err = abs(Xold-X);
end
fprintf("%.10f ",tol);
fprintf(“%.10f”,err);
fprintf("Method converges in %d iteration\n",itr);
end

Answers (1)

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh on 11 Sep 2021
Edited:Abolfazl Chaman Motlagh on 11 Sep 2021
you have an unclear statement in while.after first iteration the err IS是3x1矢量,TOL是标量。
change the statement to a norm of vector err . for example inf-norm:
whilemax(err)>tol
...
end

s manbetx 845


发布

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by