Bug inverting matrices

Bug inverting matrices - Inverting a matrix gives inconsistent answer - Сообщения

#1 Опубликовано: 27.10.2022 22:51:20
David R

David R

0 сообщений из 4 понравились пользователям.

Группа: 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?
Файл не найден.Файл не найден.Файл не найден.



#2 Опубликовано: 28.10.2022 00:32:11
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1675 понравились пользователям.

Группа: 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 Опубликовано: 28.10.2022 01:21:43
David R

David R

0 сообщений из 4 понравились пользователям.

Группа: 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 Опубликовано: 28.10.2022 01:52:40
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1675 понравились пользователям.

Группа: 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 Опубликовано: 28.10.2022 06:46:00
Oscar Campo

Oscar Campo

124 сообщений из 298 понравились пользователям.

Группа: 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 Опубликовано: 28.10.2022 07:52:57
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: User

Wrote

Please, attach the document, mostly the matrix creation.



Page3 MatrixForum.sm (46 КиБ) скачан 61 раз(а).

#7 Опубликовано: 28.10.2022 11:07:20
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: User

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

Page7MatrixSolve.sm (8 КиБ) скачан 62 раз(а).
#8 Опубликовано: 28.10.2022 13:38:35
David R

David R

0 сообщений из 4 понравились пользователям.

Группа: User

Here's a demo showing the differences.

MatrixInversionError_Demo.sm (56 КиБ) скачан 66 раз(а).
#9 Опубликовано: 28.10.2022 14:10:41
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1675 понравились пользователям.

Группа: User

Wrote

Here's a demo showing the differences.

MatrixInversionError_Demo.sm (56 КиБ) скачан 66 раз(а).



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

MatrixInversionError_Demo.pdf (170 КиБ) скачан 70 раз(а).
MatrixInversionError_Demo.sm (197 КиБ) скачан 66 раз(а).

Best regards.
Alvaro.
1 пользователям понравился этот пост
Oscar Campo 28.10.2022 14:16:00
#10 Опубликовано: 28.10.2022 14:16:23
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: 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 КиБ) скачан 62 раз(а).


#11 Опубликовано: 28.10.2022 15:26:08
David R

David R

0 сообщений из 4 понравились пользователям.

Группа: 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 КиБ) скачан 62 раз(а).




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 Опубликовано: 28.10.2022 15:44:31
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: 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.
  • Новые сообщения Новые сообщения
  • Нет новых сообщений Нет новых сообщений