# Invert non-symmetric tridiagonal Matrix in MatLab

I tried to invert a non-symmetrical tri-diagonal matrix 'ML' in Matlab, and multiply it with matrix 'MR': y = inv(ML)*MR.

size(ML) = 1001 x 1001

Below the end of the matrix is shown, to give an idea of the magnitude of the values:

Code:
   -0.0096    1.0192   -0.0096         0         0
0   -0.0096    1.0192   -0.0096         0
0         0   -0.0096    1.0192   -0.0096
0         0         0         0    1.0000
Calculating inv(ML) results in a 1001x1001 matrix with values NaN.
So I tried (ML)\(MR), but that gives error: "Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN."

Inverting a almost similar symmetrical tri-diagonal matrix is no problem.

So is it possible to do this inversion?
I would be really grateful with some help!

Huibert

Related Engineering and Comp Sci Homework Help News on Phys.org
CEL
It seems that the problem is not that the matrix is not symmetrical, but that you have zeros in the main diagonal. This makes the determinant zero and the matrix ix consequently singular.

The Electrician
Gold Member
To see if I could get an idea of what the problem might be, I performed a numerical experiment. I created a 10x10 matrix with structure similar to yours and calculated the determinant to see if it was very small.

I then placed a zero on the main diagonal and recalculated the determinant. It did not become zero. When you have a diagonal matrix with non-zero values only on the main diagonal, then a zero on the diagonal will make the matrix singular, but this doesn't necessarily happen with a tridiagonal matrix.

I then created the same matrix, but with exact rational elements rather than floating point values. After inverting it and displaying the inverse, I can see a pattern in the way the exponents of the elements of the inverse are varying. I think that a 1001x1001 matrix will have an inverse whose elements will have such large negative exponents that the floating point system in Matlab may not be able to cope; I think that's your problem.

If you absolutely must invert your 1001x1001 matrix, you may have to use a program such as Maple or Mathematica that can do exact rational arithmetic, and even then it's probably going to give some elements with integers of perhaps 2000 digits in the denominator.

See the attachments:

#### Attachments

• 38.5 KB Views: 1,254
• 70.1 KB Views: 744
Thanks for the effort you took to find this out!

As is seen in TriDiag2, the values far from the main diagonal decrease most. The values of the elements on the three diagonals -1, 0 and 1 are not too small.

Perhaps now I do a stupid suggestion, but would it be possible to only calculate the three diagonals around the main (so -1, 0 and 1)?
Or otherwhise, to set elements to zero when they become too small?

CEL
Thanks for the effort you took to find this out!

As is seen in TriDiag2, the values far from the main diagonal decrease most. The values of the elements on the three diagonals -1, 0 and 1 are not too small.

Perhaps now I do a stupid suggestion, but would it be possible to only calculate the three diagonals around the main (so -1, 0 and 1)?
Or otherwhise, to set elements to zero when they become too small?
Your matrix is upper triangular. So, the determinant is the product of the terms in the diagonal. Since there are zeros on the diagonal, the determinant is zero and the matrx is singular.

The Electrician
Gold Member
Your matrix is upper triangular. So, the determinant is the product of the terms in the diagonal. Since there are zeros on the diagonal, the determinant is zero and the matrx is singular.
Look again. His matrix is not upper triangular, and there are no zeros on the main diagonal.

The elements on the main diagonal are all 1.0192, except for the element on the corner. There are values of -0.0096 above and below the main diagonal. He has only shown the lower right corner, but I think the implication is that the main diagonal is an extension of what we see.

The OP describes it as a tridiagonal matrix, and it appears to me that it is indeed tridiagonal.

The Electrician
Gold Member
Thanks for the effort you took to find this out!

As is seen in TriDiag2, the values far from the main diagonal decrease most. The values of the elements on the three diagonals -1, 0 and 1 are not too small.

Perhaps now I do a stupid suggestion, but would it be possible to only calculate the three diagonals around the main (so -1, 0 and 1)?
Or otherwhise, to set elements to zero when they become too small?
What do you get if you try to calculate the determinant of your matrix ML?

What do you get for the Trace of ML?

CEL
Look again. His matrix is not upper triangular, and there are no zeros on the main diagonal.

The elements on the main diagonal are all 1.0192, except for the element on the corner. There are values of -0.0096 above and below the main diagonal. He has only shown the lower right corner, but I think the implication is that the main diagonal is an extension of what we see.

