You are on page 1of 1

mundur

clear all maju


clc clear all
a = [12 -2 3 4 ; 1 10 -2 5 ; 3 1 15 6 ; 1 2 3 4]; clc
b = [18 ; 16 ; 52 ; 43]; a = [12 -2 3 4 ; 1 10 -2 5 ; 3 1 15 6 ; 1 2 3 4];
[n,~]=size(a) b = [18 ; 16 ; 52 ; 43];
x=zeros(n,1) [n,~]=size(a)
x=zeros(n,1)
for i=1:n-1 for i=n:-1:2
m=a(i+1:n,i)/a(i,i); m=a(i-1:-1:1,i)/a(i,i);
a(i+1:n,:)=a(i+1:n,:)-m*a(i,:); a(i-1:-1:1,:)=a(i-1:-1:1,:)-m*a(i,:);
b(i+1:n,:)=b(i+1:n,:)-m*b(i,:); b(i-1:-1:1,:)=b(i-1:-1:1,:)-m*b(i,:);
end end
a a
b b
x(n,:)=b(n,:)/a(n,n); x(1,:)=b(1,:)/a(1,1);
for i=n-1:-1:1 for i=2:n
x(i,:)=(b(i,:)-a(i,i+1:n)*x(i+1:n,:))/a(i,i); x(i,:)=(b(i,:)-a(i,i-1:-1:1)*x(i-1:-
end 1:1,:))/a(i,i);
x end
x
jacoby
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)
x0=zeros(n,1)
tol=(10)^(-6)
for j = 1 : n
x(j) = ((b(j) - a(j,[1:j-1,j+1:n]) * x0([1:j-1,j+1:n])) / a(j,j));
end
x1 = x';
k = 1;
while norm(x1-x0,1) > tol
for j = 1 : n
x_ny(j) = ((b(j) - a(j,[1:j-1,j+1:n]) * x1([1:j-1,j+1:n])) / a(j,j));
end
x0 = x1;
x1 = x_ny';
k = k + 1;
end
k
x = x1

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

You might also like