Professional Documents
Culture Documents
gauss siedel
clear all
clc
A = [12 -2 3 4 ; 1 10 -2 5 ; 3 1 15 6 ; 1 2 3 4];
B = [18 ; 16 ; 52 ; 43];
[N,~]=size(A)
X = zeros(N,1);
e = ones(N,1);
C_n=10^-6
iteration = 0;
while max(e) > C_n%check error
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,1) = (B(i,1) - sum(A(i,j) * Xtemp)) / A(i,i);
end
Xs = X;
e = sqrt((X - Z).^2);
end
%%Display Results
Xs
iteration