In: Computer Science
Write a python program that can solve system of linear equations in three variables using input function. Paste your program in a word document or notepad. Note that I am using pycharm. please use a not really complex codes, thanks
# find determinant of 3 x 3 matrix
def determinant(matrix):
d = matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2]) \
- matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]) \
+ matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0])
# return the result
return d
# find solution using cramer's rule
def solution(coefficient):
# extract matrix m from coefficient matrix
m=[[coefficient[0][0], coefficient[0][1], coefficient[0][2]],\
[coefficient[1][0], coefficient[1][1], coefficient[1][2]],\
[coefficient[2][0], coefficient[2][1], coefficient[2][2]]]
# extract matrix m1 from coefficient matrix
m1=[[coefficient[0][3], coefficient[0][1], coefficient[0][2]],\
[coefficient[1][3], coefficient[1][1], coefficient[1][2]],\
[coefficient[2][3], coefficient[2][1], coefficient[2][2]]]
# extract matrix m2 from coefficient matrix
m2=[[coefficient[0][0], coefficient[0][3], coefficient[0][2]],\
[coefficient[1][0], coefficient[1][3], coefficient[1][2]],\
[coefficient[2][0], coefficient[2][3], coefficient[2][2]]]
# extract matrix m3 from coefficient matrix
m3=[[coefficient[0][0], coefficient[0][1], coefficient[0][3]],\
[coefficient[1][0], coefficient[1][1], coefficient[1][3]],\
[coefficient[2][0], coefficient[2][1], coefficient[2][3]]]
# calculate determinant of each of the matrices
det=determinant(m)
det1=determinant(m1)
det2=determinant(m2)
det3=determinant(m3)
if det!=0:
# unique solution
x=det1/det
y=det2/det
z=det3/det
print("\nSolution:")
print("Value of X: ",x)
print("Value of Y: ",y)
print("Value of Z: ",z)
else:
if det1==0 and det2==0 and det3==0:
print("There are infinite solutions")
elif det1!=0 or det2!=0 or det3!=0:
print("There is no solutions")
# take input for coefficient matrix
def equations():
# declare an 3 x 4 list
coef = [x[:] for x in [[0]*4]*3]
for i in range(3):
print("Input for equation",i+1)
coef[i][0]=float(input("Enter the coefficient of X: "))
coef[i][1]=float(input("Enter the coefficient of Y: "))
coef[i][2]=float(input("Enter the coefficient of Z: "))
coef[i][3]=float(input("Enter D"+str(i+1)+": "))
# show the equations
print("\nThe linear equations are: ")
for i in range(3):
print(coef[i][0],"X + ",coef[i][1],"Y + ",coef[i][2],"Z = ",coef[i][3])
return coef
def main():
# call function to take input
coefficient=equations();
# find solution
solution(coefficient)
# driver code
if __name__ == "__main__":
main()
___________________________________________________________________
Input for equation 1
Enter the coefficient of X: 5
Enter the coefficient of Y: 7
Enter the coefficient of Z: 4
Enter D1: 15
Input for equation 2
Enter the coefficient of X: 3
Enter the coefficient of Y: 7
Enter the coefficient of Z: 2
Enter D2: 8
Input for equation 3
Enter the coefficient of X: 5
Enter the coefficient of Y: 9
Enter the coefficient of Z: 3
Enter D3: 6
The linear equations are:
5.0 X + 7.0 Y + 4.0 Z = 15.0
3.0 X + 7.0 Y + 2.0 Z = 8.0
5.0 X + 9.0 Y + 3.0 Z = 6.0
Solution:
Value of X: -8.1
Value of Y: 1.3
Value of Z: 11.6
___________________________________________________________________
Note: If you have
queries or confusion regarding this question, please leave a
comment. I would be happy to help you. If you find it to be useful,
please upvote.