Answered step by step
Verified Expert Solution
Question
1 Approved Answer
given this function in python Write a program that uses the LU decomposition function that I gave you above and solves the problem Ax= b
given this function in python
Write a program that uses the LU decomposition function that I gave you above and solves the problem Ax= b , given the matrix A and the solution vector b. You can check your program with the solutions given by using numpy.linalg.solve
def LU_decomposition (A): "" "Perform LU decomposition of a square matrix (numpy array) using the Doolittle factorization.""" #import numpy import numpy as np make sure type is correct (or you could set this up to try and convert the type instead of raising an error.) if type (A) != np.ndarray: raise TypeError( 'Input must be a numpy array, type f will not work' .format (type (A))) #check dimension to ensure A is square #print ( np. shape ( A) [ 0 ] ) if np. shape (A) [0] != np. shape (A) [1]: raise ValueErrorInput matrix is not square, shape will not work' .format (np.shape (A))) L - np.zeros_like (A) Unp.zeros_like(A) N- np.size(A, 0) for k in range (N): L(k, k] = 1 for j in range (k+1, N): for i in range(ktl, N): U[k, j-Alk, j] - np.dot (L[k, :k], Ul:k, j]) return L, U def LU_decomposition (A): "" "Perform LU decomposition of a square matrix (numpy array) using the Doolittle factorization.""" #import numpy import numpy as np make sure type is correct (or you could set this up to try and convert the type instead of raising an error.) if type (A) != np.ndarray: raise TypeError( 'Input must be a numpy array, type f will not work' .format (type (A))) #check dimension to ensure A is square #print ( np. shape ( A) [ 0 ] ) if np. shape (A) [0] != np. shape (A) [1]: raise ValueErrorInput matrix is not square, shape will not work' .format (np.shape (A))) L - np.zeros_like (A) Unp.zeros_like(A) N- np.size(A, 0) for k in range (N): L(k, k] = 1 for j in range (k+1, N): for i in range(ktl, N): U[k, j-Alk, j] - np.dot (L[k, :k], Ul:k, j]) return L, UStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started