Clear Filters
Clear Filters

matrix gauss-seidel method

40 views (last 30 days)
주하
주하 on 17 Apr 2023
Answered: Piyush Patil on 27 Apr 2023
how to solve this problem? ...
clear
clc
%A*Xi=B matrix
%i want to know Xi.. Thank you
A=[20 15 10; -3 -2.249 7; 5 1 3];
B=[45; 1.751; 9];
Xi=[1; 0; 1];
e1=100; e2=100; e3=100;
n=3;
f=0;
Xu=Xi
while e1>0.1 && e2>0.1 && e3>0.1
for i=1:n
for j=1:n
if j==i
f=f
else
f=f+A(i,j)*Xi(j)
end
end
Xi(i)=(B(i)-f)/A(i,i)
e1=abs((Xu(1)-Xi(1))/Xu(1))*100
e2=abs((Xu(2)-Xi(2))/Xu(2))*100
e3=abs((Xu(3)-Xi(3))/Xu(3))*100
Xu=Xi
f=0
end
end
Xu
  1 Comment
Sam Chak
Sam Chak on 17 Apr 2023
Can you show the Gauss–Seidel equations or execution steps (algorithm) here for investigation purposes?

Sign in to comment.

Answers (1)

Piyush Patil
Piyush Patil on 27 Apr 2023
Hello,
You may refer to the following code to perform Gauss-Seidel Method -
clear
clc
A = [27 6 -1 ; 6 15 2 ; 1 1 54]
B = [85 ; 72 ; 110]
% You can try with this example also
% A = [12 3 -5 ; 1 5 3 ; 3 7 13]
% B = [1 ; 28 ; 76]
% A = [20 15 10 ; -3 -2.249 7 ; 5 1 3];
% B = [45 ; 1.751 ; 9];
x = linspace(0,0,length(A))'; % setting initial guess for 'x' values as 0
% x = [1;0;1] % we can also set initial guess to values of our own choice
n = size(x, 1);
normVal = Inf;
nmax = 10000; % setting maximum number of iterations to 10000
maxErr = 1e-3; % setting maximum error to 10^(-3)
itr = 0;
while normVal > maxErr && itr < nmax
x_old = x;
for i = 1:n
sum = 0;
for j = 1:i-1
sum = sum + A(i,j)*x(j);
end
for j = i+1:n
sum = sum + A(i,j)*x_old(j);
end
x(i) = (1/A(i,i)) * (B(i)-sum);
end
itr = itr + 1;
normVal = norm(x_old - x);
end
disp(x)
Please note that not all systems of equations will converge using the Gauss-Seidel Method.
Gauss-Seidel Convergence Theorem - If "A" is Diagonally Dominant, then the Gauss-Seidel method converges for any starting vector x. A sufficient, but not necessary condition.
Diagonally Dominant - A nxn matrix "A" is diagonally dominant if - and for atleast one "i".
In simple terms, the coefficient on the diagonal must be at least equal to the sum of the other coefficients in that row and at least one row with a diagonal coefficient greater than the sum of the other coefficients in that row.
In your case, the matrix "A" is not diagonally dominant matrix. So, either the matrix has no unique solution, or we need a better starting guess for our variables so that the matrix will converge.

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!