In: Mechanical Engineering
Problem 1:
Carry out the first three iterations of the solution of the following set of equations using the Gauss Seidel iterative method.Provide the solution using a program, you are free to use any language including MATLAB.
8x1+2x2+3x3 = 51
2x1+5x2+x3 = 23
-3x1+x2+6x3 = 20
clc
clear all
close all
clear;clc
format compact
A = [8 2 3;
2 5 1;
-3 1 6];% coefficients matrix
C = [51;23;20];% constants vector
n = length(C);
X = zeros(n,1);
Error_eval = ones(n,1);
for i = 1:n
j = 1:n;
j(i) = [];
B = abs(A(i,j));
Check(i) = abs(A(i,i)) - sum(B); % Is the diagonal value greater than the remaining row values combined?
if Check(i) < 0
fprintf('The matrix is not strictly diagonally dominant at row %2i\n\n',i)
end
end
iteration = 0;
while max(Error_eval) > 0.001
iteration = iteration + 1;
Z = X; % save current values to calculate error later
for i = 1:n
j = 1:n; % define an array of the coefficients' elements
j(i) = []; % eliminate the unknow's coefficient from the remaining coefficients
Xtemp = X; % copy the unknows to a new variable
Xtemp(i) = []; % eliminate the unknown under question from the set of values
X(i) = (C(i) - sum(A(i,j) * Xtemp)) / A(i,i);
end
Xsolution(:,iteration) = X;
Error_eval = sqrt((X - Z).^2);
end
GaussSeidelTable = [1:iteration;Xsolution]'
MaTrIx = [A X C]
Result