Solving Linear Equations by Reducing Matrices

Creating a augmented matrix

x+y-2z==1;
3x+2y+4z==-4;
4x+3y+3z==-4;

(*Given a system of linear equations.*)

M={{1,1,-2,1},{3,2,4,-4},{4,3,3,-4}}

(*We can take the coefficients of the equations and put it together into a list "M".*)

MatrixForm[M]

(*From the list we created, we can output it nicely in matrix form using MatrixForm[list].*)

Reducing the matrix manually using elementary row operations

(Subscript[M, [[2]]])=(Subscript[M, [[2]]])-3(Subscript[M, [[1]]]);
(Subscript[M, [[3]]])=(Subscript[M, [[3]]])-4(Subscript[M, [[1]]]);
Print[MatrixForm[M]];

(Subscript[M, [[2]]])=-(Subscript[M, [[2]]]);
Print[MatrixForm[M]];

(Subscript[M, [[1]]])=(Subscript[M, [[1]]])-(Subscript[M, [[2]]]);
(Subscript[M, [[3]]])=(Subscript[M, [[3]]])+(Subscript[M, [[2]]]);
Print[MatrixForm[M]];

(Subscript[M, [[1]]])=(Subscript[M, [[1]]])-8(Subscript[M, [[3]]]);
(Subscript[M, [[2]]])=(Subscript[M, [[2]]])+10(Subscript[M, [[3]]]);
Print[MatrixForm[M]];

Reducing the matrix using the function

M={{1,1,-2,1},{3,2,4,-4},{4,3,3,-4}}

MatrixForm[M]

RowReduces[A0_,n_]:=Module[ {M=A0,i,j}, Print[MatrixForm[M]]
For[ j=1, j≤n, j++, Print["Row",j," = Row",j," / (",(Subscript[M, [[j,j]]]),")"];
(Subscript[M, [[j]]]) = ((Subscript[M, [[j]]])/(Subscript[M, [[j,j]]])); Print[MatrixForm[M]];
For[ i=1, i≤n, i++, If[ i≠j,Print["Row", i," = Row",i," - (",(Subscript[M, [[i,j]]]),")*Row",j];
(Subscript[M, [[i]]]) = (Subscript[M, [[i]]]) - (Subscript[M, [[i,j]]])(Subscript[M, [[j]]]) ;Print[MatrixForm[M]] ;];];];  ]

A={{(Subscript[a, 1,1]),(Subscript[a, 1,2]),(Subscript[a, 1,3])},{(Subscript[a, 2,1]),(Subscript[a, 2,2]),(Subscript[a, 2,3])},{(Subscript[a, 3,1]),(Subscript[a, 3,2]),(Subscript[a, 3,3])}}

MatrixForm[A]

(({{(Subscript[a, 1,1]), (Subscript[a, 1,2]), (Subscript[a, 1,3])}, {(Subscript[a, 2,1]), (Subscript[a, 2,2]), (Subscript[a, 2,3])}, {(Subscript[a, 3,1]), (Subscript[a, 3,2]), (Subscript[a, 3,3])}})) (* This is a example of a nxn matrix.*)

For[ j=1, j≤n, j++, Print["Row",j," = Row",j," / (",(Subscript[M, [[j,j]]]),")"];
(Subscript[M, [[j]]]) = ((Subscript[M, [[j]]])/(Subscript[M, [[j,j]]])); Print[MatrixForm[M]];]

(*There is two for loops in this function; the first one is the "j" row loop that is devided by (Subscript[M, [[j,j]]]) which is the {(Subscript[a, 1,1])},{(Subscript[a, 2,2])},{(Subscript[a, 3,3])} as "j" increases, This is the loop we use to find the leading ones. *)

For[ i=1, i≤n, i++, If[ i≠j,Print["Row", i," = Row",i," - (",(Subscript[M, [[i,j]]]),")*Row",j];
(Subscript[M, [[i]]]) = (Subscript[M, [[i]]]) - (Subscript[M, [[i,j]]])(Subscript[M, [[j]]]) ;Print[MatrixForm[M]] ;];]

(*The second for loop is the 
"i" row loop which the "j" row that has the leading one on that specific row times the (Subscript[M, [[i,j]]]) which is the {(Subscript[a, 2,1])} and {(Subscript[a, 3,1])} for fisrt column,{(Subscript[a, 1,2])} and {(Subscript[a, 3,2])} for second column and so on, and then substracting it from the ith row to get the zeros we need in the columns.*}

Print["Row",j," = Row",j," / (",(Subscript[M, [[j,j]]]),")"];

Print["Row", i," = Row",i," - (",(Subscript[M, [[i,j]]]),")*Row",j];

(*To print what Mathematicas is doing for each step for eah of the for loops.*)

Print[MatrixForm[M]]

(*To print the the matrix after each calculation we use the print fucntion.*)

RowReduces[M,3]