Question: Use python to answer the question below by the code given below. hw2_105000119.py #Linear Algbra 2018 Assignment 2 from hw2_105000119_myfun import mydet, mysolve_cramer, mysolve_adj import
Use python to answer the question below by the code given below.

hw2_105000119.py
#Linear Algbra 2018 Assignment 2 from hw2_105000119_myfun import mydet, mysolve_cramer, mysolve_adj
import numpy as np from datetime import datetime
A = np.array([[1,-1,-1,0,0,0,0,0,0,0], [0,0,1,-1,-1,0,0,0,0,0], [0,0,0,0,1,-1,-1,0,0,0], [0,0,0,0,0,0,1,-1,-1,0], [2,6,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,1,-1], [0,-2,2,2,0,0,0,0,0,0], [0,0,0,-2,2,2,0,0,0,0], [0,0,0,0,0,-2,2,2,0,0], [0,0,0,0,0,0,0,-2,2,2]])
b = np.array([[0],[0],[0],[0],[10],[0],[0],[0],[0],[0]]) n = A.shape[0]
# compute the determinant of A using numpy print(np.linalg.det(A))
# compute the determinant of A using mydet ### your code write in hw2_StudentID_myfun.py ("mydet" function) ### print(mydet(A))
# solve the linear system and measure the execution time tStart = datetime.now() x = np.linalg.solve(A, b) tEnd = datetime.now() print("Time to solve Ax=b is ", tEnd-tStart, " seconds.")
# check the correctness res = np.subtract(np.dot(A, x), b) print(res)
# TODO 1. solve Ax=b using adjoint matrix (using mydet) ### your code write in hw2_StudentID_myfun.py ("mysolve_adj" function) ### # execution time tStart = datetime.now() x = mysolve_adj(A, b, x) tEnd = datetime.now() print("Execution Time = ", tEnd-tStart, " seconds. ")
# rediduals # print(rediduals)
# TODO 2. solve Ax=b using Cramer's rule (using mydet) ### your code write in hw2_StudentID_myfun.py ("mysolve_cramer" function) ### # execution time tStart = datetime.now() x = mysolve_cramer(A, b, x) tEnd = datetime.now() print("Execution Time = ", tEnd-tStart, " seconds. ")
# rediduals # print(rediduals)
hw2_105000119_myfun.py
import numpy as np from datetime import datetime #--------------------------------------------------------------- def mydet(A): """"compute deteminant of A using cofactor expansion.""" n = A.shape[0] if n == 1: return A[0][0] elif n == 2: return A[0][0]*A[1][1]-A[0][1]*A[1][0] else: det = 0 for i in range(0,10,1):
# compute minor M(0,i) ### you can use np.concatenate((A,B), axis=1) to merge two matrices
# compute cofactor A(0,i) = (-1)^{i} det(M(0,i)) ### call mydet recursively to compute det(M)
#--------------------------------------------------------------- def mysolve_adj(A, b, detA): # Now simply solve for x # TODO 1. solve Ax=b using adjoint matrix (using mydet) # return ? #--------------------------------------------------------------- def mysolve_cramer(A, b, detA): # TODO 2. solve Ax=b using Cramer's rule (using mydet) # return ? #------------------------------------------------
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