The OP describes it as a tridiagonal matrix, and it appears to me that it is indeed tridiagonal.
Look again! The elements in the main diagonal are -0.0096, with one zero in the fourth element.
Remember, the OP said that it is a non-symmetrical tri-diagonal matrix. The two secondary diagonals are both above the main diagonal and not one on each side, as with a symmetrical matrix.

The Electrician
Gold Member
Look again! The elements in the main diagonal are -0.0096, with one zero in the fourth element.
Remember, the OP said that it is a non-symmetrical tri-diagonal matrix. The two secondary diagonals are both above the main diagonal and not one on each side, as with a symmetrical matrix.
A matrix with two superdiagonals above the main diagonal is not an unsymmetrical tridiagonal matrix; see:

http://mathworld.wolfram.com/TridiagonalMatrix.html

See:

http://digilander.libero.it/foxes/matrix/convert_unsym_trid_to_sym.pdf

for examples of an unsymmetrical tridiagonal matrix, and a symmetrical tridiagonal matrix.

You are apparently thinking that what the OP has shown:

Code:
   -0.0096    1.0192   -0.0096         0         0
0   -0.0096    1.0192   -0.0096         0
0         0   -0.0096    1.0192   -0.0096
0         0         0         0    1.0000
is from the upper left portion of the 1001 x 1001 matrix he is asking about.
But he said "Below the end of the matrix is shown, to give an idea of the magnitude of the values:"

This is a portion from the end of the matrix (presumably the lower right), not the beginning of the matrix (the upper left).

If this is the lower right corner of the square 1001 x 1001 matrix, then the main diagonal would be as I have shown in red:

Code:
   -0.0096    [COLOR="Red"]1.0192[/COLOR]   -0.0096         0         0
0   -0.0096    [COLOR="Red"]1.0192[/COLOR]   -0.0096         0
0         0   -0.0096    [COLOR="Red"]1.0192[/COLOR]   -0.0096
0         0         0         0    [COLOR="Red"]1.0000[/COLOR]
Perhaps the OP will clarify what he intended.

Sorry for irregular posting. I've to do different courses at the moment...

Indeed the end of the matrix is shown. As far as I can see, the values on the diagonal can not be zero, because I defined ML as:

Code:
ML  = diag(one * 1.0192,0) + diag(one_1 * -0.0096,-1)+ diag(one_1 * <value>,1)
ML(1,1)  = 1;
ML(1,2)  = 0;
ML(end,end-1)  = 0;
ML(end,end)  = 1;
So that can not be the problem.
For the matter of fact I found the previous part of the code whas not complete. The upper diagonal shouls be in the range of 0.4, as shown below:

Code:
    0.4151         0         0         0         0         0
1.0192    0.4151         0         0         0         0
-0.0096    1.0192    0.4152         0         0         0
0   -0.0096    1.0192    0.4153         0         0
0         0   -0.0096    1.0192    0.4153         0
0         0         0   -0.0096    1.0192    0.4154
0         0         0         0         0    1.0000
Now:
trace(ML) = 510.5808
det(ML) = NaN, so can't be calculated.

The Electrician
Gold Member
I think your matrix is not as you think it is.

If this is a 1001x1001 matrix with all 1.0192 on the main diagonal except for the (1,1) and (1001,1001) elements which are 1, then clearly the trace of the matrix should be 999*1.0192+2 which is 1020.18. But you have 510.5808.

Perhaps you should display the entire main diagonal of the matrix and scroll through it; something must be wrong.

Your code snippet shows a quantity "<value>" which is involved in creating the superdiagonal. I see values ranging from .4151 to .4154. Since I don't know what <value> is, I can't create a matrix exactly like yours, but I just used .4151 along the entire superdiagonal

I've shown an 8x8 matrix in the first attached image which I think follows your plan.

In the second image, I increased the size to 1001x1001 and calculated the trace and determinant. I didn't get any arithmetic errors, so I don't know why Matlab isn't returning the same result.

As I said, I think you need to display your matrix and scroll around in it to see if it is constructed as you think it is.

Another thing you might do is to change the size of your matrix. You will see that I have a variable called "siz" which I can easily change. If you will run your program with some smaller sizes, say 10x10, 100x100, 200x200, 500x500 and see what you get for the trace and determinant, I can also run those sized for comparison. Also, tell me what that "<value>" construct is doing so I can do the same.

#### Attachments

• 4.8 KB Views: 555
• 3.7 KB Views: 542
ah, found it indeed..

the 'value' was calculated by interpolation of a series in an other file, which first column not started at 0 but at 10. Therefore that values were not right, and apparently that gave some problems.

Thanks a lot for your help!