Jan 21, 2013

Solving multivariable linear equation using Numpy in python

Here I am going to solve a trivial problem in linear algebra using python. The problem is the famous problem of simultaneous linear equation involving multivariable.  In this article I will explain an equation in 3 variable. In linear algebra we can solve such equation using different methods.  One of the methods for solving linear equation involving multivariable using matrices operation.  A typical 3 variable equation can be represented as
A3x+B3y +C3z=D3 
 This can be represent in matrices

where.   Ax=B is the linear equation is represented as Ax=B  i.e where x is the variable matrix and A and B are the coefficient and the constant matrix respectively.
From this the variable x is calculated as

In python we can calculate linear algebra problem using the Numpy module. NumPy is the fundamental Python package for scientific computing. It adds the capabilities of N-dimensional arrays, element-by-element operations (broadcasting), core mathematical operations like linear algebra. Here we are going to use the matrix function in Numpy.
Let us represent 3 variable linear equation such that

The general representation is A.X=B and X=B.A-1

 Here in python we are going to represent the input for the equation in form of list as such the equation above is represented as eq1=[(3,2,-2,10)]   eq2=[(1,-5,3,2)] eq3=[(5,-1,4,14)], 
 Then we will feed the the lists into a function to calculate the operation. 

Here the snapshot for the source code is shown below


Here we calculate basic linear algebra using Numpy python library.  It is evident that such equation can be done using MATLAB or other mathematical tools easily. However with the flexibility of the Python language and rich feature of the Numpy library, we can perform similar and advanced operation. Finally, Numpy is very vast and powerful library to perform even advanced functions in mathematics.
There was an error in this gadget