Finding matrix inverse by Gaussian Elimination With Partial Pivoting

11.2k Views Asked by At

Hello guys I am writing program to compute determinant(this part i already did) and Inverse matrix with GEPP. Here problem arises since i have completely no idea how to inverse Matrix using GEPP, i know how to inverse using Gauss Elimination ([A|I]=>[I|B]). I have searched through internet but still no clue, could you please explain me?

Here is my matlab code (maybe someone will find it useful), as of now it solves AX=b and computes determinant:

function [det1,X ] = GEPP( A, b )
%GEPP
perm=0;

n = length(b);
if n~=m 
error('vector has wrong size');
end
for j = 1:n
    p=j;
    % choice of main element
    for i = j:n
        if abs(A(i,j)) >= abs(A(p,j))
            p = i;
        end
    end
    if A(p,j) == 0
        error('Matrix A is singular');
    end
    %rows permutation
    t       = A(p,:);
    A(p,:)  = A(j,:);
    A(j,:) = t;
    t       = b(p);
    b(p)    = b(j);
    b(j)    = t;
    if~(p==i)
    perm=perm+1;
    end

    % reduction
    for i = j+1:n
        t       = (A(i,j)/A(j,j)); 
        A(i,:)  = A(i,:)-A(j,:)*t; 
        b(i)    = b(i)-b(j)*t; 
    end 
end
%determinant
mn=1;
for i=1:n
    mn=mn*A(i,i);
end
det1=mn*(-1)^perm;
% solution
X   = zeros(1,n); 
X(n) = b(n)/A(n,n); 

if (det1~=0)
for i = 1:n
    s = sum( A(i, (i+1):n) .* X((i+1):n) ); 
    X(i) = (b(i) - s) / A(i,i); 
end
end
end
1

There are 1 best solutions below

5
On

It really is no different than doing Gaussian Elimination with Pivoting, but repeating each step to an augmented matrix.

We would set up and do GEPP to both sides of the augmented matrix $[A | I]$.

If the matrices condition number plays nice, we will end up with $[I | A^{-1}]$. Note, I would recommend you also look into the Crout and the Doolittle procedures as better alternatives! Also, it is worth noting that you must be careful with single precision, rounding and numerical issues!

So, lets do a difficult example of what I described above so you can see it in practice (so, just take whichever variant of GEPP you are using and set up the augmented matrix as above and see if you end up with the inverse as I show below).

$$A = \left[\begin{array}{ccccc} 1 & 1/2 & 1/3 & 1/4 & 1/5 \\ 1/2 & 1/3 & 1/4 & 1/5 & 1/6 \\ 1/3 & 1/4 & 1/5 & 1/6 & 1/7 \\ 1/4 & 1/5 & 1/6 & 1/7 & 1/8 \\ 1/5 & 1/6 & 1/7 & 1/8 & 1/9 \end{array}\right]$$

Do pivoted Gaussian Elimination (row reduction):

Setup the augmented matrix $[A | I]$ as:

$$\left[\begin{array}{ccccc|ccccc} 1 & 1/2 & 1/3 & 1/4 & 1/5 & 1 & 0 & 0 & 0 & 0\\ 1/2 & 1/3 & 1/4 & 1/5 & 1/6 & 0 & 1 & 0 & 0 & 0\\ 1/3 & 1/4 & 1/5 & 1/6 & 1/7 & 0 & 0 & 1 & 0 & 0\\ 1/4 & 1/5 & 1/6 & 1/7 & 1/8 & 0 & 0 & 0 & 1 & 0\\ 1/5 & 1/6 & 1/7 & 1/8 & 1/9 & 0 & 0 & 0 & 0 & 1 \end{array}\right]$$

Perform the following GEPP steps:

  • Subtract 1/2 (row 1) from row 2
  • Multiply row 1 by 60
  • Multiply row 2 by 120
  • Multiply row 3 by 420
  • Multiply row 4 by 840
  • Multiply row 5 by 2520
  • Subtract 7/3 (row 1) from row 3
  • Multiply row 3 by 3
  • Subtract 7/2 (row 1) from row 4
  • Multiply row 4 by 2
  • Subtract 42/5 (row 1) from row 5
  • Multiply row 5 by 5
  • Swap row 2 with row 5
  • Subtract 1/8 (row 2) from row 3
  • Multiply row 3 by -8
  • Subtract 3/20 (row 2) from row 4
  • Multiply row 4 by -20
  • Subtract 1/84 (row 2) from row 5
  • Multiply row 5 by -84
  • Swap row 3 with row 5
  • Subtract 2/3 (row 3) from row 4
  • Multiply row 4 by 3
  • Subtract 8/15 (row 3) from row 5
  • Multiply row 5 by 15
  • Swap row 4 with row 5
  • Subtract 3/7 (row 4) from row 5
  • Multiply row 5 by 7/8
  • Subtract 128 (row 5) from row 4
  • Subtract 224 (row 5) from row 3
  • Subtract 896 (row 5) from row 2
  • Subtract 12 (row 5) from row 1
  • Subtract 3 (row 4) from row 3
  • Subtract 15 (row 4) from row 2
  • Subtract 5/21 (row 4) from row 1
  • Subtract 8 (row 3) from row 2
  • Subtract 1/6 (row 3) from row 1
  • Subtract 1/28 (row 2) from row 1
  • Divide row 1 by 60
  • Divide row 2 by 840
  • Divide row 3 by 120
  • Divide row 4 by 63

Our final solution using GEPP gives us (in this case), the desired $[I | A^{-1}]$ as:

$$\left[\begin{array}{ccccc|ccccc} 1 & 0 & 0 & 0 & 0 & 25 & -300 & 1050 & -1400 & 630\\ 0 & 1 & 0 & 0 & 0 & -300 & 4800 & -18900 & 26880 & -12600\\ 0 & 0 & 1 & 0 & 0 & 1050 & -18900 & 79380 & -117600 & 56700\\ 0 & 0 & 0 & 1 & 0 & -1400 & 26880 & -117600 & 179200 & -88200\\ 0 & 0 & 0 & 0 & 1 & 630 & -12600 & 56700 & -88200 & 44100 \end{array}\right]$$