A=[1 2 3 4;
3 4 8 9;
8 20 4 3;
5 6 7 8];
C=[3;4;8;10];
newA=GaussElimination(A,C);
[n,n]=size(A);
Aprime=newA(1:n,1:n)
Cprime=newA(:,n+1)
x=UpperTriangularSolver(Aprime,Cprime)
function newA=GaussElimination(A,C)
%Augment A and C
[n,n]=size(A);
newA=[A C];
for k=1:n-1
for i=k+1:n
factor=newA(i,k)/newA(k,k);
for j=k:n+1
newA(i,j)=newA(i,j)-factor*newA(k,j);
end
end
end
end
function x=UpperTriangularSolver(A,C)
[n,n]=size(A) % Finding the size of the problem
x=zeros(n,1) % set up x vector with elements initially set to zero
x(n)=C(n)/A(n,n);
for i=n-1:-1:1
sum=0;
for j=i+1:n
sum=sum+A(i,j)*x(j);
end
x(i)=(C(i)-sum)/A(i,i);
end
end