2 by 2 Simultaneous Equations Code in Python
Well, it has not been much of algebra so far; We are about to have
our first taste of it.
Simultaneous equations can be solved using either
Elimination or Substitution method.
We will be using the Elimination method.
Algorithm Steps to Follow for Simultaneous Equations with 2 unknowns in Python
Consider the equations:
2x + 3y = 13; and
5x - y = 7
Step 1:
Pick a variable to eliminate, either x or y.
Our code will always eliminate the y variable.
Step 2:
Multiply equation 1 by the coefficient of variable y
in equation 2.
⇒
-1 X (2x + 3y = 13)
⇒
-2x - 3y = -13
Step 3:
Multiply equation 2 by the coefficient of variable y
in equation 1.
⇒
3 X (5x - y = 7)
⇒
15x - 3y = 21
Step 4:
Subtract the new equations obtained from Steps 2 and 3.
-2x - 3y = -13
-
15x - 3y = 21
⇒
-17x = -34
Step 5:
Divide the R.H.S. from Step 4 by the coefficient of
x to obtain x.
⇒
x = -34/-17 = 2;
Step 6:
Obtain y by solving for y from any of the
original equations, using the found value of x.
2x + 3y = 13
⇒
2(2) + 3y = 13;
⇒
3y = 13 - 4 = 9;
⇒
y = 9/3 = 3;
Create a new module file;
call it Simultaneous2Unknown.py.
Type out the adjoining Python code for solving simultaneous equations with 2 unknowns.
Note: You can comment out the SortFraction Python object
code in the main class from the previous lesson or simply continue from where it stopped.
So!
Feel free to try out your own set of x_coefficients,
y_coefficients and equals values for 2 by 2 Simultaneous Equations.
Python code for Simultaneous2Unknown Module File
class Sim2Unknown:
def __init__(self, equations):
self.x_coefficients = equations['x']
self.y_coefficients = equations['y']
self.equals = equations['eq']
self.eliminator = [[[], []], [[], []]]
def solveSimultaneous(self):
self.eliminator[0][0] = self.y_coefficients[1] * self.x_coefficients[0]
self.eliminator[0][1] = self.y_coefficients[1] * self.equals[0]
self.eliminator[1][0] = self.y_coefficients[0] * self.x_coefficients[1]
self.eliminator[1][1] = self.y_coefficients[0] * self.equals[1]
try:
self.x_variable = (self.eliminator[0][1] - self.eliminator[1][1]) / (self.eliminator[0][0] - self.eliminator[1][0])
self.y_variable = (self.equals[0] - self.x_coefficients[0] * self.x_variable) / self.y_coefficients[0]
return [self.x_variable, self.y_variable]
except:
raise ValueError(None)
main class
from Simultaneous2Unknown import Sim2Unknown
operators = ['+', '+']
x_coefficients = [2, 1]
y_coefficients = [-1, 1]
equals = [1, 2]
equations = {'x':x_coefficients, 'y':y_coefficients, 'eq':equals}
if y_coefficients[0] < 0: operators[0] = '-'
if y_coefficients[1] < 0: operators[1] = '-'
print('\n Solving simultaneously the equations:\n')
print('{:40d}{} {} {:d}{} {:d}'.format(x_coefficients[0], 'x', operators[0], abs(y_coefficients[0]), 'y = ', equals[0]))
print('{:40d}{} {} {:d}{} {:d}'.format(x_coefficients[1], 'x', operators[1], abs(y_coefficients[1]), 'y = ', equals[1]))
print('{}{:>30} {}{:>40}'.format('\n', 'Yields:', '\n', '(x, y) = '), end='')
try:
sim2unk = Sim2Unknown(equations)
solution = sim2unk.solveSimultaneous()
print('{}{:.4f}{} {:.4f}{}'.format('(', solution[0], ',', solution[1], ')'))
except: print('{}'.format('(infinity, infinity)'))
print('\n\n')