Solve using matlab code For the Axb matrix problem below com
Solution
Matlab script for Solving Ax=b using Jacobi\'s method
clear all; %clc; % clearing the command window and workspace
% Enter the Coefficient matrix
A = [4 -1 -1 0;-1 4 0 -1;-1 0 4 -1;0 -1 -1 4];
% Enter the RHS vector
b = [1 2 0 1]\';
% Number of unknowns
NUn = size(A,1);
% Number of Equations
NEq = size(A,2);
% Check number of unknowns <= number of equations
if(NUn>NEq)
message(\'Warning, The No. of unknowns should not be greater than No. equations\ \');
exit; % exit the program for a true conditions
end
% Create variable to stor unknowns
X = zeros(NUn,1); % colume vector
% Decomposing the A matrix in to D,L and U
D = diag(diag(A));
L = tril(A,-1);
U = triu(A,1);
LU = D\\(-L-U);
b = D\\b;
fprintf(\'Iteration \\t\\t\\t x \\t\\t\\t\\ty\\t\\tz\ \'); % Printing the table heading
fprintf(\'%d \\t\\t %f \\t\\t%f\\t\\t%f\ \',0,X(1),X(2),X(3)); % printing initial guess
ext = 1; % loop exiting flag
while ext %max(Error_eval) > 0.001
Xold = X; % save current values to calculate error later
X = (LU*X+b);
fprintf(\'%d \\t\\t %f \\t\\t%f\\t\\t%f\ \',ext,X(1),X(2),X(3)); % printing the iteration values
ext = ext + 1; % incrementing the iterative index
if( norm(X - Xold) < 10^-6) ext =0; end % Exit the loop with a ginven accuracy
end
OUTPUT of the Matlab program ( Jacobi\'s method)
Iteration x y z
0 0.000000 0.000000 0.000000
1 0.250000 0.500000 0.000000
2 0.375000 0.625000 0.125000
3 0.437500 0.687500 0.187500
4 0.468750 0.718750 0.218750
5 0.484375 0.734375 0.234375
6 0.492188 0.742188 0.242188
7 0.496094 0.746094 0.246094
8 0.498047 0.748047 0.248047
9 0.499023 0.749023 0.249023
10 0.499512 0.749512 0.249512
11 0.499756 0.749756 0.249756
12 0.499878 0.749878 0.249878
13 0.499939 0.749939 0.249939
14 0.499969 0.749969 0.249969
15 0.499985 0.749985 0.249985
16 0.499992 0.749992 0.249992
17 0.499996 0.749996 0.249996
18 0.499998 0.749998 0.249998
19 0.499999 0.749999 0.249999
20 0.500000 0.750000 0.250000
Matlab Program for Solving Ax=b using Gauss-Seidel method
clear all; clc; % clearing the command window and workspace
% Enter the Coefficient matrix
A = [4 -1 -1 0;-1 4 0 -1;-1 0 4 -1;0 -1 -1 4];
% Enter the RHS vector
b = [1 2 0 1]\';
% Number of unknowns
NUn = size(A,1);
% Number of Equations
NEq = size(A,2);
% Check number of unknowns <= number of equations
if(NUn>NEq)
message(\'Warning, The No. of unknowns should not be greater than No. equations\ \');
exit; % exit the program for a true conditions
end
% Create variable to stor unknowns
X = zeros(NUn,1); % colume vector
% Check if the matrix A is diagonally dominant
for l = 1:NUn
k = 1:NUn;
k(l) = [];
if abs(A(l,l)) - sum(abs(A(l,k))) < 0 % checking diagonal value greater than the remaining row sum values
fprintf(\'The matrix is not strictly diagonally dominant at row %2i\ \ \',l)
end
end
fprintf(\'Iteration \\t\\t\\t x \\t\\t\\t\\ty\\t\\tz\ \'); % Printing the table heading
fprintf(\'%d \\t\\t %f \\t\\t%f\\t\\t%f\ \',0,X(1),X(2),X(3)); % printing initial guess
ext = 1; % loop exiting flag
while ext %max(Error_eval) > 0.001
Xold = X; % save current values to calculate error later
for l = 1:NUn
k = 1:NUn; % define an array of the coefficients\' elements
k(l) = []; % eliminate the unknow\'s coefficient from the remaining coefficients
Xtemp = X; % copy the unknows to a new variable
Xtemp(l) = []; % eliminate the unknown under question from the set of values
X(l) = (b(l) - sum(A(l,k) * Xtemp)) / A(l,l);
end
fprintf(\'%d \\t\\t %f \\t\\t%f\\t\\t%f\ \',ext,X(1),X(2),X(3)); % printing the iteration values
ext = ext + 1; % incrementing the iterative index
if( norm(X - Xold) < 10^-6) ext =0; end % Exit the loop with a ginven accuracy
end
OUTPUT of the Matlab program ( Gauss-Seidel method)
0 0.000000 0.000000 0.000000
1 0.250000 0.562500 0.062500
2 0.406250 0.703125 0.203125
3 0.476563 0.738281 0.238281
4 0.494141 0.747070 0.247070
5 0.498535 0.749268 0.249268
6 0.499634 0.749817 0.249817
7 0.499908 0.749954 0.249954
8 0.499977 0.749989 0.249989
9 0.499994 0.749997 0.249997
10 0.499999 0.749999 0.249999
11 0.500000 0.750000 0.250000
12 0.500000 0.750000 0.250000
Result:
From the output of these two programs it is clear that the Gauss-Seidel method converge fastly than Jacobi\'s method.


