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

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

a loop creating a matrix with 2 columns

mkraczla
1-Newbie

a loop creating a matrix with 2 columns

Hi,

I am trying to write a simple command to create a matrix with two columns one of which would display a value of the governing shear force and the other with text (as in the screen below). When I loop it, the result is not what I intended. I would be grateful if someone could indicate what is wrong.

1.JPG

Regards,

MK

1 ACCEPTED SOLUTION

Accepted Solutions

Here are examples of what I was talking about:

1)

2)

Regards,

Werner

View solution in original post

24 REPLIES 24

Please don't post just pictures, better to attach your worksheet.

BTW, you are not doing anything wrong, its Mathcad not passing the whole range as argument, but when a function is called with a range as argument and is not assigned a variable, an implicit loop is executed and the function is called separately for every value (similar, but not the same as vectorized function calls).

Ranges are a beasty thing and should be used just for a few couple of things. Passing a range as function argument is not one of them. You will experience also other strange effects. Just try to assign the outcome of your function to a variable 😉

You see, that way you get what you are asking for. Nevertheless I would rather create x as a vector, not a range.

Or you might consider adding additional arguments to your function - start value, second value (or step size - up to you), end value.

WE

Here are examples of what I was talking about:

1)

2)

Regards,

Werner

thank you all,

I would like to ask one more question, perhaps related to this thread. I have a while loop and even though the outcome is correct, there is this little thing that has been bugging me; namely I can extract only results for one variable (please see the capture + the file). V.s in the case of the capture.  I would like to know if there is by any chance a way to extract several indicated variables; in a form of a matrix for example, thus V.c, V.s and V.R and so on.

Capture1.JPGCapture2.JPG

Regards,

MK

Again, against our advice, you are using ranges as function arguments.

Here is one possible (commented) approach (of course also attached as file):

WE

You might be interested in the work of Stuart which I attached in this thread -> Re: Variable Type "Generator" in Function

No, I got your point. I simply didn't have enough time to correct it

Thanks for helping me with this one as well

Now I am trying to get rid of a range of x combining a for loop with a while loop but not succeeding. Instead of a value V for each x calculated within the while loop, I receive only one value which on top of that is incorrect. Could you please give me an example of how to use a combination of a for loop with a while loop. If possible on the example I attached in the post above.

Regards,

MK

Unfortunately I don't understand what you are trying to do.

Maybe its best if you

1) show, what you are trying

and

2) show, what kind of result you expect.

WE

Hi,

the outcome would be as in the previous posts; nothing more. The combination of the for and while loop would look like

Capture.JPG

and the result is (just one value which is not what I want):

Capture1.JPG

but instead my intention is to get a value V.R (or multiple results from the while loop as discussed in this thread) for a variable from the range variable thus for 425mm then 450mm, 475mm etc.

Please let me know if it is clear now.

Kind regards,
MK

RichardJ
19-Tanzanite
(To:mkraczla)

As Werner notes, the problem is that x is a range variable, not a vector. A range variable is an iterator, so Answer(x) is called for each value of x, not once for all values of x. So it's called for x=0.25m, which yields a one row two column matrix, then it's called for x=0.3m, which yields a one row two column matrix, etc. Make x a vector and you should get the expected results.

The crazy thing is, that assigning the function call to a variable gets exactly the demanded matrix. I would have thought that Mathcad would throw an error.

RichardJ
19-Tanzanite
(To:Werner_E)

I would expect a nested matrix.

Richard Jackson wrote:

I would expect a nested matrix.

And neither is the case.

I thought of the error because usually we don't get a vector if we call a function with a range as argument and so we get an error on trying to assign the outcome to a variable.

So I assumed the outcome in the example above would be the same structure but instead of 1,3,9,.. we would have the 1 x 2 matrices.

But in this case the loop in the function and the assignment to a vector/matrix obviously changes the behavior:

The vector/matrix assignment alone seems to make the outcome a true vector (I am talking of the 4 x 1 "outer" vector)

and the loop alone in the function seems to make it possible to assign the outcome to a variable.

The behavior w/o assignment is comparable to a vectorized call now, with the assignment it seems that the range is correctly passed as argument and Mathcad does no automatic iteration in this case

So the combination of both does the trick:

I find it still strange that the results differs whether the output is assigned a variable or not.

And I wouldn't say that I understand the concept and implementation of ranges better now.

Werner

RichardJ
19-Tanzanite
(To:Werner_E)

Actually, based on the known behavior of this function the result should be expected:

The results of evaluation and assignment are not consistent, but we have been using these vector filling functions for years, and it's the way they work.

Its an undocumented (and illogical) behavior that this function relies on. So its subject to change w/o notice.

I guess that some of the old routines to convert a range to vector don't work with versions after MC11 - at least I have a vague memory of having read something here a while ago.

Anyway, the behavior is strange and I see no logic here. So it seems to me that the FillVec routine is using a similar undocumented and equally "legal" behavior  like the simple inline evaluation trick:

RichardJ
19-Tanzanite
(To:Werner_E)

Its an undocumented (and illogical) behavior that this function relies on. So its subject to change w/o notice.

Perhaps, but it's worked in every version, including Prime, since at least 11. So I doubt it will go away anytime soon.

I agree it's not logical though.

> So I doubt it will go away anytime soon.

Given the "development speed" of Prime I guess you are perfectly right 😉

LucMeekes
23-Emerald III
(To:RichardJ)

Hmmm, Mathcad 11:

Mathcad 11 reports an "Illegal context." at the assignment of V, then of course V is undefined.

Luc

RichardJ
19-Tanzanite
(To:LucMeekes)

There are many versions of this function, and I thought this one worked in MC11, but I guess not. I think V:=FillVec(0.25,0.35..1) will work.

LucMeekes
23-Emerald III
(To:RichardJ)

Here's proof:

Nice catch.

Needless to say that your sheet works OK in MC15

When Richard wrote "we have been using these vector filling functions for years" he probably did not mean THAT many years 😉

Or he had a slightly different function in mind, when he wrote "since at least 11".

As already said, I vaguely remember discussions about some of those range2vector function from MC11 not working in later version, but I was not able to find those discussions now.

Werner

RichardJ
19-Tanzanite
(To:Werner_E)

I just looked back at an old worksheet where I compared the numeric and symbolic behavior of several variations of this function for versions 11, 12, 13, and 14. Apparently none of the variations worked in version 11 for V:=function(x), where x is a range variable, even though they all work for V:=function(range value).

I think this was the discussion I had in mind. The range variable not working  as function argument, but the explicit range as argument working. I had in mind that something which used to work in MC11 does not function in later versions, but it looks like it was the other way round.

But maybe I had this discussion in mind -> Prime 3.1: Method for converting range variable to vector no longer works! where an older method does not work in Prime anymore.

RichardJ
19-Tanzanite
(To:Werner_E)

I had forgotten about that. So in MC11 something half worked, and in MC12 it was fixed. Then Prime broke the half that was originally OK and left the fix in place. Should a laugh or cry?

Top Tags