Matrix Laboratory Manual
LAB 09) Solution of system of linear equations using Gauss-Seidel iteration.
Algorithm
Step 1: Start
Step 2: Declare the variables (syms x y z )
Step 3: Read given equations
Step 4: Formulate the coefficients of system of equations in matrix
form (A & B) (using the "equationToMatrix" function)
Step 5: Define the number of iterations to apply & absolute relative
approximate error
Step6: Calculate length of second matrix(B)
Step 7: Apply the Gauss-Seidel iteration formula for the specified
number of iterations.
Step 8: Store the iteration in matrix form
Step 9: Check for the absolute relative approximate error
Step10: If iteration value is less than approximate error go to step 7
Step 11: Display the Solution
Step12: End
syms x y z
e1=2*x + y + z == 5
e2=3*x + 5*y +2*z == 15
e3=2*x + y + 4*z == 8
% Define the coefficient matrix
[A,B]=equationsToMatrix([e1,e2,e3],[x,y,z]);
P=[A,B]
tolerance = 0.000001 % absolute relative approximate error
n = 10 % Define the number of iterations to apply
N = length(B);
X = zeros(N,1) ;
M =zeros(N,N);
Y = zeros(N,1) ;
index=1;
% Apply the Gauss-Seidel iteration formula for the specified number of
iterations
for j=1:n
for i = 1:N
X(i) = (B(i) - A(i,1:i-1)*X(1:i-1) - A(i,i+1:end)*X(i+1:end)) / A(i,i);
Department of Mathematics KLE Institute of Technology
Matrix Laboratory Manual
end
if abs(Y-X)<tolerance
break
end
M(index,:)=X;
index=index+1;
Y=X;
end
% Display the solution
V={'X', 'Y', 'Z'};
T = array2table(M,'VariableNames',V)
Output:
e1 =
e2 =
e3 =
P=
tolerance = 1.0000e-06
n = 10
T = 10×3 table
X Y Z
1 2.5000 1.5000 0.3750
2 1.5625 1.9125 0.7406
3 1.1734 1.9997 0.9134
4 1.0435 2.0086 0.9761
5 1.0077 2.0050 0.9949
6 1.0001 2.0020 0.9995
7 0.9993 2.0007 1.0002
8 0.9996 2.0002 1.0002
9 0.9998 2 1.0001
10 0.9999 2 1
Department of Mathematics KLE Institute of Technology
Matrix Laboratory Manual
Example 2:- Apply Gauss-seidel iteration method to solve the system of
equations
x + y + 54z = 110
27x + 6y –z = 85
6x + 15y + 2z = 77
syms x y z
e1=x + y + 54*z == 110
e2=27*x + 6*y -z == 85
e3=6*x + 15*y + 2*z == 77
% Define the coefficient matrix
[A,B]=equationsToMatrix([e1,e2,e3],[x,y,z]);
P=[A,B]
tolerance = 0.000001 %
n = 10 % Define the number of iterations to apply
N = length(B);
X = zeros(N,1) ;
M =zeros(N,N);
Y = zeros(N,1) ;
index=1;
% Apply the Gauss-Seidel iteration formula for the specified number of
iterations
for j=1:n
for i = 1:N
X(i) = (B(i) - A(i,1:i-1)*X(1:i-1) - A(i,i+1:end)*X(i+1:end)) / A(i,i);
end
% Display the solution
if abs(Y-X)<tolerance
break
end
M(index,:)=X();
index=index+1;
Y=X;
Department of Mathematics KLE Institute of Technology
Matrix Laboratory Manual
end
V={'x', 'y','z'};
T = array2table(M,'VariableNames',V)
Output
e1 =
e2 =
e3 =
P=
tolerance = 1.0000e-06
n = 10
T = 10×3 table
x y z
1 110 -480.8333 3.3148e+03
2 -1.7841e+05 8.0339e+05 -5.4902e+06
3 2.9567e+08 -1.3314e+09 9.0986e+09
4 -4.8999e+11 2.2065e+12 -1.5079e+13
5 8.1204e+14 -3.6567e+15 2.4989e+16
6 -1.3458e+18 6.0601e+18 -4.1413e+19
7 2.2303e+21 -1.0043e+22 6.8632e+22
8 -3.6961e+24 1.6644e+25 -1.1374e+26
9 6.1254e+27 -2.7583e+28 1.8850e+29
10 -1.0151e+31 4.5712e+31 -3.1239e+32
Department of Mathematics KLE Institute of Technology
Matrix Laboratory Manual
Example 2:- Apply Gauss-seidel iteration method to solve the system of
equations 2x+5y=16 and 3x+y=11
syms x y z
e1=2*x+5*y==16
e2=3*x+y==11
% Define the coefficient matrix
[A,B]=equationsToMatrix([e1,e2],[x,y]);
P=[A,B]
tolerance = 0.000001 %
n = 10 % Define the number of iterations to apply
N = length(B);
X = zeros(N,1) ;
M =zeros(N,N);
Y = zeros(N,1) ;
index=1;
% Apply the Gauss-Seidel iteration formula for the specified number of
iterations
for j=1:n
for i = 1:N
X(i) = (B(i) - A(i,1:i-1)*X(1:i-1) - A(i,i+1:end)*X(i+1:end)) / A(i,i);
end
% Display the solution
if abs(Y-X)<tolerance
break
end
M(index,:)=X();
index=index+1;
Y=X;
end
V={'x', 'y'};
T = array2table(M,'VariableNames',V)
Department of Mathematics KLE Institute of Technology
Matrix Laboratory Manual
Output
e1 =
e2 =
P=
tolerance = 1.0000e-06
n = 10
T = 10×2 table
x y
1 8 -13
2 40.5000 -110.5000
3 284.2500 -841.7500
4 2.1124e+03 -6.3261e+03
5 1.5823e+04 -4.7459e+04
6 1.1866e+05 -3.5596e+05
7 8.8990e+05 -2.6697e+06
8 6.6742e+06 -2.0023e+07
9 5.0056e+07 -1.5017e+08
10 3.7542e+08 -1.1263e+09
Department of Mathematics KLE Institute of Technology
Matrix Laboratory Manual
Example 4:- Apply Gauss-seidel iteration method to solve the system of
equations
5x + 2y + z = 12
x + 4y + 2z = 15
x + 2y + 5z = 20
Code:
syms x y z
e1=5*x + 2*y + z == 12
e2=x + 4*y + 2*z == 15
e3=x + 2*y + 5*z == 20
% Define the coefficient matrix
[A,B]=equationsToMatrix([e1,e2,e3],[x,y,z]);
P=[A,B]
tolerance = 0.000001 %
n = 10 % Define the number of iterations to apply
N = length(B);
X = zeros(N,1) ;
M =zeros(N,N);
Y = zeros(N,1) ;
index=1;
% Apply the Gauss-Seidel iteration formula for the specified number of
iterations
for j=1:n
for i = 1:N
X(i) = (B(i) - A(i,1:i-1)*X(1:i-1) - A(i,i+1:end)*X(i+1:end)) / A(i,i);
end
% Display the solution
if abs(Y-X)<tolerance
break
end
M(index,:)=X();
index=index+1;
Y=X;
end
V={'x', 'y','z'};
T = array2table(M,'VariableNames',V)
Department of Mathematics KLE Institute of Technology
Matrix Laboratory Manual
Output:
e1 =
e2 =
e3 =
P=
tolerance = 1.0000e-06
n = 10
T = 10×3 table
x y z
1 2.4000 3.1500 2.2600
2 0.6880 2.4480 2.8832
3 0.8442 2.0974 2.9922
4 0.9626 2.0132 3.0022
5 0.9943 2.0003 3.0010
6 0.9997 1.9996 3.0002
7 1.0001 1.9999 3
8 1.0001 2 3
9 1 2 3
10 1 2 3
Department of Mathematics KLE Institute of Technology