In this lab, you will keep developing your skills using Python to solve systems of linear equations by using the sub-libraby numpy.linalg. In this notebook you will:
Use NumPy linear algebra package to find the solutions to the system of linear equations
Evaluate the determinant of the matrix to see again the connection between matrix singularity and the number of solutions of the linear system
Explore the numpy.linalg sub-library to get to know its properties
To solve this system of linear equations means to find such values of the variables x1, x2, x3, that all of its equations are simultaneously satisfied.
Let's prepare to solve the linear system (1) using NumPy. A will be a matrix, each row will represent one equation in the system and each column will correspond to the variable x1, x2, x3. And b is a 1-D array of the free (right side) coefficients:
Check the dimensions of A and b using shape() function:
Now use np.linalg.solve(A, b) function to find the solution of the system (1). The result will be saved in the 1-D array x. The elements will correspond to the values of x1, x2 and x3:
Try to substitute those values of x1, x2 and x3 into the original system of equations to check its consistency.
Matrix A corresponding to the linear system (1) 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 that characterizes some properties of the matrix. A linear system containing three equations with three unknown variables will have one solution if and only if the matrix A has a non-zero determinant.
Let's calculate the determinant using np.linalg.det(A) function:
Please, note that its value is non-zero, as expected.
Let's explore what happens if we use np.linalg.solve in a system with no unique solution (no solution at all or infinitely many solutions).
Given another system of linear equations:
⎩⎨⎧x1+x2+x3=2,x2−3x3=1,2x1+x2+5x3=0,(2)
Let's find the determinant of the corresponding matrix.
As you can see, it throws a LinAlgError because the matrix is singular. You can check it by yourself by computing the determinant of the matrix:
The sub-library np.linalg has several linear algebra functions to help linear algebra tasks and we have exhausted so far the functions you have learned in class.
Once you learn more theory, the functions in this library will become clearer. You also will be using them in the assignments in week 3 and 4, but do not worry because you will be guided through them.
Well done! You used the NumPy functions to solve a system of equations. As expected, using a predefined function is much easier, but gives much less insight into what is happening under the hood. This is why the next assignment will be in Gaussian Elimination, a method to solve linear systems. Remember that np.linalg.solve gives an error if there are no or infinitely many solutions, thus when implementing it you will have to think carefully so not to make your program crash!