See the Pen Solve a Linear System of Equations by Marcus Kazmierczak (@mkaz) on CodePen.

## The Mathematics Behind It

The above solves a system of 3 equations and 3 unknowns, for example :

x + 2y + 3z = 9 2x - y + z = 8 3x - z = 3

This would be entered into the matrix above as :

1 2 3 : 9 2 -1 1 : 8 3 0 -1 : 3

And the result would give you

x = 2 y = -1 z = 3

which can be easily checked by entering each value into any of the equations.

Now how was this all done so magically using Java Script?

The technique I used to solve the system of equations is called row reduction, or **reduced row eschelon form**. This is manipulating the equations by one of the following operations:

- Multiplying any Row by a Non-Zero Constant
- Replacing a Row(k) with { Row(i) + Row(k) } ; where i, k are any row
- Interchanging any two rows. (flipping row(i) with row(k))

By applying these operations, you can reduce the matrix down so you have 1’s along the diagonal and zero’s elsewhere. Which leaves you with the solution in the augmented portion of the matrix.

A couple of problems do arise in particular systems. There are sets of equations which have no solution, and also systems which have infinitely many solutions. They arise geometrically from having either two parallel planes (no solutions) or having two planes being the same (infinite solutions).

Both cases were not handled very well by my programming. I may try and fix it up some. *BUT* I did check my answer, and an alert will display if there is an erroneous solution. So I covered myself.

## Related Links

- Have you seen the Java Applet Matrix Calculator? I have made a new matrix calculator this time using double arrays in Java, which can now calculate
**n x n**matrices, up to 50 x 50. - Read about the Linear Algebra mathematics behind the calculators for some mathematical explanations on how the matrices are calculated.