Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment, you will practice the following programming skills: a. conve

ID: 3890299 • Letter: I

Question

In this assignment, you will practice the following programming skills: a. converting a program functionality description into a functional program b. for loops and if-else statements c. matrix manipulation d. built-in matrix functions e. user input checking f. program flow Introduction This week you are going to write a function script that will solve a system of linear algebraic equations. Your script will be passed a matrix [Al and a column vector b) which are from the IAlx b Your function will perform Gauss Elimination with partial pivoting to calculate the x) matrix equation: solution vector

Explanation / Answer

Use Python2 to run the script.Change input in the __main__ function. import numpy as np import pprint def GaussianEliminatio(A, b): ''' Gaussian elimination with partial pivoting. % input: A is an n x n nonsingular matrix % b is an n x 1 vector % output: x is the solution of Ax=b. % post-condition: A and b have been modified. ''' n = len(A) if b.size != n: raise ValueError("Invalid argument: incompatible sizes between A & b.", b.size, n) # k represents the current pivot row. Since GE traverses the matrix in the upper # right triangle, we also use k for indicating the k-th diagonal column index. for k in xrange(n-1): #Choose largest pivot element below (and including) k maxindex = abs(A[k:,k]).argmax() + k if A[maxindex, k] == 0: raise ValueError("Matrix is singular.") #Swap rows if maxindex != k: A[[k,maxindex]] = A[[maxindex, k]] b[[k,maxindex]] = b[[maxindex, k]] for row in xrange(k+1, n): multiplier = A[row][k]/A[k][k] #the only one in this column since the rest are zero A[row][k] = multiplier for col in xrange(k + 1, n): A[row][col] = A[row][col] - multiplier*A[k][col] #Equation solution column b[row] = b[row] - multiplier*b[k] print A print b x = np.zeros(n) k = n-1 x[k] = b[k]/A[k,k] while k >= 0: x[k] = (b[k] - np.dot(A[k,k+1:],x[k+1:]))/A[k,k] k = k-1 return x if __name__ == "__main__": A = np.array([[1.,-1.,1.,-1.],[1.,0.,0.,0.],[1.,1.,1.,1.],[1.,2.,4.,8.]]) b = np.array([[14.],[4.],[2.],[2.]]) pprint.pprint (GaussianEliminatio(A,b))