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

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

Prime 7, programing "if" any of these values.

DS_10135119
4-Participant

Prime 7, programing "if" any of these values.

Greetings, im trying to program something that fills a matrix automatically, but you can choose wich rows and columns to make 0 instead.

 

So far i can do it by hand, just setting what row and colum i want (fil=row), and adding extra conditions to the "if" 

 

I tried to use a matrix instead, like row:=[1 2 3], so if i= any of those 3 values, it assign it 0, but it doesnt seem to work with a matrix, i wrote it something like "if i=fil1_i" (_i meaning subscript), but it doesnt compare it to the value on the matrix.

 

Im working with Power Flow equations, in this part i need to do a 2x2 matrix each space being a Jacobian matrix. Im trying to automate it, because this is with 2 "bars" so matrix are 2x2, but when its something like 6 bars i dont want to do everything by hand

 

111.JPG

1 ACCEPTED SOLUTION

Accepted Solutions

I am not absolutely sure what you are trying to achieve, but maybe this function could be of help:

Werner_E_0-1638442843991.png

 

Prime 6 sheet attached

 

View solution in original post

13 REPLIES 13

Please upload your Mathcad Prime worksheet.

I am not absolutely sure what you are trying to achieve, but maybe this function could be of help:

Werner_E_0-1638442843991.png

 

Prime 6 sheet attached

 

There is no attachment in your message.


@VladimirN wrote:

There is no attachment in your message.


Ooops, tnx! I just fixed it.

Bitte 😉.

Here is a slight variation

Werner_E_0-1638452502139.png

 

DS_10135119
4-Participant
(To:Werner_E)

Thanks, that match function is just what i was looking for. Plus i did something so i can put four 2x2 matrix on a single big matrix (since the jacobian matrix is like that) to adjust the subindex so it starts at 0 when you reach each cuadrant (note that i changed matrix origin to 1, hence those i-1).

 

Now since you finally have to work with the actual values of that matrix, im looking into, either dont fill those rows/columns from the matrix so it ends up as a 3x3 in this case. Or take the info from this matrix and remove the required rows and columns, so i dont need to make a new matrix and manually put each element looking at the original matrix.

 

 1234r321.JPG324234534.JPG

So i applied that same program to calculate the actual values, and like i asked before, how could make a new 3x3 matrix with the non 0 values, so i dont have to do it by hand?, either by adding more commands to the program that gives me this result, or to trim the result matrix

 

3rdsa.JPG

There might be better and more elegant ways, but here is a quick first attempt for a function which trims all rows and columns which consist of all zeros:

Werner_E_1-1638476405642.png

Prime 6 worksheet attached

 

 

Here is a version without the "IsElement" function

Werner_E_1-1638495019087.png

 

 

DS_10135119
4-Participant
(To:Werner_E)

Thanks a lot. I was trying to understand your method, the just do columns but transpose and transpose back is a great idea.

 

I have a question since i been using mathcad for this semester only, why is it needed to declare the origin when you start a program? i see many people does it, but i notice i can get away with not doing it at all.

 

Also i didnt knew you could use the input matrix, do some work and output to a different matrix, that way you can fill another matrix with just the info you want. Thats another thing i was trying to figure out.

 

Very grateful for your help. Plus my teacher was impressed that i took the time to learn to program in mathcad since it wasnt necessary.

LucMeekes
23-Emerald III
(To:DS_10135119)

You don't HAVE to set the value of ORIGIN. By default it is set to 0. Through the menu you can set it either 0 or 1, but if you define it on the sheet you can set other values as well.

People may tend to define it on (top of) the sheet to make clear which ORIGIN they use.

 

Success!
Luc

The double transposition was done out of laziness as I did not want to write a trimZeroRows function, too. But if you are using really huge matrices, this could be a performance drawback. But by huge I am talking of matrices with much more than a few hundreds elements.

 

According ORIGIN:

The default value for ORIGIN is 0, but I noticed/assumed from your screenshots that you changed that to 1, probably via the menu (Calculation ribbon). An alternate way to set the value of ORIGIN is to simply write ORIGIN:=... at the top of your sheet which also has the benefit that you immediatly see that the default value is changed. Furthermore you could set any value here, not just 0 and 1 but I can't see much usage for setting a different value - I am used to use the default value 0.
So if you set the desired value via the menu, its not necessary to write ORIGIN:=... in your sheet.

 

When I write utility programs usually I try to make them ORIGIN-aware so they work as expected whatever the value of ORIGIN in the file might be.

So while "for i:=1 .. rows(M)" works OK only if ORIGIN is set to 1, something like "for i:=ORIGIN .. ORIGIN-1+rows(M)" works OK no matter what the value of ORIGIN is. To make it more compact I assign ORIGIN to a variable O at the start of my programs and use O instead of ORIGIN.

 

BTW few days ago we had a discussion here in the forum about undocumented Prime functions and we came across the functions "update1(..)" and "update2(...)" which also can be used to insert values in an already existing matrix:

https://community.ptc.com/t5/PTC-Mathcad/Mathcad-Prime-fold-function/m-p/760579/highlight/true#M198456

The usage of those function may impress your teacher even more 🙂

But I am not sure if they would also work as expected when used with symbolic evaluations - you'll have to give it a try.
Furthermore there is always the potential danger when using undocumented features that they will not be available any more in future versions of the program. Nonetheless its fun playing around with them 😉

 

Top Tags