Start a topic
With the exception of Windchill, The PTC Community is on read-only status until April 6 in preparation for moving our community to a new platform. Learn more here
cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

The PTC Community is on temporary read only status in preparation for moving our community to a new platform. Learn more here

Translate the entire conversation x

Programming construct with for & if-then loop

thodij
6-Contributor

Programming construct with for & if-then loop

Hi PTC Community,

I am not sure why this programming construct will not work: apply the Nu function on a scalar & a column vector.

 

I use Mathcad Prime 11.0.1.0.

 

Thanks,

thodij

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:thodij)

And why is you function using the vector Re? It seems to be useless.

And why isn't it using its argument x??

 

So you are just looking for something like this?

grafik.png

You may also use

grafik.png

View solution in original post

14 REPLIES 14
Werner_E
25-Diamond I
(To:thodij)

Which results do you expect in case of argument 99 and argument Re.l ?

Your function does not use its argument x at all. So the result would be the same for whatever argument you provide.

The error stems from the fact that you can't compare a vector with a scalar (Re<50).

But I don't see what the goal of your function should be.

thodij
6-Contributor
(To:Werner_E)

I see that Nu(99) is not covered in the logic ranges! Need to add another else if 50 <= Re < 100 in the logic.

 

For Rel = [49 200 230 300], I would expect Nu(Rel) = [0.25 0.50 0.50 0.75]

 

Thanks,

thodij

Werner_E
25-Diamond I
(To:thodij)

And why is you function using the vector Re? It seems to be useless.

And why isn't it using its argument x??

 

So you are just looking for something like this?

grafik.png

You may also use

grafik.png

thodij
6-Contributor
(To:Werner_E)

Werner -

You are correct. I corrected my silly mistakes. It now works just as you have shown.

Thanks,

thodij

 

SPaulis
14-Alexandrite
(To:thodij)

@thodij try this

 

 

Screenshot 2026-03-04 104422.png

 

Shawn P.
“It’s OK to fall in love with your heart. But, when it comes to making engineering decisions, don’t design with your heart.” – Blodgett, Omer W.
SPaulis
14-Alexandrite
(To:SPaulis)

@Werner_E covered most of this already. 

 

I guess I only added the updated program that handles both single item and arrays automatically, without vectorization

Shawn P.
“It’s OK to fall in love with your heart. But, when it comes to making engineering decisions, don’t design with your heart.” – Blodgett, Omer W.
thodij
6-Contributor
(To:SPaulis)

SPaulis -

 

I appreciate your taking the time to offer this additional/ more general solution.

Awesome PTC community!

 

Regards,

thodij

 

Werner_E
25-Diamond I
(To:SPaulis)

Here is another version which also can be called with a single scalar argument as well as a vector argument without having to use explicit vectorization:

grafik.png

Of course its also possible to turn the local temporary function into a a global worksheet function and so split the function into two

grafik.png

SPaulis
14-Alexandrite
(To:Werner_E)

Nice!

Shawn P.
“It’s OK to fall in love with your heart. But, when it comes to making engineering decisions, don’t design with your heart.” – Blodgett, Omer W.
SPaulis
14-Alexandrite
(To:Werner_E)

@thodij  even simpler program, honing @Werner_E's simplification 

 

Screenshot 2026-03-05 075451.png

 

Thanks for the idea @Werner_E, I never thought to do use vectorizations inside programs.

Shawn P.
“It’s OK to fall in love with your heart. But, when it comes to making engineering decisions, don’t design with your heart.” – Blodgett, Omer W.
SPaulis
14-Alexandrite
(To:Werner_E)

Dang it @Werner_E!  Now I need to add that to all my general functions!!!

 

Screenshot 2026-03-05 082141.png

 

In a few cases when I know that I might send a single value or an array of values to a function, I would convert the singles into arrays, then simply for-next the calculation and convert back into a single.  Now I can simply add this to all my functions so that I coner any conceivable combination without a sweat.  It'll even handle horizontal array and matrices. 

 

And I can test for one or all of the parameters.  With my example above, maybe you want to see when the fraction equivalent of a single number but with different denominators—as if you had that much time on your hands 😜 

 

And for very basic user functions that are not specifically matrix/vector function, simply add the vectorization right away:

 

Screenshot 2026-03-05 082141.png

You know what i'll be doing for the next several hours... 

Shawn P.
“It’s OK to fall in love with your heart. But, when it comes to making engineering decisions, don’t design with your heart.” – Blodgett, Omer W.
Werner_E
25-Diamond I
(To:SPaulis)

Instead of rewriting all utility functions in your toolbox you may also simply add functions with the same name which call them vectorized.

grafik.png

The latter definition overlays the first function definition with the same name, but still uses it.

 

P.S.: I am a bit confused as of your "fraction" function

grafik.png

As I see it it would always just return a zero (or a vector of zeros) independent from the input values ('tol' is not use at all) because the return value is just the result of the last program line n <-- 0 which is zero.

But maybe you were just showing part of that function...

SPaulis
14-Alexandrite
(To:Werner_E)

@Werner_E yes, I did keep everyone in the dark.  The fraction function does exactly what you might think it does, but I screen-grabbed the start of it only.  Your imagination can fill in the rest.

 

Secondly, my 'function library' that I include in all my engineering work is a sheet of globally defined functions, so I cannot reuse the function names.  Like you alluded before, I could append an underscore to my base function.


This thread has made me ponder what would be the best way to update my library, and in a manor that is consistent.  My thinking is this:  If I have a very basic function like LEFT(), I may still use it in another function and even as a sub-function for a function,  The fact is that I do not know how my future complex programs might need to use LEFT().  Therefore I feel that I would rather push the flexibility into each of my basic functions.  This way I can use any of my functions without having to ask myself  "did I have an array version of this function?"


There are a few exceptions where the functions that do not lend themselves to having an array/matrix input.

 

If it's actually a case of reducing the time to edit all my functions, like you are implying, then my desire is to rewrite the bad code since I really enjoy programming and I like perfecting my work.  I'm a bit autistic that way;)  If you feel that I was accusing you, think again, I was only being playful and I am actually looking forward to updating my library.

Shawn P.
“It’s OK to fall in love with your heart. But, when it comes to making engineering decisions, don’t design with your heart.” – Blodgett, Omer W.
Werner_E
25-Diamond I
(To:SPaulis)

I sure did not feel of being accused.

Wish you good luck in updating your library.

Announcements


Top Tags