In this post I would be writing a guide on how to make a basic simultaneous equation solver in python. It is designed to solve 2 linear simultaneous equations with 2 unknowns.
Plan
- Instructions
- Input 2 equations
- Make equations in useful format
- Change subject of formula of Equation 1
- Change subject of formula of Equation 2
- Finding X coefficients
- Making a merged Equation
- Finding Value of Y
- Sub Y in
Instructions
print("Write equations in the format:\n aX + bY=C or aX - bY=C"+'\n')
This gives the user instructions in which format to use. I did the instructions first so I don’t try and over-complicate whilst coding. It is good to know what format you want your inputs in. You can always all multiple formats later but first make sure your program does the goal.
Input 2 equations
EquationOne=input("Enter Equation 1: ")
EquationTwo=input("Enter Equation 2: ")
These are the equations you want to solve. The user needs to input them in the format which are stated in the instructions.
Make equations in useful format
EqOne=EquationOne.split('=')
SumOne=EqOne[0].split(' ')
EqTwo=EquationTwo.split('=')
SumTwo=EqTwo[0].split(' ')
print()
This makes the input in a form which is easier for the program to use.
EqOne=EquationOne.split(‘=’)
This splits the equation into the left and right half of the equation.
Like 2 + 3 = 5 into ‘2 + 3′,’5’
SumOne=EqOne[0].split(‘ ‘)
This splits the left half of the equation into each term and the operation.
Like 2 + 3 into ‘2’,’+’,’3′
Change sign of Equation
if SumOne[1]=='+':
sign='-'
elif SumOne[1]=='-':
sign='+'
if SumTwo[1]=='+':
sign='-'
elif SumTwo[1]=='-':
sign='+'
This turns a ‘+’ into a ‘-‘ and a ‘-‘ into a ‘+’
Change subject of formula of Equation
EquationOne=str(SumOne[0]+'='+EqOne[1]+' '+sign+' '+SumOne[2])
EqOne=EquationOne.split('=')
SumOne=EqOne[1].split(' ')
EquationTwo=str(SumTwo[0]+'='+EqTwo[1]+' '+sign+' '+SumTwo[2])
EqTwo=EquationTwo.split('=')
SumTwo=EqTwo[1].split(' ')
This turns aX + bY = c into aX = c – bY
Finding X coefficients
CoefficientOne=int(EqOne[0][:-1])
CoefficientTwo=int(EqTwo[0][:-1])
This finds the coefficient of x
Making a merged Equation
SumOne[0]=str((int(SumOne[0]))*CoefficientTwo)
SumOne[2]=str((int(SumOne[2][:-1]))*CoefficientTwo)+'y'
SumTwo[0]=str((int(SumTwo[0]))*CoefficientOne)
SumTwo[2]=str((int(SumTwo[2][:-1]))*CoefficientOne)+'y'
EquationThree=SumOne[0]+' '+SumOne[1]+' '+SumOne[2]+'='+SumTwo[0]+' '+SumTwo[1]+' '+SumTwo[2]
This makes the coefficient of x the same in both equations and equates them.
SumOne[0]=str((int(SumOne[0]))CoefficientTwo) SumOne[2]=str((int(SumOne[2][:-1]))CoefficientTwo)+’y’
SumTwo[0]=str((int(SumTwo[0]))CoefficientOne) SumTwo[2]=str((int(SumTwo[2][:-1]))CoefficientOne)+’y’
These multiply all terms of equation 1 by the coefficient of equation 2 and vice versa.
EquationThree=SumOne[0]+’ ‘+SumOne[1]+’ ‘+SumOne[2]+’=’+SumTwo[0]+’ ‘+SumTwo[1]+’ ‘+SumTwo[2]
This step is only necessary if you want to print the equated equations.
Finding Value of Y
Num=int(SumOne[0])-int(SumTwo[0])
if SumOne[1]=='+':
SumOne[2]=str(0+int(SumOne[2][:-1]))
if SumOne[1]=='-':
SumOne[2]=str(0-int(SumOne[2][:-1]))
if SumTwo[1]=='+':
SumTwo[2]=str(0+int(SumTwo[2][:-1]))
if SumTwo[1]=='-':
SumTwo[2]=str(0-int(SumTwo[2][:-1]))
YCoefficient=int(SumTwo[2])-int(SumOne[2])
Y=Num/YCoefficient
print('y = '+str(Y))
This finds the value of y
Num=int(SumOne[0])-int(SumTwo[0])
This moves the whole number to the same side of an equation.
Like ‘2 + y = 4 + 2y’ into ‘-2 + y = 2y’
if SumOne[1]==’+’:
SumOne[2]=str(0+int(SumOne[2][:-1]))
if SumOne[1]==’-‘:
SumOne[2]=str(0-int(SumOne[2][:-1]))
if SumTwo[1]==’+’:
SumTwo[2]=str(0+int(SumTwo[2][:-1]))
if SumTwo[1]==’-‘:
SumTwo[2]=str(0-int(SumTwo[2][:-1]))
This make ‘+’,’a’ into ‘+a’
YCoefficient=int(SumTwo[2])-int(SumOne[2])
This makes the y terms on the same side of equation so c = bY
print(‘y = ‘+str(Y))
prints the value of Y
Sub Y in to an equation to find value of X
print('x = '+str( (int(SumOne[0])+(int(SumOne[2])*(Y)))/(CoefficientOne*CoefficientTwo) ))
Prints the value of X
(int(SumOne[0])+(int(SumOne[2])*(Y))
Subs y into equation aX=c-bY
(int(SumOne[0])+(int(SumOne[2])(Y)))/(CoefficientOneCoefficientTwo)
Divides c-bY by x coefficient so (c-bY)/a=X
Full code
#Instructions
print("Write equations in the format:\n aX + bY=C or aX - bY=C"+'\n')
#Inputs
EquationOne=input("Enter Equation 1: ")
EquationTwo=input("Enter Equation 2: ")
#Make equations in useful format
EqOne=EquationOne.split('=')
SumOne=EqOne[0].split(' ')
EqTwo=EquationTwo.split('=')
SumTwo=EqTwo[0].split(' ')
print()
#Change sign of Equation 1
if SumOne[1]=='+':
sign='-'
elif SumOne[1]=='-':
sign='+'
#Change subject of formula of Equation 1
EquationOne=str(SumOne[0]+'='+EqOne[1]+' '+sign+' '+SumOne[2])
EqOne=EquationOne.split('=')
SumOne=EqOne[1].split(' ')
#Change sign of Equation 2
if SumTwo[1]=='+':
sign='-'
elif SumTwo[1]=='-':
sign='+'
#Change subject of formula of Equation 2
EquationTwo=str(SumTwo[0]+'='+EqTwo[1]+' '+sign+' '+SumTwo[2])
EqTwo=EquationTwo.split('=')
SumTwo=EqTwo[1].split(' ')
#Finding X coefficients
CoefficientOne=int(EqOne[0][:-1])
CoefficientTwo=int(EqTwo[0][:-1])
#Making a merged Equation
SumOne[0]=str((int(SumOne[0]))*CoefficientTwo)
SumOne[2]=str((int(SumOne[2][:-1]))*CoefficientTwo)+'y'
SumTwo[0]=str((int(SumTwo[0]))*CoefficientOne)
SumTwo[2]=str((int(SumTwo[2][:-1]))*CoefficientOne)+'y'
EquationThree=SumOne[0]+' '+SumOne[1]+' '+SumOne[2]+'='+SumTwo[0]+' '+SumTwo[1]+' '+SumTwo[2]
#Finding Value of Y
Num=int(SumOne[0])-int(SumTwo[0])
if SumOne[1]=='+':
SumOne[2]=str(0+int(SumOne[2][:-1]))
if SumOne[1]=='-':
SumOne[2]=str(0-int(SumOne[2][:-1]))
if SumTwo[1]=='+':
SumTwo[2]=str(0+int(SumTwo[2][:-1]))
if SumTwo[1]=='-':
SumTwo[2]=str(0-int(SumTwo[2][:-1]))
YCoefficient=int(SumTwo[2])-int(SumOne[2])
Y=Num/YCoefficient
print('y = '+str(Y))
#Sub Y in
print('x = '+str( (int(SumOne[0])+(int(SumOne[2])*(Y)))/(CoefficientOne*CoefficientTwo) ))

