Gauss-Seidel method Help

228 vues (au cours des 30 derniers jours)
kemgi
kemgi le 18 Avr 2020
Hello,
I am hoping someone can assist me with this problem. I created a Gauss-Seidel code that will allow me to solve a set of linear equations, finding x1, x2 x3 and x4. I made two matrices; A=[4 -21 -7 1; -4 0 -3 11 ; 4 -1 10 -1; 151/8 5 8 -3] and b =[11; 15; 19; -12;]. Through using the equatio Ax=b i would be able to find the unknows, which worked using the backslash built in solver. But my code below give 4 values for x but they do not match the reuslts of the built in solver and do not equate when substitued back into a line of the linear equations.
A major aspect of the code is that it is meant to make your matrix diagonally dominant to solve.
My code is as follows:
function gauss-seidel
A=input('write matrix a')
b=input('write matrix b')
x=linspace(0,0,length(A))';
n=size(x,1);
normVal=Inf;
nmax=1000; %number of maximum iterations which can be reached%
tol=0.000001; % Tolerence for method%
iter=0;
while normVal>tol && iter<nmax
x_old=x;
[V,A]=eig(A);
TF=isdiag(A);
if TF==0
fprintf('the matrix is not diagonally dominant')
else
for i=1:n
guess=0;
for j=1:i-1
guess=guess+A(i,j)*x(j);
end
for j=i+1:n
guess=guess+A(i,j)*x_old(j);
end
x(i)=(b(i)+guess)/(A(i,i));
end
iter=iter+1;
normVal=norm(x_old-x);
end
end
fprintf('Solution of the system is : ')
for i=1:length(x)
fprintf(' %1.4f ',x(i));
end
fprintf('in %d iterations ',iter)
  4 commentaires
John D'Errico
John D'Errico le 11 Nov 2021
If you made a mistake, it is trivial to edit your question to fix it. But many people seem to post the same question as many as 5 or 6 times. If they don't get an answer quickly enough to suit their needs, they just keep posting the same question.
Mahmoud
Mahmoud le 7 Mar 2023
Writing the negative exponent to give the same result as the calculator. Example: 0.3000 * 10^-3 gives a result of 3 * 10^-4
But in MATLAB it gives a very different result. What is the problem here or how to write the equation correctly to give the same value as the calculator

Connectez-vous pour commenter.

Réponse acceptée

Prabhan Purwar
Prabhan Purwar le 22 Avr 2020
Hi,
Refer to the following code:
The following code performs Gauss-Seidel...
clc
clear
close all
A=[5 -2 3 0 6; -3 9 1 -2 7.4; 2 -1 -7 1 6.7; 4 3 -5 7 9; 2 3.5 6.1 -4 -8.1];
b=[-1 2 3 0.5 3.1]';
x=linspace(0,0,length(A))';
n=size(x,1);
normVal=Inf;
nmax=1000; %number of maximum iterations which can be reached%
tol=1e-3; % Tolerence for method%
iter=0;
while normVal>tol && iter<nmax
x_old=x;
for i=1:n
guess=0;
for j=1:i-1
guess=guess+A(i,j)*x(j);
end
for j=i+1:n
guess=guess+A(i,j)*x_old(j);
end
x(i)=(1/A(i,i))*(b(i)-guess);
end
iter=iter+1;
normVal=norm(x_old-x);
end
fprintf('Solution of the system is : \n%f\n%f\n%f\n%f\n%f in %d iterations',x,iter);
Output:
0.551479
0.469276
-0.595161
-0.649082
-0.171448 in 86 iterations
Tips:
The convergence properties of the Gauss-Seidel method are dependent on the matrix A. Namely, the procedure is known to converge if either:
https://www.mathworks.com/help/matlab/ref/chol.html (chol, only for symmetric matrix) [~,p] = chol(A)
OR
(Diagonal Dominant)
The Gauss-Seidel method sometimes converges even if these conditions are not satisfied.
Although you can prove that the methods converge if A has all eigenvalues positive from the above conditions (but not the only convergence criterion).
Hope it helps!!
  3 commentaires
Prabhan Purwar
Prabhan Purwar le 23 Avr 2020
Kindly accept the answer if its helpful...
ahmed al-khazraji
ahmed al-khazraji le 2 Fév 2021
thanks for the hard work

Connectez-vous pour commenter.

Plus de réponses (1)

Teresa Matthews
Teresa Matthews le 1 Déc 2023
Modifié(e) : Teresa Matthews le 2 Déc 2023
You should update the values of x for all elements within the loop. However, to avoid overwriting the current element being updated, you can store the geometry dash world temporarily and then assign them to x after the loop completes.
function gauss-seidel
A=input('write matrix a')
b=input('write matrix b')
x=linspace(0,0,length(A))';
n=size(x,1);
normVal=Inf;
nmax=1000; %number of maximum iterations which can be reached%
tol=0.000001; % Tolerence for method%
iter=0;
while normVal>tol && iter<nmax
x_new = x;
[V,A]=eig(A);
TF=isdiag(A);
if TF==0
fprintf('the matrix is not diagonally dominant')
else
for i=1:n
guess=0;
for j=1:i-1
guess=guess+A(i,j)*x_new(j);
end
for j=i+1:n
guess=guess+A(i,j)*x(j);
end
x_new(i)=(b(i)+guess)/(A(i,i));
end
x = x_new;
iter=iter+1;
normVal=norm(x-x_new);
end
end
fprintf('Solution of the system is : ')
for i=1:length(x)
fprintf(' %1.4f ',x(i));
end
fprintf('in %d iterations ',iter)

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by