Write a program in Python to solve a linear system of the fo

Write a program in Python to solve a linear system of the form Ax = b by Gaussian elimination with scaled partial pivoting. You 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 can assume A is a square matrix. Your calling statements should be: def gaussy(A, b, n) Your output statement should be of the form: The solution vector is XXXXXX.

Solution

\'\'\'

Python script that Solves Ax = b by Gauss elimination with
    scaled row pivoting

Here n is number of rows or column
\'\'\'  
from numpy import zeros,argmax,dot
import swap
import error

def gaussy(a,b,n):
    n = len(b)
    tol=1.0e-12
# Set up scale factors
    s = zeros(n)
    for i in range(n):
        s[i] = max(abs(a[i,:]))

    for k in range(0,n-1):

      # Row interchange, if needed
        p = argmax(abs(a[k:n,k])/s[k:n]) + k
        if abs(a[p,k]) < tol: error.err(\'Matrix is singular\')
        if p != k:
            swap.swapRows(b,k,p)
            swap.swapRows(s,k,p)
            swap.swapRows(a,k,p)

      # Elimination
        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]
    if abs(a[n-1,n-1]) < tol: error.err(\'Matrix is singular\')

# Back substitution
    b[n-1] = b[n-1]/a[n-1,n-1]
    for k in range(n-2,-1,-1):
        b[k] = (b[k] - dot(a[k,k+1:n],b[k+1:n]))/a[k,k]
    return b

a=[[1,2],[13,43]]
b=[1,2]

print \"The solution vector is \"
print gaussy(a,b,len(b))

 Write a program in Python to solve a linear system of the form Ax = b by Gaussian elimination with scaled partial pivoting. You should pass the matrix A and th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site