Bug inverting matrices

Bug inverting matrices - Inverting a matrix gives inconsistent answer - Messages

#1 Posted: 10/27/2022 10:51:20 PM
David R

David R

0 likes in 4 posts.

Group: User

When inverting a matrix, I get a different output between (1) evaluating the inverse of a matrix and (2) defining a new variable as the inverse of the original matrix and evaluating the new variable. The outputs get worse the larger the matrix is. See attached images. Is this a bug in the matrix inversion algorithm perhaps?
File not found.File not found.File not found.



#2 Posted: 10/28/2022 12:32:11 AM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1675 posts.

Group: User

Hi David. The difference in the values may be due to whether one of the versions has an implicit eval() and the other does not. To verify that it would be necessary to see the original matrix K. In any case, to know if there are going to be differences in the calculation, the condition numbers are used, available in mathcad, but I don't know if they are in any SMath plugin.

Best regards.
Alvaro.
#3 Posted: 10/28/2022 1:21:43 AM
David R

David R

0 likes in 4 posts.

Group: User

Thanks, Alvaro. I've attached a screenshot of part of the K matrix in case that will spark any thoughts, but not sure if that will be helpful. I could upload the .sm file as well if that would be easier.
When you say one of the versions, you mean 'K^-1 =' vs 'Kinv = K^-1; Kinv = '. If it was a condition number problem, would you expect that to show up simply because we've defined a variable rather than evaluating without a new variable definition? I'm just specifically confused as to why:

K^-1 = one answer

Kinv = K^-1; Kinv = a different answer.

Is something happening related to the condition number when saving the inverse as a variable that doesn't happen when evaluating directly?

Thanks again for your thoughts.
K matrix.jpg

#4 Posted: 10/28/2022 1:52:40 AM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1675 posts.

Group: User

Hi. Condition numbers are for check if a problem involving some matrix is a problem with a low condition number is said to be well-conditioned, while a problem with a high condition number is said to be ill-conditioned. What the inverse() SMath's function do is a numerical procedure, not a symbolic matrix inversion. So, it could have a lot of different issues. This are the available condition numbers in Mathcad. If you attach the smath dile someone here can tell you more about. Notice that there are some plugins that have the matrix inversion procedures too. For instance dn_MatrixInverse, but there are others, I think.

What I say about eval() is about an SMath function that make some round-up and truncation errors. In your case you need to check if you get the same different result with KK := submatrix(K,1,20,1,20) and KK := eval( submatrix(K,1,20,1,20) )

Finally, your K matrix is of the type 'sparce'. Some programs can detect that and use some specific algorithms for such cases, like matlab.

Best regards.
Alvaro.

PD. Perhaps the condition number requires some explanation. Suppose you solved a purely resistive electrical circuit by direct calculation, either nodes or meshes. If you see it from a simple theoretical point of view, you could have used Kramer's rule to solve it, since it is a linear system. And you know well that for there to be a unique solution, the determinant of the matrix of the system has to be different from zero. But we are talking about a real circuit. And the resistors are not exact, they come with a 10% error, for example. So how do I know if the circuit I designed is going to be stable or a mess? You justify by calculating some of the condition numbers to the system matrix. So you say that the problem you solved is well conditioned because you got a low value for that number, which is going to indicate that there are going to be no problems using that matrix in a linear system. That is, you never provide only one solution to a practical problem. You also have to study its stability. Condition numbers are suitable for linear systems with matrices. For differential equations there are also methods to study the stability of the proposed solutions.
#5 Posted: 10/28/2022 6:46:00 AM
Oscar Campo

Oscar Campo

124 likes in 298 posts.

Group: Moderator

Wrote

Thanks, Alvaro. I've attached a screenshot of part of the K matrix in case that will spark any thoughts, but not sure if that will be helpful. I could upload the .sm file as well if that would be easier.
When you say one of the versions, you mean 'K^-1 =' vs 'Kinv = K^-1; Kinv = '. If it was a condition number problem, would you expect that to show up simply because we've defined a variable rather than evaluating without a new variable definition? I'm just specifically confused as to why:

K^-1 = one answer

Kinv = K^-1; Kinv = a different answer.

Is something happening related to the condition number when saving the inverse as a variable that doesn't happen when evaluating directly?

Thanks again for your thoughts.



Hi David,
From my point of view, your matrix looks like a stiffness matrix from a Finite Elements Method approach, if that is the case, I was working on that some time ago, and I found some issues trying to solve the equation [x]=[K]^-1*[F]. My solution was to use Gauss elimination or a solver for a banded version of [K].
Anyway, as mention Alvaro, it would be great if you attach your .sm file to check it.

Best,
Oscar
#6 Posted: 10/28/2022 7:52:57 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Wrote

Please, attach the document, mostly the matrix creation.



Page3 MatrixForum.sm (46 KiB) downloaded 51 time(s).

#7 Posted: 10/28/2022 11:07:20 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

... don't drop dead on that one !

Page7MatrixSolve.sm (8 KiB) downloaded 59 time(s).
#8 Posted: 10/28/2022 1:38:35 PM
David R

David R

0 likes in 4 posts.

Group: User

Here's a demo showing the differences.

MatrixInversionError_Demo.sm (56 KiB) downloaded 64 time(s).
#9 Posted: 10/28/2022 2:10:41 PM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1675 posts.

Group: User

Wrote

Here's a demo showing the differences.

MatrixInversionError_Demo.sm (56 KiB) downloaded 64 time(s).



Hi. The attached shows how using eval for get the same inverse matrix in both cases.

MatrixInversionError_Demo.pdf (170 KiB) downloaded 62 time(s).
MatrixInversionError_Demo.sm (197 KiB) downloaded 58 time(s).

Best regards.
Alvaro.
1 users liked this post
Oscar Campo 10/28/2022 2:16:00 PM
#10 Posted: 10/28/2022 2:16:23 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Wrote

When inverting a matrix, I get a different output between (1) evaluating the inverse of a matrix and (2) defining a new variable as the inverse of the original matrix and evaluating the new variable


Set your matrix inversion to NUMERIC ...yellow in there.

Page3 MatrixForum.sm (99 KiB) downloaded 60 time(s).


#11 Posted: 10/28/2022 3:26:08 PM
David R

David R

0 likes in 4 posts.

Group: User

Wrote

Wrote

When inverting a matrix, I get a different output between (1) evaluating the inverse of a matrix and (2) defining a new variable as the inverse of the original matrix and evaluating the new variable


Set your matrix inversion to NUMERIC ...yellow in there.

Page3 MatrixForum.sm (99 KiB) downloaded 60 time(s).




That worked perfectly. Thank you! Is there an Explain It Like I'm 5 as to why we need to specify the numeric solver rather than symbolic to get the right answer?
#12 Posted: 10/28/2022 3:44:31 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Wrote

That worked perfectly. Thank you! Is there an Explain It Like I'm 5 as to why we need to specify the numeric solver rather than symbolic to get the right answer?


By default, everything you define in Smath is symbolic.
Matrix inverse are numeric from libraries [mostly Cholesky]
Thus, specify inverse as numeric to comply with Smath plugins.
Will you sleep better ! Jean.
  • New Posts New Posts
  • No New Posts No New Posts