Skip to main content
1-Visitor
July 15, 2016
Solved

a loop creating a matrix with 2 columns

  • July 15, 2016
  • 2 replies
  • 12591 views

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

Best answer by Werner_E

Here are examples of what I was talking about:

1)

2)

Regards,

Werner

2 replies

25-Diamond I
July 15, 2016

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

Werner_E25-Diamond IAnswer
25-Diamond I
July 15, 2016

Here are examples of what I was talking about:

1)

2)

Regards,

Werner

mkraczla1-VisitorAuthor
1-Visitor
July 15, 2016

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

19-Tanzanite
July 15, 2016

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.

25-Diamond I
July 15, 2016

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.

19-Tanzanite
July 15, 2016

I would expect a nested matrix.