Jump to content

  • Log In with Google      Sign In   
  • Create Account

Matrix inversion


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Mantear   Members   -  Reputation: 251

Like
0Likes
Like

Posted 25 February 2013 - 11:48 AM

I'm a linear algebra weakling and am looking for help anywhere I can find it.

 

I'm attempting to calculate the inverse of a 14x14 square matrix using Gauss-Jordan elimination.  This seems to work for some matrix input, but not all.  I end up with an almost zero value on the diagonal, which really blows up the scaling of the values in the matrix.

 

I'm comparing my results to those calculated by Matlab.  For my initial set of matricies, the results match (within rounding differences).  But for the following matrix, they do not.  (sorry for the large data set: if there's a better way to show this please let me know).

 

 

    -10.685512    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    63.545570    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    -213.193800    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    493.092433    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    -831.846712    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    1055.702065    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    -1024.015861    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    762.935063    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000    0.000000
    -434.501577    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000    0.000000
    186.205620    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000    0.000000
    -58.228421    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000    0.000000
    12.560150    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000    0.000000
    -1.672796    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000    -1.000000
    0.103779    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    1.000000
 
Element (14,14) becomes nearly zero [~4.354*10^-12] when eliminating element (14,13) by scaling and subtracting row 13 from row 14.  So when I go to scale row 14 to get the diagonal value to equal 1.0, the scaling just gets huge.  [Note: I'm using long double data types to operate on.]
The Matlab result is also a matrix with very large values, but they are quite different than mine.  I'm guess the very small/very large numbers are throwing things off, but I can't tell where.  Is there a different technique I need to employ to get the same answer as Matlab?


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8611

Like
1Likes
Like

Posted 25 February 2013 - 12:13 PM

Your matrix is very badly conditioned and will be extremely sensitive to numerical errors. If you want the same results as Matlab, then you're simply going to have to do the same calculations as Matlab.

 

As far as I remember, the Gauss-Jordan method is one of the worst general matrix inversion algorithms when it comes to numerical precision. You really need to feed it well-conditioned matrices for it to work.



#3 unbird   Crossbones+   -  Reputation: 6016

Like
0Likes
Like

Posted 25 February 2013 - 12:53 PM

Do you use pivoting ?

#4 Mantear   Members   -  Reputation: 251

Like
0Likes
Like

Posted 25 February 2013 - 01:06 PM

@ Brother Bob;  I'm sure that's good advice.  The problem is I don't understand enough for it to help me.  I wouldn't know how to properly condition the matrix.  I also do not know how Matlab is coming up with its answer.

 

@ unbird: I'm looking at your linked article.  My current solution claims to use row-pivoting; I'm going to attempt to better understand pivoting to see if: A) i'm already pivoting and B) if I'm not, if doing so will help.

 

Thanks.



#5 DT....   Members   -  Reputation: 487

Like
1Likes
Like

Posted 25 February 2013 - 01:39 PM

I ran into this problem years ago and it looks like you may be pivoting on roundoff error.

 

Try partial pivoting.

 

http://en.wikipedia.org/wiki/Pivot_element



#6 Brother Bob   Moderators   -  Reputation: 8611

Like
0Likes
Like

Posted 25 February 2013 - 01:51 PM

@ Brother Bob;  I'm sure that's good advice.  The problem is I don't understand enough for it to help me.  I wouldn't know how to properly condition the matrix.  I also do not know how Matlab is coming up with its answer.

The condition of the matrix is a property of the matrix itself and not something you can just adjust to improve it. Your specific matrix has a specific condition, and the specific matrix you posted is not well conditioned. In this context, you could see the condition of the matrix as how numerically sensitive it is to perform calculations on it.

 

If the exact matrix itself is not important but its general structure is, then you can improve the condition by a technique called regularization. It basically means adding a constant value to the diagonal of your matrix, effectively turning your matrix M into the new matrix M'=M+r*I where r is called a regularization factor and I is the identity matrix.


Edited by Brother Bob, 25 February 2013 - 01:53 PM.


#7 cadjunkie   Members   -  Reputation: 1353

Like
2Likes
Like

Posted 04 March 2013 - 12:15 PM

Matlab has a lot of algorithms it uses to solve problems where matrix inversion might be used. Which one it uses depends on its own analysis of the input matrix to determine what answer is most correct. In fact, Matlab tells you not to use the result of inv(A) to solve an Ax=b problem because of numerical error, probably because inv(A) is a quick naive Gauss elimination algorithm.

 

Looking at your matrix, it could be using a sparse matrix algorithm because a lot of the entries are zero, but the size of the matrix makes me think it's not. If you're not explicitly using a specific algorithm in your code, I think there is a command for it to tell you which algorithms it's using to solve your problem. By the way, which command are you using to get the inverse in Matlab?

 

For your information, naive Gaussian elimination is not numerically stable because of the mathematical roundoff errors that arise from the precision in the computer. Even though you might be calculating with double precision, tiny errors due to roundoff can compound to throw off the entire result if not checked. Partial pivoting checks these small errors so they don't become big. Complete pivoting does this even better, but takes more computations to do this. However, if you're trying to solve an Ax=B problem, don't use matrix inversion. There are better techniques for this, such as LU decomposition.



#8 Nick Alger   Members   -  Reputation: 486

Like
0Likes
Like

Posted 18 April 2013 - 03:27 AM

This is a rank-1 update to a very nice matrix, so you could use the Sherman-Morrison formula:
http://en.wikipedia.org/wiki/Sherman%E2%80%93Morrison_formula

 

bbe580913be6ab3b0f74fc48e47b2440.png

Here

