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.
Solved! Go to Solution.
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.
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.
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.
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:
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.