Get Help

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Community
- :
- PTC Mathcad
- :
- PTC Mathcad
- :
- a loop creating a matrix with 2 columns

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-15-2016
06:31 AM

07-15-2016
06:31 AM

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.

Regards,

MK

Solved! Go to Solution.

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-15-2016
09:20 AM

24 REPLIES 24

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-15-2016
08:42 AM

07-15-2016
08:42 AM

Re: a loop creating a matrix with 2 columns

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-15-2016
09:19 AM

07-15-2016
09:19 AM

Re: a loop creating a matrix with 2 columns

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-15-2016
09:20 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-15-2016
09:22 AM

07-15-2016
09:22 AM

Re: a loop creating a matrix with 2 columns

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-15-2016
09:39 AM

07-15-2016
09:39 AM

Re: a loop creating a matrix with 2 columns

I would expect a nested matrix.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-15-2016
10:30 AM

07-15-2016
10:30 AM

Re: a loop creating a matrix with 2 columns

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-15-2016
10:45 AM

07-15-2016
10:45 AM

Re: a loop creating a matrix with 2 columns

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-15-2016
10:54 AM

07-15-2016
10:54 AM

Re: a loop creating a matrix with 2 columns

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:

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-15-2016
10:58 AM

07-15-2016
10:58 AM

Re: a loop creating a matrix with 2 columns

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.

Regards,

MK

Top Tags