Tuesday, 26 December 2017

Program to Solve Simultaneous Equation in Python

For all lovers of the Python Programming Language, I would like to share a program to solve simultaneous equation(system of linear equation) in Python. I have written the program, so you just have to copy and paste in your Python IDE enjoy running it.


I would like to explain a bit of how the program works.

How it Works
First, the program request for inputs a1, a2 and a3, those are the coefficient of the first equation. Then it prompts for the coefficients: b1, b2 and b3, the coefficients of the second equation.
It then solves and display the result for x1 and x2

Source code:
#**************************************************  
#PROGRAM BY: KINDSON THE GENIUS
#CATEGORY:   PYTHON TUTORIALS 
#DATE:       26TH DECEMBER2017 #***************************************************

print("\n*** PROGRAM TO SOLVE 2x2 SIMULTANEOUS EQUATION\n\n")
print "\nEquation of the form a1x + b1y = c1, a2x + b2y = c2  \n\n\n"

#INPUT THE COEFFICIENTS OF EQUATION 1
a1 = input("\nEnter a1: ")
b1 = input("\nEnter b1: ")
c1 = input("\nEnter c1: ")

#INPUT THE COEFFICIENTS OF EQUATION 2
a2 = input("\nEnter a2: ")
b2 = input("\nEnter b2: ")
c2 = input("\nEnter c2: ")

#USING CRAMER'S RULE...
D = (a1 * b2) - (a2 * b1)
Dx = (c1 * b2) - (c2 * b1)
Dy = (a1 * c2) - (a2 * c1)
x = Dx/D
y = Dy/D

#DISPLAY THE  RESULTS
print "*********The results are:******************* \n\n"
print "x = {}".format(x)
print "y = {}".format(y)
raw_input("\n\nPress <Enter> to exit...")