Cramer\'s Rule can be used to solve sets of linear equations expressed in the fo
ID: 2074313 • Letter: C
Question
Cramer's Rule can be used to solve sets of linear equations expressed in the form Ax=b where A is a square matrix of constants, b is a vector of constants, and x isa vector of unknowns. Cramer's rule is generally not the best way to solve large sets of equations, but it is an excellent method for solving 2-equations and 2-unknowns. That is because there is a very simple equation for calculating the determinant of a 2 by 2 square matrix then determinant(A) = a-d-b-c Write a Python program that meets the following requirements a) The program must have a function named det2(A). This function has one argument named A. A is b) The program has a function named solve2(A, b). This function has two arguments named A and required to be a 2 by 2 matrix. This function must calculate and return the determinant of A. b. A is required to be a 2 by 2 square matrix, and b is required to be a 2-term vector (list). This function must calculate and return the solution to the set of equations defined by A x = b. This function does NOT print anything. This function MUST use Cramer's Rule as the solution method. This function must use (call) det2D to calculate the necessary determinants as part of Cramer's Rule If you need more information on Cramer's Rule, you may google it c) The main program will creates the data (A1. bl. A2, b2) shown below. It will then pass (A1, B1) them to the solve2 function and print the answer. Then, it will pass (A2, b2) to the solve2 function and print the answer. Huge hint: since this program only works for 2 equation and 2 unknown problems, this entire program can be written without any IF's or LOOPS solve Al.x=bl for A2 = b2 = solve A2.x=b2 for x 5 -4Explanation / Answer
The Python code is given below. It is a single file.
def det2(A):
return A[0][0]*A[1][1]-(A[0][1]*A[1][0])
def solve2(A,b):
A1 = [[b[0],A[0][1]],[b[1],A[1][1]]]
x = det2(A1)/det2(A)
A2 = [[A[0][0],b[0]],[A[1][0],b[1]]]
y = det2(A2)/det2(A)
return [x,y]
A1 = [[1.,-3.],[2.,-4.]]
b1 = [-1.,1.]
A2 = [[2.,-2.],[5.,-4.]]
b2 = [2.,-3.]
[x1,y1] = solve2(A1,b1)
[x2,y2] = solve2(A2,b2)
print(' The solution to A1.x = b1 is ' + str([x1,y1]))
print(' The solution to A2.x = b2 is ' + str([x2,y2]))
I hope you understood the process. Please rate.