How to Solve Linear Equations with Matrix Inversion

codeperfectplus

Deepak Raj

Posted on August 18, 2023

How to Solve Linear Equations with Matrix Inversion

In this article, we will solve linear equations using inverse matrix using Python and Numpy.

We will solve linear equations using an inverse matrix in two steps. First, we will find the inverse of the matrix and then we will multiply the inverse matrix with the constant matrix.

For this, we will take an example of two linear equations with two variables. we will solve to find the value of x and y.

What is Linear Equation?

A linear equation is an equation of a straight line. It is a polynomial equation of degree 1. A linear equation can have more than one variable.

Standard Form of Linear Equation

The standard form of the linear equation is given below.

ax+by=c ax + by = c

Example of Linear Equation

3x+8y=5 3x + 8y = 5

4x+11y=7 4x + 11y = 7

How to Solve Linear Equations with Matrix Inversion? | Mathematics

A = [[3, 8], 
    [4, 11]]

B = [5, 7]

X = ?


X = inverse(A) . B

Enter fullscreen mode Exit fullscreen mode

How to Find Inverse of Matrix? | Python

Here is the Python code to find the value of x and y using an inverse matrix using Python and Numpy.

First, we will check if the matrix is invertible or not. If the matrix is invertible then we will find the inverse of the matrix and then we will multiply the inverse matrix with the constant matrix to find the value of x and y.

Driver Code

It’s time to test our code. Here is the driver code to test our code.

def test_case():
    A = np.array([[3, 8], [4, 11]])
    B = np.array([5, 7])
    solver = LinearEquationSolver(A, B)
    coff = solver.solve()
    print(coff)

if __name__ == " __main__":
    test_case()

Enter fullscreen mode Exit fullscreen mode

Output

[-1. 1.]

Enter fullscreen mode Exit fullscreen mode

Explanation

Here is the explanation of the output.

x=1 x = -1
y=1 y = 1
3x+8y=5 3x + 8y = 5
3(1)+8(1)=5 3(-1) + 8(1) = 5
3+8=5 -3 + 8 = 5
5=5 5 = 5
4x+11y=7 4x + 11y = 7
4(1)+11(1)=7 4(-1) + 11(1) = 7
4+11=7 -4 + 11 = 7
7=7 7 = 7

Conclusion

In this article, we learned how to solve linear equations using an inverse matrix. We learned how to find the inverse of the matrix and how to multiply the inverse matrix with the constant matrix to find the value of x and y.

💖 💪 🙅 🚩
codeperfectplus
Deepak Raj

Posted on August 18, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related