cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Translate the entire conversation x

Matrix, partial derivate and 'for' loop

CLE_MW
6-Contributor

Matrix, partial derivate and 'for' loop

Hi,

[using MP 11].

I'd like to build up a matrix (n x n, square and symmetric), with the help of a loop.

The problem is that each of the terms in this matrix is the second partial derivate of a function of n variables . I couldn't find a way to include this in a program (Match of the variable and the loop counter)

My goal is to program in a way that I don't get any "red square" or "this variable is undefined" in the layout.

 

In the enclosed sheet, the terms of the matrix are colour-highlighted. (The context is civil engineering, and the determination of the buckling factor of a column, here with a varying cross-section and varying compressive force).

 

Eager to see any insightful idea! Thanks.

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:Werner_E)

And here finally the shortest approach. Unfortunately at the cost of calculation time. Maybe using the Nabla operator isn't so efficient.

Werner_E_1-1753285697588.png

Prime 11 sheet attached

View solution in original post

5 REPLIES 5
Werner_E
25-Diamond I
(To:CLE_MW)

Prime capabilities are quite limited when it comes to matrix functions and derivatives with respect to vector arguments.

So it looks to me that you already have found the most suitable workaround.

The red error stems from the numeric engine and does not affect the ability of symbolic calculations, as you found out.

To avoid the red error

Werner_E_0-1753268333117.png

You would have to define a function in ALL five arguments

Werner_E_1-1753268366888.png

as Primes numeric can't 'know' that function Pi is defined in a way so that all variables except k.EV cancel/fall out.

Werner_E_2-1753268494734.png

You could use symbolic evaluation to come around this annoyance

Werner_E_3-1753268535884.png

But the crucial point is that in order to automatically create your matrix we need the 0 and 1 in the variable name to be matrix indices. And this is where Prime imposes a serious limitation, as Prime does not allow matrix elements to be defined as functions—not if we have to define the matrix itself as a function (as would be necessary here).

Werner_E_4-1753268779912.png

Prim moans that it does not understand the syntax. And, no, Prime won't even allow to type the matrix indices right after the function name "M" and in front of the argument parentheses.

 

So I guess  that you will have to stay with the way you already had chosen. You may hide the definitions with the red errors in a collapsed region if this is acceptable.

BTW, you could create the final matrix also with the "submatrix" function, as M.C already is the full 4x4 matrix.

Werner_E_6-1753269760780.png

The red error can be avoided if you follow the definition by a symbolic evaluation

Werner_E_7-1753269837125.png

In case you need to see the matrix and its determinant for certain values of k.EV and would like to use numeric evaluation (maybe because of unit simplification), you may (re)define M.calc as a function (symbolic inline evaluation is mandatory here):

Werner_E_8-1753270043842.png

 

 

Another idea could be to create an auxiliary function with a vector argument "u"

Werner_E_5-1753268998363.png

and use this function in a program with nested for-loops to define the matrix without having to predefine the various matrix elements M.C.
But here we encounter another limitation of Prime as Prime won't allow to define a derivative with respect to a vector element.

 

So I can't think of a more elegant and straight forward method to automatically create that matrix (actually a function of k.EV) based on the dimension variable n.t.

 

 

CLE_MW
6-Contributor
(To:Werner_E)

Thanks @Werner_E  for quick reply and pointing the limits of MP11, You spotted well that "crucial point".

I conclude there's no "elegant" way to combine the partial derivation + the loops + the matrices. However, you pointed me to idea of a straightforward definition of the matrix as follows : 

CLE_MW_1-1753273478802.png

Including the symbolic evaluation to avoid the red tags.

Then I'll use a "for" loop to resize it as per the targeted level of precision, which works.

CLE_MW_2-1753273515533.png

Not as short as I would have wished, but fair enough for me.

Thanks again!

 

 

Werner_E
25-Diamond I
(To:CLE_MW)

Using "submatrix" as show above instead of the nested for-loops makes it a tiny bit shorter.

 

Here is version which makes calculating the full 4x4 matrix a little bit easier using the gradient (Nabla) operator.

Could not find a way to eliminate the repetition of the second derivatives.

The approach does not make advantage of the symmetry of second derivatives (theorem of Schwarz, Clairot, Young).

Werner_E_0-1753284232918.png

 

Prime 11 sheet attached

Werner_E
25-Diamond I
(To:Werner_E)

And here finally the shortest approach. Unfortunately at the cost of calculation time. Maybe using the Nabla operator isn't so efficient.

Werner_E_1-1753285697588.png

Prime 11 sheet attached

CLE_MW
6-Contributor
(To:Werner_E)

Waow, that's efficient! Thanks @Werner_E . I will test this on larger problem to check how longer is calculation time. Thanks for pointing me to the Nabla operator.

Announcements

Top Tags