Solutions to Systems of Linear Equations using python

In this article, we will discuss how we solve a systems of equations when it has unique solution. We will discuss some of the common methods that you often come across in your work in this section. And in the next section, we will show you how to solve it in Python.

Consider a system of linear equations in matrix form, AX=y, where A is an n×n matrix. Recall that this means there are nequations and n unknowns in our system. A solution to a system of linear equations is an x in Real numbers system that satisfies the matrix form equation.

Let’s say we have n equations with n variables, AX=y, as shown in the following:

There are some famous methods could be found in every elementary linear algebra book,such as Gauss Elimination Method, Gauss-Jordan Elimination Method, LU Decomposition Method and Iterative Methods — Gauss-Seidel Method.But in this article we do not investigate these methods,consequently therefore we focus on numerical analysis programming.

Now we will use Python to solve the systems of equations. The easiest way to get a solution is via the solve function in Numpy.

Let’s try numpy.linalg.solve to solve the following equations:

import numpy as np

A = np.array([[4, 3, -5],
[-2, -4, 5],
[8, 8, 0]])
y = np.array([2, 5, -3])

x = np.linalg.solve(A, y)

The result will be :

[ 2.20833333 -2.58333333 -0.18333333]

The solver is actually doing a LU decomposition to get the results. You can check the help of the function, it needs the input matrix to be square and of full-rank, i.e., all rows (or, equivalently, columns) must be linearly independent.

Now let’s try to solve the above equations using the matrix inversion approach.

A_inv = np.linalg.inv(A)

x =, y)

The result will be :

[ 2.20833333 -2.58333333 -0.18333333]

We can also get the L and U matrices used in the LU decomposition using the scipy package.

from scipy.linalg import lu

P, L, U = lu(A)
print('P:\n', P)
print('L:\n', L)
print('U:\n', U)
print('LU:\n',, U))

The result will be :

[[0. 0. 1.]
[0. 1. 0.]
[1. 0. 0.]]
[[ 1. 0. 0. ]
[-0.25 1. 0. ]
[ 0.5 0.5 1. ]]
[[ 8. 8. 0. ]
[ 0. -2. 5. ]
[ 0. 0. -7.5]]
[[ 8. 8. 0.]
[-2. -4. 5.]
[ 4. 3. -5.]]

Programming skills which were reviewed above are useful in scientific computing solving many problems in engineering.In the next articles we will follow up these methods in the field of finance trying to build quantitative algorithms.

We wish to escalate public knowledge about computer science topics.NetCoinCapital is a blockchain start up which is motivated to expand the bounds of science by trending,researching and developing state of art technologies for future concepts.

Follow us on social media.




Does small or a big role matter? Anyone who puts all his energy into his position will benefit by reaching the goal.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Powering Through that Math Degree —  What’s Next?

Did You Know these Facts about GRE Math?

Liberal Arts Blog — The Orion of Math — Counting, Formulating, Calculating

3 Ways to solve a Quadratic equation (3rd is my favorite) -

A game called Numbers Four Fours

Divide Any Number by 2 in Your Head

Fig cut in half

My favourite YouTube channels in every category

Who defined Division by Zero as Infinity?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Netcoincapital Official

Netcoincapital Official

Does small or a big role matter? Anyone who puts all his energy into his position will benefit by reaching the goal.

More from Medium

Analysing my Magic: The Gathering Commander Deck using Monte Carlo Simulations with Python

Boolean masking (NumPy) in Python

Convert a Negative Decimal Number with a Fractional Part using 2’s Complement in Python

Fetching subsets of VCF files, a DIY approach