A = 
    [1    -1     0     0     0     0     0     0     0     0     0     0     0     0
     0     1    -1     0     0     0     0     0     0     0     0     0     0     0
     0     0     1    -1     0     0     0     0     0     0     0     0     0     0
     0     0     0     1    -1     0     0     0     0     0     0     0     0     0
     0     0     0     0     1    -1     0     0     0     0     0     0     0     0
     0     0     0     0     0     1    -1     0     0     0     0     0     0     0
     0     0     0     0     0     0     1    -1     0     0     0     0     0     0
     0     0     0     0     0     0     0     1    -1     0     0     0     0     0
     0     0     0     0     0     0     0     0     1    -1     0     0     0     0
     0     0     0     0     0     0     0     0     0     1    -1     0     0     0
     0     0     0     0     0     0     0     0     0     0     1    -1     0     0
     0     0     0     0     0     0     0     0     0     0     0     1    -1     0
     0     0     0     0     0     0     0     0     0     0     0     0     1    -1
     0     0     0     0     0     0     0     0     0     0     0     0     0     1]
u= [-11.7, 63.5,  -213.2,  493.1,  -831.8, 1055.7,  -1024.0,  762.9,  -434.5,  186.2,   -58.2,  12.6,  -1.7,  0.1]'
v=[1,0,0,0,0,0,0,0,0,0,0,0,0,0]'

Interestingly, some matlab calculations reveal this:

 

inv(A)=     
    [1     1     1     1     1     1     1     1     1     1     1     1     1     1
     0     1     1     1     1     1     1     1     1     1     1     1     1     1
     0     0     1     1     1     1     1     1     1     1     1     1     1     1
     0     0     0     1     1     1     1     1     1     1     1     1     1     1
     0     0     0     0     1     1     1     1     1     1     1     1     1     1
     0     0     0     0     0     1     1     1     1     1     1     1     1     1
     0     0     0     0     0     0     1     1     1     1     1     1     1     1
     0     0     0     0     0     0     0     1     1     1     1     1     1     1
     0     0     0     0     0     0     0     0     1     1     1     1     1     1
     0     0     0     0     0     0     0     0     0     1     1     1     1     1
     0     0     0     0     0     0     0     0     0     0     1     1     1     1
     0     0     0     0     0     0     0     0     0     0     0     1     1     1
     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     0     0     0     0     0     0     0     0     0     0     0     0     0     1]
 

 

Also, 

1+v'*invA*u = 1.0000e-06


Now at this point you could use just perform the above sequence of calculations to solve the linear system
So... putting this together

invM = invA-(invA*u*v'*invA)/(1+v'*invA*u)


   1.0e+08 *


  Columns 1 through 11


    0.0100    0.0100    0.0100    0.0100    0.0100    0.0100    0.0100    0.0100    0.0100    0.0100    0.0100
   -0.1069   -0.1069   -0.1069   -0.1069   -0.1069   -0.1069   -0.1069   -0.1069   -0.1069   -0.1069   -0.1069
    0.5286    0.5286    0.5286    0.5286    0.5286    0.5286    0.5286    0.5286    0.5286    0.5286    0.5286
   -1.6033   -1.6033   -1.6033   -1.6033   -1.6033   -1.6033   -1.6033   -1.6033   -1.6033   -1.6033   -1.6033
    3.3276    3.3276    3.3276    3.3276    3.3276    3.3276    3.3276    3.3276    3.3276    3.3276    3.3276
   -4.9909   -4.9909   -4.9909   -4.9909   -4.9909   -4.9909   -4.9909   -4.9909   -4.9909   -4.9909   -4.9909
    5.5661    5.5661    5.5661    5.5661    5.5661    5.5661    5.5661    5.5661    5.5661    5.5661    5.5661
   -4.6740   -4.6740   -4.6740   -4.6740   -4.6740   -4.6740   -4.6740   -4.6740   -4.6740   -4.6740   -4.6740
    2.9553    2.9553    2.9553    2.9553    2.9553    2.9553    2.9553    2.9553    2.9553    2.9553    2.9553
   -1.3897   -1.3897   -1.3897   -1.3897   -1.3897   -1.3897   -1.3897   -1.3897   -1.3897   -1.3897   -1.3897
    0.4724    0.4724    0.4724    0.4724    0.4724    0.4724    0.4724    0.4724    0.4724    0.4724    0.4724
   -0.1099   -0.1099   -0.1099   -0.1099   -0.1099   -0.1099   -0.1099   -0.1099   -0.1099   -0.1099   -0.1099
    0.0157    0.0157    0.0157    0.0157    0.0157    0.0157    0.0157    0.0157    0.0157    0.0157    0.0157
   -0.0010   -0.0010   -0.0010   -0.0010   -0.0010   -0.0010   -0.0010   -0.0010   -0.0010   -0.0010   -0.0010


  Columns 12 through 14


    0.0100    0.0100    0.0100
   -0.1069   -0.1069   -0.1069
    0.5286    0.5286    0.5286
   -1.6033   -1.6033   -1.6033
    3.3276    3.3276    3.3276
   -4.9909   -4.9909   -4.9909
    5.5661    5.5661    5.5661
   -4.6740   -4.6740   -4.6740
    2.9553    2.9553    2.9553
   -1.3897   -1.3897   -1.3897
    0.4724    0.4724    0.4724
   -0.1099   -0.1099   -0.1099
    0.0157    0.0157    0.0157
   -0.0010   -0.0010   -0.0010
 

And it seems to be decently accurate:

norm(invM*M-eye(14))


ans =


   9.5220e-05

This is pretty good, but if you want to go further, in practice you might instead apply the inverse to solve a particular system rather than computing the full inverse. In that case you can just do the matrix multiplications in the sherman-morrison formula to your right hand side vector one at a time.


Edited by Nick Alger, 18 April 2013 - 03:29 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS