This post explains how to find the solutions of any system of linear equations with an arbitrary number of variables, if solutions exist. The method consists of the following three steps:
- Using Gauss elimination we write the system in triangular form.
- In this format, we see whether the system has either no solution, exactly one solution or infinitely many solutions.
- If there are solutions, we determine them by simple insertions.
We start with a system of linear equations in
variables
:
The and
are elements of a given field, for example the real numbers. We want to solve the system for each of the
if possible.
Simple insertions
Let us at first consider a very basic strategy: We solve one of the equations for one of the variables, say . Then we insert the result for
into the other equations and pick one of them to solve for the next variable, and so on. For very simple systems, this is indeed all we need to do. For example in
we solve the second equation to obtain Inserting this into the first equation gives
from which we easily obtain
. We have arrived at the unique solution of the system.
This was so easy, because the second equation depends on less variables than the first one. This tells us, that we should start solving with the second one and it also tell us, that the basic method of insertions will lead to some result. However, many linear systems are not that simple. In particular, there are cases which do not have a solution at all, for example
These two equations contradict each other, so there are no values for which satisfy both of them. In this case, it is still easy to see that there is no solution. However, for more complicated systems, it can by much less obvious whether solutions exist. One can try to find out by the basic method of insertions as above, but this can get messy very easily. It also can be frustrating to do many insertions just to find out that there is no solution at all.
Triangular shape
Therefore we follow a different strategy for complicated systems: Before we do the insertions, we try to bring the system into a shape where every equation depends on less variables than the previous one. This is often called triangular shape, even though it does not have to look like a triangle like the simple example above. It is fine if the last equation still depends on several variables, as long as it depends on less than the previous one. For example we would be happy with the shape
If a system is in this shape, we can easily use the basic method of insertions successively, starting with the last equation, and we will find solutions. In this example, we decide whether we solve the last equation for or
and leave the other variable as it is. This undetermined variable may take any value, so we arrive at an infinite number of solutions in such a case.
If we are given a system, which is not in this shape, there is still good news: There is a method called Gauss elimination, by which any system can be manipulated such that it either takes triangular shape or produces an obvious contradiction. In the first case, it can be solved by insertions and in the latter case, there is no solution.
How does Gauss elimination work?
Gauss elimination uses the fact that we may manipulate any system of linear equations by the following three operations without changing its solutions:
- We obviously may change the order of the equations.
- We may multiply an equation with any number which is not zero.
- We may add an equation to another one (keeping this equation in the system as well).
The following simple example shows how these operations can help to get to a triangular shape. We consider the system
This system is not in triangular shape. In particular, the second equation has a factor 3 in front of . If this factor would be zero instead, we would have a triangular shape and could proceed with simple insertions. The trick is now to force this factor to become zero by the mentioned operations. Each of the three operations alone do not help: Changing the order does not do anything here, multiplying with a number (unequal zero) does not make this factor vanish, and if we add the first equation to the second one, it does not help either. However, if we combine these operations, we are getting what we want. We take the first equation times
and then add it to the second one. In this way we obtain:
Now we have a system in triangular shape and solving it is easy. (One obtains from the second equation and
by insertion into the first one.) We have been a bit lucky in this example, because the factor of
in the first equation is 1. So we only need to multiply with -3 before we add. However, if in the first equation there was a factor
in front of
, we would have to just multiply with
before adding it to the second one.
The algorithm
Gauss elimination combines the above steps systematically for all equations and
variables until the system is in triangular form. The method can be formulated as the following algorithm:
- Start with equation
and variable
- If
search for a
for which
and exchange the order of equations
and
.
(This step moves equations downwards, which have missing variables from the beginning.) - If in step 2 no
is found, set
and repeat from step 2. (End when
.)
- Equation
is now beginning with
Multiply this equation with
(Now it starts with)
- For all
Add
times the
-th equation to the
-th equation.
(This erases variablein the
-th equation.)
- Set
and repeat everything starting from step 2. End at
or
After applying these steps, the system will be in triangular shape. Generally speaking we will always obtain:
where The number
of non-trivial equations which still involve at least one variable is called the rank of the system.
How many solutions does the system have?
We can now determine the number of solutions from the rank . Recall that
is the total number of equations and
is the number of variables. The following cases are possible:
- If
Gauss elimination has produced some trivial equations
at the end of the system. (This will always happen if
but it can also happen in other cases.) The system has no solution if any of these trivial equations is wrong, meaning that any of the constants
would not be zero. It is one of the great advantages of Gauss elimination that all possible contradictions in a system of equations are reduced to such a simple form.
- If
the system has exactly one solution. This means that each variable can only take exactly one value in order to satisfy the system. This solution can now be found by the above method of simple insertions.
- In the case of
the system has infinitely many solutions. In order to express this space of solutions, we can choose
variables which we just leave undetermined. Then we can use the method of simple insertions to express all the other variables in terms of the undetermined ones.
Example
Let us conclude this post with the example
In order to keep the computations relatively short, I use the following short-hand notation, which is very common in the context of Gauss elimination: For any system
we only write the coefficients and the constants
in the following way:
By the position in this scheme we keep track of which coefficient belongs to which variable. In this notation our example becomes:
The system is not in triangular shape, so we start with the Gauss elimination algorithm. According to step 1 we consider at first. As the first equation does not depend on
, but the second one does, we change the order of these equations according to step 2 and obtain
According to step 4 we now divide the first equation by the coefficient of and obtain
Following step 5 we multiply the first equation with and add it to the third equation, such that this one does not depend on
anymore:
There is nothing left to do regarding the first variable and first equation, so by step 6 we proceed with and the second equation. Following step 4, we divide this equation by
and obtain:
According to step 5 we add times the second equation to the third one and obtain:
Now we are done. The system is in triangular shape. The last equation has become trivial, which means that it was just a linear combination of the previous two and therefore it actually didn’t add any restriction to the solutions. As this trivial equation is obviously true, we know that solutions exist. As we are left with two non-trivial equations, the rank of the system is
. It is smaller than the number of variables
. Therefore we have infinitely many solutions and we have to leave
variables undetermined.
In order to determine the space of solutions, we apply simple insertions. Let us leave undetermined. Solving the second equation leads to
Inserting this formula into the first equation, we obtain
. Up to
, which may take any value, these equations determine the solutions for the system.