Solving a System of Linear Equations using Python

In this brief blog post I post a small code that can be used to solve a system of linear equations using SciPy package of Python and in particular the linalg module within the SciPy package.

Documentation: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.linalg.solve.html

The code is pretty much self-explanatory, and there is also a YouTube video where I walk you through the procedure.

CODE:

import numpy as np
from scipy import linalg
#Solve a system of equations A.X=B

#Define the LHS coefficient matrix A
A = np.array([ [1, 3, -2], [3, 5, 6], [2, 4, 3] ])
#Print the matrix A
print(A)
#Define the RHS column vector B
B = np.array([ [5, 7, 8] ]) #This is a single row
#Transpose it to make a column
B = B.T
#Print the RHS vector
print(B)
#Solve the system of equations and store the result in X
X = linalg.solve(A, B)
#Print the solution
print(X)

YouTube Tutorial

[wpedon id="7041" align="center"]

One thought on “Solving a System of Linear Equations using Python

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.