Question

In: Computer Science

Write a python program that can solve system of linear equations in three variables using input...

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

Solutions

Expert Solution


# 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.


Related Solutions

Write a function to solve a system of linear equations of the form Ax= b using...
Write a function to solve a system of linear equations of the form Ax= b using the iterative Gauss-Seidel method. You are free to use any basic MATLAB operation to implement the algorithm (i.e. you may use any combination of loops, indexing, math, etc.), but avoid “built-in” solution methods — you would not be allowed to use the GaussSeidel function if such a function existed. The function must also test for a number of possible issues. If an issue is...
IN PYTHON Write a program to do the following: Solve the a set of equations as...
IN PYTHON Write a program to do the following: Solve the a set of equations as mentioned in "Zybook 5.20 LAB: Brute force equation solver". Instead of the arithmetic operators use your own function defined in a module named calc. You must provide two files (calc.py, brute_force_solver.py) :- 1) calc.py:- Add function named 'add' instead of using operator '+' [10pts] Add function named 'difference' instead of using operator '-' [10pts] Add function named 'product' instead of using operator '*' [10pts]...
Use a software program or a graphing utility to solve the system of linear equations. (If...
Use a software program or a graphing utility to solve the system of linear equations. (If there is no solution, enter NO SOLUTION. If the system has an infinite number of solutions, set x5 = t and solve for x1, x2, x3, and x4 in terms of t.) x1 − x2 + 2x3 + 2x4 + 6x5 = 16 3x1 − 2x2 + 4x3 + 4x4 + 12x5 = 33 x2 − x3 − x4 − 3x5 = −9 2x1...
Use a software program or a graphing utility to solve the system of linear equations. (If...
Use a software program or a graphing utility to solve the system of linear equations. (If there is no solution, enter NO SOLUTION. If the system has an infinite number of solutions, set x5 = t and solve for x1, x2, x3, and x4 in terms of t.) x1 − x2 + 2x3 + 2x4 + 6x5 = 16 3x1 − 2x2 + 4x3 + 4x4 + 12x5 = 33 x2 − x3 − x4 − 3x5 = −9 2x1...
Write your own routine to solve a system of n linear equations and n unknowns using...
Write your own routine to solve a system of n linear equations and n unknowns using the LU decomposition. Input should take an augmented matrix and the number of equations n. Output should be the augmented matrix. L and U matrices and the solution. Also compare your results to those of Matlab’s built in LU decomposition. Use your code to solve the systems given as (a) and (b) below: a) 3a − 2b + c = −3, a − 4b...
Describe at least three distinct ways to solve a system of equations using linear algebra. (Distinct...
Describe at least three distinct ways to solve a system of equations using linear algebra. (Distinct means that the approach is fundamentally different.) Be specific and detailed using linear algebra vocabulary. It might be helpful to pick an example problem and illustrate each of the three methods.   Suppose T1 and T2 are linear transformations from Rn to Rn. Let T(x) = T2(T1(x)) The responses should be very clear like you are writing instructions to someone who doesn’t know the process.  ...
When using Gaussian elimination to solve a system of linear equations, how can you recognize that...
When using Gaussian elimination to solve a system of linear equations, how can you recognize that the system has no solution?
Question No.1: Solve the following system of two linear equations with two variables x and y...
Question No.1: Solve the following system of two linear equations with two variables x and y by “Equating the equations” method. ? = ?? − ?? ??? ? = −? + 5 Question No.2: Is this matrix ? = [ ? ? ? ? ] singular or non-singular? Question No. 3: Solve the following operations with the help of “PEMDAS”. ? ? − (?? ÷ ?) × ? ÷ ? − ? × ? + ?? ÷ ?3 Question No.4:...
Write a system of equations that represents the situation. Then, solve the system using the inverse...
Write a system of equations that represents the situation. Then, solve the system using the inverse of a matrix. Students were asked to bring their favorite fruit to class. 93% of the fruits consisted of bananas, apples, and oranges. If oranges were twice as popular as bananas, and apples were 19 percentage points less popular than bananas, what are the percentages of each individual fruit?
1) Solve the system of linear equations, using the Gauss-Jordan elimination method. (If there is no...
1) Solve the system of linear equations, using the Gauss-Jordan elimination method. (If there is no solution, enter NO SOLUTION. If there are infinitely many solutions, express your answer in terms of the parameters t and/or s.) 3y + 2z = 1 2x − y − 3z = 4 2x + 2y − z = 5 (x, y, z) = 2) Solve the system of linear equations, using the Gauss-Jordan elimination method. (If there is no solution, enter NO SOLUTION....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT