Solving Linear equations with Python

This notebook shows the way to solve linear equations problem with Python numpy library to find the values of x and y .

3x -9y =-42
2x + 4y = 2

Let Z be array of x and y.

z = inverse(A) * b

Where 
A = ([[3,-9],[2,4]])
Z= ([x,y])
b= ([-42,4])
In [12]:
import numpy as np

#Assign Array A
A = np.array([[3,-9],[2,4]])

print("Array A {0} \n".format(A))

# Assign Array B
b = np.array([-42,2])
print("Array B {0} \n".format(B))

# Use numpy linear Algebra function to solve functions
z = np.linalg.solve(A,b)

# Print the result
print("Value of x and y ".format(z))
Array A [[ 3 -9]
 [ 2  4]] 

Array B [-42   2] 

Valye of x and y 

Now we will see how we can use this library to solve linear equations problems for 3 variables

x - 2y - z = 6
2x + 2y - z = 1
-x - y + 2z = 1
In [16]:
import numpy as np

#Array a1
a1 = np.array([[1,-2,-1],[2,2,-1],[-1,-1,+2]])

#print a1
print(a1)

# Array b1
b1 = np.array([6,1,1])
print(b1)

#Using numpy linear Algebra library to solve this functions
z1 = np.linalg.solve(a1,b1)

#Print the result 
print("Result Value {0}".format(z1))
[[ 1 -2 -1]
 [ 2  2 -1]
 [-1 -1  2]]
[6 1 1]
Result Value [ 3. -2.  1.]