Write a program in Python to solve a linear system of the form Ax = b by Gaussian elimination with sealed partial pivoting- Yon should pass the matrix A and the right hand side vector b, and the value of n (the number of rows and columns in the matrix). You will need to have vectors for the scaling factors and the index of the rows internal to the program. You ran assume A is a square matrix. Your calling statements should be: def gauss (A, b, n) Your output statement should be of the form: The solution vector is XXXXXX. Test your program on the following: The Larger Numerical Example on page 87 of the text. The system in exercise #13b on page 99. The system Ax = b, where A = [a_ij] and a_i, j = 1/i + j + 1, i + j + 1 for i, j = 0...n - 1 and b[i] = sigma_j = 0^n - 1 1/i + j + 1 (which is the sum of the entries in the row of A (so the solution is all ones). The matrix A is the Hilbert matrix and the way the b vector is constructed makes tin- solution obvious. Run your program for n = C and n = 15, which are 7 times 7 and 16 times 16 matrices. Compute the 2-norms of the residual vector, r, and the error vector c = x - x. Use Maple to compute the condition number of both matrices and report those numbers in your Python window as a comment. Are the solutions accurate or not? Could we predict this from the condition numbers? Discuss your findings in complete sentences. Write a program to solve a linear system of the form Ax = b by Gauss-Seidel iteration. You should pass the matrix A. solution vector b. the initial guess vector x, the size of the system X. and the relative error tolerance in the solution. Overwrite the x vector with the solution. Make sure you have bound on the number of iterations and return an information message if the maximum number of iterations has been exceeded. Return the solution and the number of iterations it took to converge. Test your program on the system in exercise #7 above to verify the accuracy of your code. Run your program on the system of equations 9x_1 - 3x_2 = 6 -2x_1 + 8x_2 = -4 with starting vector x = (0 0)^T. Change the coefficient of x_1 in the first equation from 9 to 1 see what happens to your program.
import numpy as np
def gaussy(A, b, n1):
\'\'\'
Gaussian elimination with scaled partial pivoting.
input: A is an n x n numpy matrix
b is an n x 1 numpy array
output: x is the solution of Ax=b
with the entries permuted in
accordance with the pivoting
done by the algorithm
post-condition: A and b have been modified.
\'\'\'
n = len(A)
if (n*2) != n1:
raise ValueError(\"Invalid argument: incompatible sizes between\"+
\"A & n\", n, n1)
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.
# Elimination
for k in range(n-1):
#Eliminate
for row in range(k+1, n):
multiplier = A[row,k]/A[k,k]
A[row, k:] = A[row, k:] - multiplier*A[k, k:]
b[row] = b[row] - multiplier*b[k]
# Back Substitution
x = np.zeros(n)
for k in range(n-1, -1, -1):
x[k] = (b[k] - np.dot(A[k,k+1:],x[k+1:]))/A[k,k]
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.]])
print gaussy(A,b,8)
Output in url : http://ideone.com/41yGYp