S Use the Gauss-Seidel iteration technique with no relaxation to solve. 12x, +3x
ID: 3281777 • Letter: S
Question
S Use the Gauss-Seidel iteration technique with no relaxation to solve. 12x, +3x+2x,-35 3x 82 6x11 4x1 + 2x2 + 15x,--26 Solve until the approximate percent relative error falls below 5-5% or with no more than five iterations. Do not use the function. Use an initial guess of 1 for each variable. Show the initial guess and, after each iteration, the approximate solution and APRE (no fprint required, just omit the semicolon at the end of each calculation. Use an fprintf statement to display how many iterations it took for 5%.Explanation / Answer
Save code in gauss_siedel.m file and run gauss_siedel
###################################
clear;clc
format compact
%% Read or Input any square Matrix
A = [12 3 2;
3 8 6;
4 2 15;];% coefficients matrix
C = [-35;-11;-26];% constants vector
n = length(C);
X = zeros(n,1);
Error_eval = ones(n,1);
%% Check if the matrix A is diagonally dominant
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 ',i)
end
end
%% Start the Iterative method
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
%% Display Results
GaussSeidelTable = [1:iteration;Xsolution]'
Matrix = [A X C]
##############################
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.