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 a and b.

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 is a matrix of all coefficients to the left of the equals sign. X is a matrix of unknowns a and b. While B is a matrix containing the outputs, like 27 and 28 in the case of Equation 1. The matrices A, X and B will look like this:

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 A, then take dot product of the inverse with matrix B as shown in the following equation:

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 A, X and B. The following script creates NumPy arrays for matrix A in the system of linear equations in our Equation 1:

import numpy as np

matrix  = [[6, 3], [9, 4]]

A = np.array(matrix)

Next, we need to take the inverse of matrix A. To do so, we can use the 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 X, you can take the dot product of the inverse of A and the matrix B, like this:

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 a and b in Equation 1 are 2 and 5, respectively. To verify, you can plug these values back into the Equation 1 and see if you can get the correct answer.


Code More, Distract Less: Support Our Ad-Free Site

You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.


Let’s now solve a system of linear equations with three unknowns i.e. a, b, and c. Regardless of the number of unknown variables, the process remains the same. Our system of linear equations looks like this:

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 a, b, and c in Equation 2 are 3, 5, and 8, respectively.

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 and B matrices to get the solution for the system of linear equations. This is a second, possibly easier to remember, way to solve a system of linear equations with Python’s NumPy library. Look at the following script:

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 a represents the price of 1 apple, while the variable b corresponds to the price of 1 banana.

Just like in our examples above, you can use the linalg.solve() method to find the values for variables a and b as shown below.

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 A and then take its dot product with matrix B to solve your system of linear equations.

With linalg.solve(), you can directly pass the A and B matrices to the method to get your unknown variables. Both methods are great, but I recommend using the linalg.solve() method as it saves you from the additional step of taking dot product with the output matrix.


Code More, Distract Less: Support Our Ad-Free Site

You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.