You are now ready to learn something that has been lurking yet present in the previous lessons. In one of the first quizzes, you took a system of linear equations and found its solution. In general, this is hard to do by simply looking at the equations. However, there exists a simple way to do this.
In this section, you will learn a method or an algorithm that helps you find a solution to a system of linear equations and that is also able to tell you if this system is singular or non-singular.
Revisiting Our First Solution
First, let’s go back to how you solved the first system of equations. The one with equations \(a + b = 10\) and \(a + 2b = 12\).
Recall that the scenario was that an apple and a banana cost $10 and an apple and 2 bananas cost $12. So you figured out that since the second day you bought an extra banana and paid $2 more, that extra banana must cost $2. From this, and the fact that they both cost $10, you concluded that the apple must cost $8.
Thus, you went from the original system of equations:
\[\begin{cases} a + b = 10 \\ a + 2b = 12 \end{cases}\]
to a solved system:
\[\begin{cases} a = 8 \\ b = 2 \end{cases}\]
The solved system is much simpler because each equation actually tells us the value of each of the variables. The goal is to take every system and turn it into a solved system — that is, if the system is non-singular and has exactly one solution.
The Elimination Process
Now, in order to get from the system to the solved system, you followed some process. This process required manipulating the equations. Without noticing, what you actually did in your head was to follow a specific process that involves manipulations, such as swapping equations, adding them, multiplying them by constants.
The first step in order to go from a system to a solved system is the following. Notice that in the system on the left, the equations both have \(a\) and \(b\) in them. You’d like to get to one in which the \(a\) and the \(b\) are isolated and they only appear in one equation each. The first step will be to look at the second equation and try to eliminate the variable \(a\) from it.
Legal Equation Manipulations
But before you do that, it’s important to know how one can manipulate equations. That is, take some linear equations and produce some other ones that are still true based on the original ones.
Multiplication by a Constant
One way to manipulate equations is to multiply them by a constant. For example, if an apple and a banana cost $10, how much would 7 apples and 7 bananas cost? Well, the answer is $70, as this is 7 times 10.
These equations carry the exact same information. Thus, if the first one is true, so is the second one.
Adding Two Equations
Another way to manipulate equations is to add two equations. For example, if an apple and a banana cost $10, and 2 apples and 3 bananas cost $26, then how much are 3 apples and 4 bananas?
Well, if you add these two equations:
\[\begin{array}{rcl}
& a + b &= 10 \\
+ & 2a + 3b &= 22 \\
\hline
& 3a + 4b &= 32
\end{array}\] If the two first equations are true, then so is their sum.
A Complete Example: Step by Step
So now I’ll show you how to solve a harder system of equations in order for you to see how these manipulations are used. Here’s the system of equations:
Recall that your first goal in order to get to a solved system is to try to eliminate the variable \(a\) from the second equation in order to leave \(b\) by itself.
Step 1: Normalize the Coefficients (Optional but Helpful)
The first step is not always necessary, but it will make your life easier. The step is to divide each equation by the coefficient of \(a\) in order for both of them to have a coefficient of one beside the \(a\).
So, if we do this:
First equation: \(\frac{5a + b}{5} = \frac{17}{5} \Rightarrow a + 0.2b = 3.4\)
Second equation: \(\frac{4a - 3b}{4} = \frac{6}{4} \Rightarrow a - 0.75b = 1.5\)
And as you saw before, these are equivalent to the first two, because all we did was multiply the entire equation left and right by a constant.
Step 2: Eliminate Variable \(a\)
Now, in order to remove \(a\) from the second equation, one way is to subtract the first equation from the second one.
Like this, we take the second equation, we subtract the first one, and we can do this component-wise:
\[(a - 0.75b) - (a + 0.2b) = 1.5 - 3.4\]
\(a - a = 0a\)
\(-0.75b - 0.2b = -0.95b\)
\(1.5 - 3.4 = -1.9\)
The resulting equation is: \[-0.95b = -1.9\]
Now, we can divide both sides by \(-0.95\) to get: \[b = \frac{-1.9}{-0.95} = 2\]
Success! You have found the value of \(b\). In other words, you removed \(a\) from the second equation to get \(b = 2\) in your solved system.
Step 3: Back-Substitution
Now that you know that \(b = 2\), you can simply plug that into the first equation.
The first equation says \(a + 0.2b = 3.4\). So: \[a + 0.2(2) = 3.4\]\[a + 0.4 = 3.4\]
Subtracting \(0.4\) from both sides: \[a = 3.4 - 0.4 = 3\]
And that is the value of the second variable.
Solution: \(a = 3, b = 2\)
Special Case: When a Variable is Already Eliminated
Here’s a small caveat. Imagine that you are solving this new system with equations:
And you’d like to eliminate \(a\) from the second equation. The first step is to divide both by the coefficient of \(a\).
For the first equation, this goes exactly as before. However, for the second equation, the coefficient of \(a\) is 0 and you can’t divide by 0. So, are we doomed?
Well, actually, the doom turns into good luck. Check it out: the equation says \(3b = 6\). It already has \(a\) eliminated from it and actually implies that \(b = 2\) if you divide by 3 on both sides. So this is the value of \(b\) already, and the second equation is already solved in the system.
In order to solve the first one, you proceed to do the same thing. Replace in the first equation that \(b = 2\): \[5a + 2 = 17\]\[5a = 15\]\[a = 3\]
And bring it back as the solution: \(a = 3, b = 2\).
Summary: The Elimination Algorithm
So you’re now ready for the general process. Here’s the elimination algorithm in a nutshell:
Choose a pivot equation (usually the first one)
Normalize coefficients (optional but helpful)
Eliminate variables from other equations using addition/subtraction
Repeat for the next variable in the remaining equations
Back-substitute to find all variable values
Three Equations with Three Variables
Now that you learned how to solve systems of two equations with two variables, let me show you how to solve systems of three equations with three variables. It’s actually very similar. Consider the following system:
\[\begin{cases} a + b + 2c = 12 \\ 3a - 3b - c = 3 \\ 2a - b + 6c = 24 \end{cases}\]
The goal here is first to leave \(a\) by itself. We’re going to make sure that the only equation that contains an \(a\) is the first one, and that there’s no \(a\) in the second and third equations.
Step 1: Normalize the First Column
The way to do this is to normalize the first column. Namely, divide each row by the coefficient of \(a\) to make sure that every equation has a coefficient of one for the variable \(a\).
The first equation already has a coefficient of 1 for \(a\). The second has a coefficient of 3, so we divide it by 3. The third has a coefficient of 2, so we divide it by 2:
\[\begin{cases} a + b + 2c = 12 \\ a - b - \frac{1}{3}c = 1 \\ a - \frac{1}{2}b + 3c = 12 \end{cases}\]
Step 2: Eliminate \(a\) from the Second and Third Equations
Now, use the first equation to remove the variable \(a\) from all the other ones. Subtract the first equation from the second and then from the third to get the following:
\[\begin{cases} a + b + 2c = 12 \\ -2b - \frac{7}{3}c = -11 \\ -\frac{3}{2}b + c = 0 \end{cases}\]
Now, in the resulting system, you have successfully isolated \(a\) and what remains is a system of two equations with two unknowns, \(b\) and \(c\). You know exactly how to solve these ones.
Step 3: Solve the Remaining 2×2 System
Let’s forget about the first equation and solve the second and third. First, divide these two rows by the coefficient of \(b\) to get this, where the leading coefficient of \(b\) is now one:
\[\begin{cases} b + \frac{7}{6}c = \frac{11}{2} \\ b - \frac{2}{3}c = 0 \end{cases}\]
Now, use the second equation to remove \(b\) from the third one. Subtract the second equation from the third one to get:
\[-\frac{11}{6}c = -\frac{11}{2}\]
In this way, you have isolated \(b\) in the bottom two rows. From here, you can divide by \(-\frac{11}{6}\) to get that \(c = 3\).
Step 4: Back-Substitute
Now let’s go back to the beginning. We know that \(c = 3\). Then we can propagate upstairs, so replace \(c = 3\) in the second equation to get:
Now that you know \(b\) and \(c\), replace \(b = 2\) and \(c = 3\) in the first equation to get:
\[a + 2 + 2(3) = 12\]\[a = 4\]
Solution: \(a = 4, b = 2, c = 3\)
After this, you’ll get to use a new interactive tool to explore systems of equations in three dimensions. You’ll be able to see each equation as a plane floating in three-dimensional space, and the solution to the system at the intersection of these planes. It’s a fun tool to play with and hopefully deepens your understanding of the concepts you’ve been studying. As always, some suggested activities are below the tool itself. Enjoy.
Lab: Solving Linear Systems with NumPy
Now that you know how to solve systems by hand, let’s see how to do it with code using NumPy. Consider the following system:
We can check the dimensions of \(A\) and \(b\) using the shape attribute:
print(f"Shape of A: {np.shape(A)}")print(f"Shape of b: {np.shape(b)}")
Shape of A: (3, 3)
Shape of b: (3,)
Solving the System
NumPy provides np.linalg.solve to find the solution directly. The result is a 1-D array where the elements correspond to \(x_1\), \(x_2\), and \(x_3\):
x = np.linalg.solve(A, b)print(f"Solution: {x}")
Solution: [ 1. 4. -2.]
The solution is \(x_1 = 1\), \(x_2 = 4\), \(x_3 = -2\). Try substituting these values into the original equations to verify!