Welcome Guest! To enable all features please Login. New Registrations are disabled.

Notification

Icon
Error

Login


Options
Go to last post Go to first unread
Offline David R  
#1 Posted : 28 October 2022 04:51:20(UTC)
David R

Rank: Newbie

Groups: Registered
Joined: 28/10/2022(UTC)
Posts: 4
United States
Location: Alabama

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?
10x10.jpg19x19.jpg20x20.jpg



Wanna join the discussion?! Login to your SMath Studio Forum forum account. New Registrations are disabled.

Offline Razonar  
#2 Posted : 28 October 2022 06:32:11(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,356
Uruguay

Was thanked: 815 time(s) in 516 post(s)
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.
Offline David R  
#3 Posted : 28 October 2022 07:21:43(UTC)
David R

Rank: Newbie

Groups: Registered
Joined: 28/10/2022(UTC)
Posts: 4
United States
Location: Alabama

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

Offline Razonar  
#4 Posted : 28 October 2022 07:52:40(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,356
Uruguay

Was thanked: 815 time(s) in 516 post(s)
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.

Edited by user 28 October 2022 08:07:30(UTC)  | Reason: Notes added.

Offline oscampo  
#5 Posted : 28 October 2022 12:46:00(UTC)
oscampo


Rank: Advanced Member

Groups: Registered
Joined: 10/12/2009(UTC)
Posts: 246
Man
Colombia
Location: Cali, Colombia

Was thanked: 85 time(s) in 65 post(s)
Originally Posted by: David R Go to Quoted Post
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

Edited by user 28 October 2022 15:20:12(UTC)  | Reason: Not specified

Offline Jean Giraud  
#6 Posted : 28 October 2022 13:52:57(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
Originally Posted by: Jean Giraud Go to Quoted Post
Please, attach the document, mostly the matrix creation.


Page3 MatrixForum.sm (47kb) downloaded 7 time(s).

Offline Jean Giraud  
#7 Posted : 28 October 2022 17:07:20(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
... don't drop dead on that one !

Page7MatrixSolve.sm (9kb) downloaded 6 time(s).
Offline David R  
#8 Posted : 28 October 2022 19:38:35(UTC)
David R

Rank: Newbie

Groups: Registered
Joined: 28/10/2022(UTC)
Posts: 4
United States
Location: Alabama

Here's a demo showing the differences.

MatrixInversionError_Demo.sm (57kb) downloaded 10 time(s).
Offline Razonar  
#9 Posted : 28 October 2022 20:10:41(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,356
Uruguay

Was thanked: 815 time(s) in 516 post(s)
Originally Posted by: David R Go to Quoted Post
Here's a demo showing the differences.

MatrixInversionError_Demo.sm (57kb) downloaded 10 time(s).


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

MatrixInversionError_Demo.pdf (171kb) downloaded 11 time(s).
MatrixInversionError_Demo.sm (198kb) downloaded 5 time(s).

Best regards.
Alvaro.
thanks 1 user thanked Razonar for this useful post.
on 28/10/2022(UTC)
Offline Jean Giraud  
#10 Posted : 28 October 2022 20:16:23(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
Originally Posted by: David R Go to Quoted Post
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 (100kb) downloaded 14 time(s).


Offline David R  
#11 Posted : 28 October 2022 21:26:08(UTC)
David R

Rank: Newbie

Groups: Registered
Joined: 28/10/2022(UTC)
Posts: 4
United States
Location: Alabama

Originally Posted by: Jean Giraud Go to Quoted Post
Originally Posted by: David R Go to Quoted Post
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 (100kb) downloaded 14 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?
Offline Jean Giraud  
#12 Posted : 28 October 2022 21:44:31(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
Originally Posted by: David R Go to Quoted Post
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.
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.