Question: I am trying to create Python code that will do Gauss Elimination with Partial Pivot. Below code works for Gauss Elimination, but I am having
I am trying to create Python code that will do Gauss Elimination with Partial Pivot. Below code works for Gauss Elimination, but I am having trouble getting the Partial Pivot to work. Any help would be greatly appreciated. I have looked and saw that there are others that have asked this question, but specifically I would like to use the code below as part of the GE-PP.
# Importing NumPy Library import numpy as np
n = 4
A = np.array([[1, 2, 1, -1], [3, 2, 4, 4], [4, 4, 3, 4], [2,0,1,5]])
b = np.array([5,16,22,15]) print('A Original: ') print(A) print('b Original: ') print(b) print(' ')
for k in range(0, n-1): for i in range(k+1, n): ratio = A[i,k]/A[k,k] for j in range(k, n): A[i,j]-= ratio*A[k,j] b[i] -= ratio*b[k] print('A: ') print(A) print('b: ') print(b) print(' ') print(' ')
x = np.zeros(n) x[n-1] = b[n-1]/A[n-1, n-1] for i in range (n-2, -1, -1): sum_j = 0 for j in range(i+1, n): sum_j += A[i,j]*x[j] x[i] = (b[i] - sum_j)/A[i,i] print(' The answer for X: ') for i in range(n): print('X%d = %0.2f' %(i,x[i]), end = '\t')
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
