The PTC Community is on temporary read only status in preparation for moving our community to a new platform. Learn more here
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
Solved! Go to Solution.
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?
You may also use
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.
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
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?
You may also use
Werner -
You are correct. I corrected my silly mistakes. It now works just as you have shown.
Thanks,
thodij
@thodij try this
@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
SPaulis -
I appreciate your taking the time to offer this additional/ more general solution.
Awesome PTC community!
Regards,
thodij
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:
Of course its also possible to turn the local temporary function into a a global worksheet function and so split the function into two
Nice!
@thodij even simpler program, honing @Werner_E's simplification
Thanks for the idea @Werner_E, I never thought to do use vectorizations inside programs.
Dang it @Werner_E! Now I need to add that to all my general functions!!!
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:
You know what i'll be doing for the next several hours...
Instead of rewriting all utility functions in your toolbox you may also simply add functions with the same name which call them vectorized.
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
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...
@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.
I sure did not feel of being accused.
Wish you good luck in updating your library.
