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.


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


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
#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
#Solve the system of equations and store the result in X
X = linalg.solve(A, B)
#Print the solution

YouTube Tutorial

PhD researcher at Friedrich-Schiller University Jena, Germany. I'm a physicist specializing in theoretical, computational and experimental condensed matter physics. I like to develop Physics related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.
[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 *