A=[1 2 3 5;
0 8 -7 3;
0 0 -9 7
0 0 0 6];
C=[14;-26;12;3]
x=UpperTriangularSolver(A,C)
%Check that you have the right answer by calculating Ax-C=0, which should be
%very close to zero if Ax=C.
A*x-C
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