You are on page 1of 1

%-------------------------------------------------------%Metode Eleminasi Gaus

%-------------------------------------------------------clc;
clear all;
%b = [8 -3 2; -3 10 -1; 2 -1 12];
b = [20; 10; 16];
n = length(b);
for i=1:n
for j=1:n
x=sprintf('A(%g,%g) : ',i,j);
A(i,j)=input(x);
end;
end;
x = Gauss(A, b);
x

function x = Gauss(A, b)
% Solve linear system Ax = b
% using Gaussian elimination without pivoting
% A is an n by n matrix
% b is an n by k matrix (k copies of n-vectors)
% x is an n by k matrix (k copies of solution vectors)
[n,
[n,
x =
for

n] = size(A);
% Find size of matrix A
k] = size(b);
% Find size of matrix b
zeros(n,k);
% Initialize x
i = 1:n-1
m = -A(i+1:n,i)/A(i,i); % multipliers
A(i+1:n,:) = A(i+1:n,:) + m*A(i,:);
b(i+1:n,:) = b(i+1:n,:) + m*b(i,:);

end;
% Use back substitution to find unknowns
x(n,:) = b(n,:)/A(n,n);
for i = n-1:-1:1
x(i,:) = (b(i,:) - A(i,i+1:n)*x(i+1:n,:))/A(i,i);
end

You might also like