This tutorial uses several examples to explain how to solve a system of linear questions using Python’s NumPy library and its linalg.solve
and linalg.inv
methods. Before we get to the NumPy codes, let’s refresh our memories on what linear equations are and how they can be solved.
What is a System of Linear Equations?
A system of linear equation is a collection of two or more linear equations with the same number of unknowns. To solve a system of linear equations, we have to find the values of the unknowns. It’s important to mention that in this type of linear algebra none of the unknowns have powers greater than one (hence the “linear”). For instance, a system of linear equations having two linear equations and two unknowns looks like this:
Equation 1:
6a + 3b = 27 ## Equation 1
9a + 4b = 38
How to Solve a System of Linear Equations?
To solve a linear system, such as the one shown in Equation 1, we have to find the values of unknown variables, which in case of Equation 1 are
There are multiple ways to solve a system of linear equations. Some of the most commonly used methods are the Matrix Solution, Row Reduction Technique, and Cramer’s Rule. In this article you will see how to use tue Matrix solution to solve a system of linear equation.
While applying the matrix solution, a linear equation is represented in the form of the following matrices:
AX = B
Here, a
and b
. While 27
and 28
in the case of Equation 1. The matrices
A = [6 3]
[9 4]
X = [a]
[b]
B = [27]
[28]
To find the value of a matrix, i.e. the unknown variables, you can take the inverse of matrix
X = A-1·B
Solving a System of Linear Equations with NumPy
To install the NumPy library, you can use pip installer. The following command installs the NumPy library:
$ pip install numpy
To solve a system of linear equations with NumPy, the matrix forms of linear equations should be converted to two multidimensional NumPy arrays, where each row will contain the contents for the matrix
import numpy as np
matrix = [[6, 3], [9, 4]]
A = np.array(matrix)
Next, we need to take the inverse of matrix linalg.inv()
method from the NumPy library.
inv_A = np.linalg.inv(A)
print(inv_A)
Output:
[[-1.33333333 1. ]
[ 3. -2. ]]
Finally, to find the values for matrix
B = np.array([27, 38])
X = np.linalg.inv(A).dot(B)
print(X)
Output:
[2. 5.]
The output shows that the values of unknown variables
Get Our Python Developer Kit for Free
I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.
Let’s now solve a system of linear equations with three unknowns i.e.
Equation 2
4a + 2b +5c = 53
5a + 3b +7c = 74
9a + 2b +6c = 73
The following script solves the above system of linear equations:
A = np.array([[4, 2, 5], [5, 3, 7], [9, 2, 6]])
B = np.array([53, 74, 73])
X = np.linalg.inv(A).dot(B)
print(X)
Output:
[3. 8. 5.]
The output shows that values of unknown variables
In addition to using the linalg.inv()
function and then taking its dot product with the output matrix, you can directly call the linalg.solve()
method and pass it the
A = np.array([[4, 2, 5], [5, 3, 7], [9, 2, 6]])
B = np.array([53, 74, 73])
X = np.linalg.solve(A,B)
print(X)
Output:
[3. 8. 5.]
Both linalg
methods yield the same result!
Using Linear Equations in a Real World Scenario
A lady bought 12 apples and 6 bananas for $102 on a specific day (expensive fruit, I know…). The next day, she bought 10 apples and 10 bananas for $110. Assuming the price of the apples and bananas didn’t change in two days, what is the price of one apple and one banana?
The above question can be solved using a system of linear equations involving two variables. The problem can be framed in the form of a system of linear equations as follows:
12a +6b = 102
10a +10b = 110
In the above equation the unknown variable
Just like in our examples above, you can use the linalg.solve()
method to find the values for variables
A = np.array([[12, 6], [10, 10]])
B = np.array([102, 110])
X = np.linalg.solve(A,B)
print(X)
Output:
[6. 5.]
The output shows that the price of 1 apple is $6, while the price of 1 banana is $5. Told you it was some expensive fruit!
Conclusion
In this tutorial, we showed you how to solve a system of linear equations using Python’s NumPy library. The NumPy library provides two methods for this purpose: linalg.inv()
and linalg.solve()
.
With linalg.inv()
, you can take the inverse of the matrix
With linalg.solve()
, you can directly pass the linalg.solve()
method as it saves you from the additional step of taking dot product with the output matrix.
Get Our Python Developer Kit for Free
I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.