Representing Systems of Equations as Matrices
By completing this lab, you will be able to use basic programming skills with Python and the NumPy
package to represent systems of linear equations as matrices. In this notebook, you will:
- Use the
NumPy
linear algebra package to model systems of linear equations as matrices. - Evaluate the determinant of the matrix and examine the relationship between matrix singularity and the number of solutions of the linear system.
Packages
Load the NumPy
package to access its functions. Additionally, load the matplotlib.pyplot
package, which you will use for creating the plots.
Representing System of Linear Equations using Matrices
System of Linear Equations
A system of linear equations (or linear system) is a collection of one or more linear equations involving the same variables. For example:
is a system of two equations with two unknown variables and . To solve a system of linear equations means to find values for the variables and such that all of its equations are simultaneously satisfied.
A linear system is singular if it has no unique solution, and otherwise, it is said to be non-singular.
System of Linear Equations as Matrices
In the lecture, you saw that we represented linear systems of equations as matrices. The system represented as a matrix is as follows:
Each row represents an equation in the system. The first column represents the coefficients of in the system, the second column represents the coefficients of , and the third column represents the constant values on the right side of the equals signs in the equations.
We could further choose to represent the coefficients of the system as its own matrix as follows:
and the outputs of the system as a vector like this:
We show the matrix and vector in NumPy
below:
What are the dimensions of matrix and vector ?
You can confirm the dimensions of and using the shape
attribute (you can also use np.shape()
as an alternative).
In the lectures, you manually solved some simple linear systems with two equations. However, you have yet to formalize the approach to solving systems of linear equations. In this lab, we use a handy function to solve the equations.
The NumPy
linear algebra package provides a quick and reliable way to solve systems of linear equations using the function np.linalg.solve(A, b)
. Here, is a matrix, as you've seen previously, where each row represents one equation in the system, and each column corresponds to the variables and . is a 1-D array of the free (right side) coefficients. More information about the np.linalg.solve()
function can be found in the documentation.
To find the solution of the system , we will simply use the np.linalg.solve(A, b)
function. The result will be saved in the 1-D array , where the elements correspond to the values of and :
The first column in this output is the solution to the variable , and the second column is the solution to the variable . Confirm that the solution is correct by substituting these values of and into the original system of equations.
Evaluating Determinant of a Matrix
The matrix corresponding to the linear system is a square matrix - it has the same number of rows and columns. In the case of a square matrix, it is possible to calculate its determinant - a real number which characterizes some properties of the matrix. A linear system containing two (or more) equations with the same number of unknown variables will have one solution if and only if matrix has a non-zero determinant.
In this course, it's useful to calculate properties like the determinant by hand to develop an intuition for how it is calculated, but these calculations are also easily done by a computer.
Let's calculate the determinant using the NumPy
linear algebra package. You can do it with the np.linalg.det(A)
function. More information about it can be found in the official documentation.
Visualizing 2x2 Systems as Plotlines
You can see how easy it is to use contemporary packages to solve linear equations and calculate useful properties of matrices like the determinant. In this section, we will visualize a 2x2 system as plot lines, as you saw in the ungraded plugin.
Representation of the system as a matrix
Before you visualize the system , you would want to represent the system in a matrix with the form:
To do this, you can either create a new matrix with these values or horizontally stack the and matrices you created earlier. Note that the np.hstack()
function will require you to reshape array before it is stacked, as its current shape is . The code below includes the .reshape((2, 1))
command to allow the horizontal stack to be completed.
Let's review how to extract a row of a matrix, which will help later in performing the required operations with the rows. Remember that indexing of arrays in Python starts from zero, so to extract the second row of a matrix, you need to use the following code:
Graphical Representation of the Solution
A linear equation in two variables (here, and ) can be represented geometrically by a line in the plane. This is called the graph of the linear equation. In the case of the system of two equations, there will be two lines corresponding to each of the equations, and the solution will be the intersection point of those lines.
In the following code, you will define a function plot_lines()
to plot the lines and use it later to represent the solution which you found earlier. Do not worry if the code in the following cell is not clear - at this stage, it is not important to understand.
Notice how the lines intersect at , the solution to the system of equations.
System of Linear Equations with No Solutions
Given another system of linear equations:
Let's find the determinant of the corresponding matrix.
It is equal to zero, thus the system cannot have one unique solution. It will either have infinitely many solutions or none. The consistency of it will depend on the free coefficients (right-side coefficients). You can run the code in the following cell to check that the np.linalg.solve()
function will give an error due to singularity.
Construct the matrix corresponding to this linear system:
As expected, the lines of the two equations are parallel.
System of Linear Equations with an Infinite Number of Solutions
By changing the free coefficients of the system , you can bring it to consistency:
Prepare the new matrix, corresponding to the system :
Thus, from the corresponding linear system
the solutions of the linear system are:
where is any real number.
If you plot the equations of the system, how many lines do you expect to see in the graph now? Check it using the code below: