Gauss-Seidel code gives wrong answers

2 Ansichten (letzte 30 Tage)
kemgi
kemgi am 17 Apr. 2020
Kommentiert: Mohammad shadab am 17 Okt. 2023
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)

Antworten (1)

Prabhan Purwar
Prabhan Purwar am 22 Apr. 2020
Hi,
Seems duplicate questions are posted by mistake, kindly refer to ans using following link:
Thanks.

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by