1. mathlab--jacobi iteration method

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

2. 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

3. first ı want to thank you captain,i have been searching this homework for about a week and recently i tried your code but it gives me an error like this ı don't know what to do;
Code:
??? function [x] = gsmp(A,b,n,z)
|
Error: Function definitions are not permitted at the prompt or in scripts.
but ı found another solution,but it is possible that my teacher won't accept it becouse she wants me to write a script and ı really going to be crazy ı have more exams to study whatever ı am givin my solution her check it please;
Code:
>> M = [5 -5 1; 2 8 -1; -1 1 4];
b = [10 11 3]';
D = diag( M ).^-1

D =

0.2000
0.1250
0.2500

>> Moff = M - diag( diag( M ) )

Moff =

0    -5     1
2     0    -1
-1     1     0

>> x = [0, 0, 0]'

x =

0
0
0

>> for i = 1:100
xold = x;

for j = 1:3
x(j) = D(j)*(b(j) - Moff(j, :)*x);
end

if norm( x - xold ) < 0.001
break;
end
end
>> i

i =

9

>> x

x =

2.6284
0.8667
1.1904

4. Originally Posted by sah_mat
first ı want to thank you captain,i have been searching this homework for about a week and recently i tried your code but it gives me an error like this ı don't know what to do;
Code:
??? function [x] = gsmp(A,b,n,z)
|
Error: Function definitions are not permitted at the prompt or in scripts.
You put the function definition in a file called gsmp.m on the search path, or add the directory with it to the search path.

Also if a script file is required you put:

Code:
A=[5,-1,1;2,8,-1;-1,1,4]
b=[10;11;3]
x=gsmp(A,b,20,[1;1;1])
in a file in the same directory and call it something like script.m and invoke it at the command prompt with:

Code:
>>script
CB

5. ı appreciate you captain blac for your helps ı tried your last advice and it realyy works,then ı send it to my teacher she said 'this m.file is too professional probably u did not write it ı want a simple one which u can write'' so ı want u too ask a more question captain ı wrote a solution code at my previous message can we write a gsmp.m file with using the codes in that solution if u help me ı really be glad ı have to study my other exams ı think i am in trouble with the mathlab.THANKS for your helps captain black.

6. Originally Posted by sah_mat
ı appreciate you captain blac for your helps ı tried your last advice and it realyy works,then ı send it to my teacher she said 'this m.file is too professional probably u did not write it ı want a simple one which u can write'' so ı want u too ask a more question captain ı wrote a solution code at my previous message can we write a gsmp.m file with using the codes in that solution if u help me ı really be glad ı have to study my other exams ı think i am in trouble with the mathlab.THANKS for your helps captain black.
Put this in a script file:

Code:
M = [5 -5 1; 2 8 -1; -1 1 4];
b = [10 11 3]';
D = diag( M ).^-1

Moff = M - diag( diag( M ) )

x = [0, 0, 0]'

for i = 1:100
xold = x;

for j = 1:3
x(j) = D(j)*(b(j) - Moff(j, :)*x);
end

if norm( x - xold ) < 0.001
break;
end
end

disp( i)

disp( x)
CB

7. it is really working thanks a lot CB.ı think ı can implement the same steps for solving with gauss-seidel method.