Question

In: Computer Science

Write a function in Python that solves the linear system ??=? using Gaussian Elimination, taking ?,?...

Write a function in Python that solves the linear system ??=? using Gaussian Elimination, taking ?,? as input. The function should have two phases: the elimination phase, and the back substitution phase. You can use numpy library.

Solutions

Expert Solution

Ax = b by Gauss elimination Python function

# importing numpy it use as np
import numpy as np

#function takes inputs a and b
def gaussElimin(a,b):
n = len(b)
  
# Elimination Phase
#k takes vales 0 to n-1 one by one
for k in range(0,n-1):
#i takes vales k+1 to n one by one
for i in range(k+1,n):
if a[i,k] != 0.0:
lam = a [i,k]/a[k,k]
a[i,k+1:n] = a[i,k+1:n] - lam*a[k,k+1:n]
b[i] = b[i] - lam*b[k]
  
# Back substitution
for k in range(n-1,-1,-1):
b[k] = (b[k] - np.dot(a[k,k+1:n],b[k+1:n]))/a[k,k]
  
#return value from this function
return b

Code only ( Python function that solves the linear system

??=? using Gaussian Elimination, taking ?,? as input.)

import numpy as np
def gaussElimin(a,b):
n = len(b)
# Elimination Phase
for k in range(0,n-1):
for i in range(k+1,n):
if a[i,k] != 0.0:
lam = a [i,k]/a[k,k]
a[i,k+1:n] = a[i,k+1:n] - lam*a[k,k+1:n]
b[i] = b[i] - lam*b[k]
# Back substitution
for k in range(n-1,-1,-1):
b[k] = (b[k] - np.dot(a[k,k+1:n],b[k+1:n]))/a[k,k]
return b


Related Solutions

Write a MATLAB function function = pivGauss(.....) to solve linear equations using Gaussian Elimination with Partial...
Write a MATLAB function function = pivGauss(.....) to solve linear equations using Gaussian Elimination with Partial Pivoting. You'll need to employ Nested Loops. Thank you !
Write a python function which takes input matrix A and applies gaussian elimination with pivoting strategy...
Write a python function which takes input matrix A and applies gaussian elimination with pivoting strategy which returns P,Q (permutation matrices) L (lower triangular matrix with 1's on the diagonal) and U (upper triangular matrix) such that (P^t)(A)(Q)=LU is correct,
When using Gaussian elimination to solve a system of linear equations, how can you recognize that...
When using Gaussian elimination to solve a system of linear equations, how can you recognize that the system has no solution?
1. Solve linear system using Gaussian elimination a) x1 + 2x2 + x3 = 2 -x1...
1. Solve linear system using Gaussian elimination a) x1 + 2x2 + x3 = 2 -x1 − 3x2 + 2x3 = -3   x1 − 6x2 + 3x3 = -6 b) -2b + 2c = 10 3a + 12b -3c = -6 6a + 18b + 0c = 19 c) 4x - 1y + 4z + 3t = 5 1x - 4z + 6t = 7 5x - 5y + 1z + 2t = -5 4x + 1y + 3z +...
in parts a and b use gaussian elimination to solve the system of linear equations. show...
in parts a and b use gaussian elimination to solve the system of linear equations. show all algebraic steps. a. x1 + x2 + x3 = 2 x1 - x3 = -2 2x2 + x3 = -1 b. x1 + x2 + x3 = 3 3x1 + 4x2 + 2x3 = 4 4x1 + 5x2 + 3x3 = 7 2x1 + 3x2 + x3 = 1
Solve the system using either Gaussian elimination with back-substitution or Gauss-Jordan elimination. (If there is no...
Solve the system using either Gaussian elimination with back-substitution or Gauss-Jordan elimination. (If there is no solution, enter NO SOLUTION. If the system has an infinite number of solutions, express x, y, z, and w in terms of the parameters t and s.) 4x + 12y − 7z − 20w = 20 3x + 9y − 5z − 28w = 36 (x, y, z, w) = ( ) *Last person who solved this got it wrong
Explain what the Gaussian elimination does, by column picture, to a linear system with 3 unknowns...
Explain what the Gaussian elimination does, by column picture, to a linear system with 3 unknowns and 3 equations.
For the following exercises, solve each system by Gaussian elimination.
For the following exercises, solve each system by Gaussian elimination.
Use either Gaussian elimination or Gauss-Jordan elimination to solve the given system or show that no...
Use either Gaussian elimination or Gauss-Jordan elimination to solve the given system or show that no solution exists. (Please show clear steps and explain them) x1 + x2 + x3 = 7 x1 − x2 − x3 = −3 3x1 + x2 + x3 = 11
Solve the following system of equations using Gaussian or​ Gauss-Jordan elimination. w + x + y...
Solve the following system of equations using Gaussian or​ Gauss-Jordan elimination. w + x + y + z = -2 2w +2x - 2y - 2z = -12 3w - 2x + 2y + z = 4 w - x + 7y + 3z = 4
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT