% 5-component example
clear all

A=[4 8 0 -4 1; 3 2 1 0 2; 1 3 1 1 -3;-2 1 5 6 0; 12 -5 -2 -1 3]

b=[-1; -4; 11; 5; 3]

M=[A b]

% M(1,1) as pivot
M(2,:)=M(2,:)-3/4*M(1,:)

M(3,:)=M(3,:)-1/4*M(1,:)

M(4,:)=M(4,:)+1/2*M(1,:)

M(5,:)=M(5,:)-3*M(1,:)

% M(2,2) as pivot
M(3,:)=M(3,:)+1/4*M(2,:)

M(4,:)=M(4,:)+5/4*M(2,:)

M(5,:)=M(5,:)-29/4*M(2,:)

% M(3,3) as pivot
M(4,:)=M(4,:)-6.25/1.25*M(3,:)

M(5,:)=M(5,:)+9.25/1.25*M(3,:)

% M(4,4) as pivot
M(5,:)=M(5,:)+9.6/6*M(4,:)

% upper triangular coefficient matrix
U=M(:,1:5)

% new rhs vector 
newb=M(:,6)

% initialize solution x as a column vector of the correct length
x=zeros(5,1);

% back substitution
x(5)= newb(5)/U(5,5)

x(4)=( newb(4) - U(4,5)*x(5) )/U(4,4)

x(3)=( newb(3) - U(3,5)*x(5) - U(3,4)*x(4) )/U(3,3)

x(2)=( newb(2) - U(2,5)*x(5) - U(2,4)*x(4) - U(2,3)*x(3) )/U(2,2)

x(1)=( newb(1) - U(1,5)*x(5) - U(1,4)*x(4) - U(1,3)*x(3) - U(1,2)*x(2) )/U(1,1)


