Originally Posted by

**sah_mat** how can ı solve this linear system with Gauss-seidel iteration method using mathlab codes??

5x - y + z=10

2x + 8y -z=11

-x + y + 4z=3

ı have some codes but how can ı implement on this question actually ı don't know please somebody help me.i am giving the codes;

function [x,G,c] = gsmp(A,b,n,z)

%

% x = gsmp(A,b,n,z)

%

% Gauss-Seidel iteration on system A*x = b with printing

% using matrix multiplication (not optimal)

% n -- number of iterations

% z -- initial vector (default 0)

%

% x -- final iterate

% G -- Gauss-Seidel matrix

% c -- Gauss-Seidel vector

%

if nargin <=3, z=0*b; end

LD = tril(A);

G = -LD\triu(A,1);

c = LD\b;

x=z;

for i = 1:n

x = G*x + c;

fprintf(1,'%3d ',i)

fprintf(1,'%5.5f ',x')

fprintf(1,'\n')

end

Try this:

Code:

function [x] = gsmp(A,b,n,z)
%
% x = gsmp(A,b,n,z)
%
% Gauss-Seidel iteration on system A*x = b
%
% n -- number of iterations
% z -- initial vector (default 0)
%
% x -- final iterate
if nargin <=3, z=0*b; end
L = tril(A); %lower triangular part of A
U = triu(A); %upper triangular part of A
D = diag(diag(A)); %diag matrix with same diag as A
x=z;
for i = 1:n
x =(D+L)\(b-U*x);
disp([i,x'])
end

with calling code:

Code:

A=[5,-1,1;2,8,-1;-1,1,4]
b=[10;11;3]
x=gsmp(A,b,20,[1;1;1]);

Reference

CB