11 views (last 30 days)

It is because normal inv(Ybus) can not give the answer..The result is giving NaN at every element..

John D'Errico
on 18 Sep 2020

An 800 by 800 matrix is barely what I would call large anymore. Perhaps 40 years ago, yes. Today, it is almost tiny. Ok, not tiny, perhaps mediocre is a better description by today's standards.

Regardless, if in returns NANs, then the matrix is numerically singular. We can not know if it is truly singular or not without virtually infinte precision arithmetic. Essentially, your matrix has no inverse. So using inv on it returns garbage. What can you expect?

So is your matrix truly singular? This can happen because of a bug in your code, if the matrix was computed incorrectly. Or it could happen because of a problem in your mathematics. For example, it is trivially easy to write engineeering code for the elastic behavior of a truss, but have the matrix be singular/ Thus if I do not force some point in the truss to be fixed in space, then the object can be freely translated to any point in the domain with no cost in potential energy. The matrix decribing the elastic deformation of the trus will be singular. But that is a problem in the mathematics.

And of course, a matrix can be mathematically non-singular, but numerically singular, when the mathematics is performed using floating point arithmetic.

We cannot know which is the case. Did you screw up the code? The mathematics? The numerical linear algebra? Where does the problem lie? Who knows. All we are told is inv did not work.

Worse yet, most of the time when you use inv, you are doing so for the wrong reasons. Just because a formula on a printed page shows a matrix inverse does not necessarily mean you need to use inv.

Usually the backslash operator is a better choice for numerical reasons. And sometimes, if your matrix is singular, then a pseudo-inverse MAY be appropriate. But that too is impossible to know, since only you know what the matrix represents and why you think you need to compute an inverse here.

Stephen Cobeldick
on 19 Sep 2020

"...but when I need inverse of such matrix, there must be a way to get it."

No, "getting it" is not a logical consequence of you "needing" it. Just because you "need" an inverse does not mean that there "must be a way to get it": many matrices are not invertible in practice, many have no inverse at all.

Most likely you do not need an explicit inverse: e.g. if you are solving systems of equations you should be using more robust approaches, as has already been mentioned several times on this thread. Once you tell us what you are planning to do with this inverse, someone can (possibly) help you solve it in a better way.

John D'Errico
on 20 Sep 2020

That the determinant of an 800x800 matrix is zero is completely irrelevant.

Feel free to compute the determinant of the matrix

>> det(eye(800))

ans =

1

>> det(eye(800)/3)

ans =

0

>> det(eye(800)*3)

ans =

Inf

Now, is one of those matrices singular? Surely det must be able to tell me that much?

The point is, you have no clue if the matrix is singular, at least not if you are using det to tell you. I really don't care if you remember some notes from some long almost forgotten class, where your teacher told you to use det. They were giving you flat out poor advice, in terms of using a computer to learn if a matrix is singular - to use det. Heck, it may be your current teacher in the class you are taking now. Whoever is telling you to use det and inv is giving out poor advice.

One problem is that it is often convenient to write the inverse of a matrix on paper in a formula, but it is then assumed the person doing the work actually understands enough about linear algebra to know what tools to use. (Sorry, but often the case.)

Instead, use rank or cond to test for singularity.

>> rank(eye(800))

ans =

800

>> rank(eye(800)/3)

ans =

800

>> rank(eye(800)*3)

ans =

800

In all cases rank is not fooled. In fact, cond also tells you the matrix is splendily well behaved.

>> cond(eye(800)/3)

ans =

1

>> cond(eye(800)*3)

ans =

1

So the very first thing you need to do is use some tool that can actually give you valid information. Is the matrix of full rank? If it is not, then hoping to use inv here is a complete waste of time.

Worse, hoping to use a sparse matrix, and then compute the inverse is usually a bad idea, because the inverse matrix will often be a full matrix anyway, unless the matrix is tightly banded. In fact, the way the matrix was constructed, using sparse tools and then computing the inverse will almost certainly result in a full matrix. So I would probably tell you to just use a full matrix here.

Regardless, the first thing you need to do is determine if the matrix is well conditioned. You will immediately learn, since inv has failed, is the matris is indeed singular, at least it will be numerically singular. That you have a PDF from somebody unknown telling you to compute the matrix is not relevant, since we don't even know if you computed it properly. So

- Verify that your matrix does not contain any inf or nan elements in it. That will result in a failure by any tool to solve the problem. An inf or a NaN in the original matrix will suggest a possible bug in your code.
- If all elements are finite, then determine if the matrix is singular. If you are wedded to the idea that the matrix is sparse (How sparse is it?) You can use tools like rank(full(A)), or cond(full(A)), or condest(A), etc.

If the matrix is singular, then you need to understand WHY it is singular. Thus if you made a coding error, then you are wasting your time until you fix the coding error.

I'll next assume the matrix is singular because it is large and is poorly conditioned. In that case, the inverse literally does not exist in terms of a double precision matrix. You might want to decide if it makes sense to use a pseudo-inverse. For that, you will need to disciuss this with your advisor/teacher/boss/mentor/etc., since we don't have anything useful to help you. One page of slides is not sufficient.

Opportunities for recent engineering grads.

Apply TodayFind the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 2 Comments

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/595954-how-can-large-sparse-matrix-y-bus-matrix-of-order-800-800-be-done-inverse-in-matlab#comment_1014256

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/595954-how-can-large-sparse-matrix-y-bus-matrix-of-order-800-800-be-done-inverse-in-matlab#comment_1014256

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/595954-how-can-large-sparse-matrix-y-bus-matrix-of-order-800-800-be-done-inverse-in-matlab#comment_1014265

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/595954-how-can-large-sparse-matrix-y-bus-matrix-of-order-800-800-be-done-inverse-in-matlab#comment_1014265

Sign in to comment.