cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Translate the entire conversation x

Not getting the results as J <---- J+1 not doing well

RWickrama
10-Marble

Not getting the results as J <---- J+1 not doing well

I am working on a sample to develop my calculations.

I used both If .. Then and While.

But, I can get the results: J is not adding up. Looks like J keeps remaining at 1.

 

I request help.

 

ACCEPTED SOLUTION

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

Your programs don't show what you intend to do if the function argument x is not exactly in the list of nominal diameters.
The programs (with Terry's fix applied) will return just the last value of j in this case (15).
If, as I assume, that's not intended, you could simply place the return value you want to see in case of an invalid x in the last program line, like:

Werner_E_5-1760878693857.png

 

To achieve what you obviously intend to do (take the nominal diameter ND, lookup the corresponding outer diameter OD and use it to calculate the cross-sectional area) could more easily be done using the "lookup" function:

Werner_E_0-1760877012952.png

To avoid the error in case x is not in the list you could add your own return value in case lookup fails:

Werner_E_1-1760877084765.png

I think that returning the index j as well was done just for debugging. But in case you really need that index you could use the "match" function (see attached sheet).

You may also just use the next larger value in ND in case x is not found in that list

Werner_E_2-1760877281033.png

Similarly you could also decide to use the next smaller value or the nearest value (see attached sheet).

And of course a further option would be to use linear interpolation to deal with x-values not in the ND-list.

Werner_E_6-1760878723404.png

Prime 11 file attached

View solution in original post

22 REPLIES 22

Hi,

The comparison equals is a bold equals sign and is entered by the following keys together [CTRL][equal  sign].

Capture.JPG

Cheers

Terry

Werner_E
25-Diamond I
(To:RWickrama)

Your programs don't show what you intend to do if the function argument x is not exactly in the list of nominal diameters.
The programs (with Terry's fix applied) will return just the last value of j in this case (15).
If, as I assume, that's not intended, you could simply place the return value you want to see in case of an invalid x in the last program line, like:

Werner_E_5-1760878693857.png

 

To achieve what you obviously intend to do (take the nominal diameter ND, lookup the corresponding outer diameter OD and use it to calculate the cross-sectional area) could more easily be done using the "lookup" function:

Werner_E_0-1760877012952.png

To avoid the error in case x is not in the list you could add your own return value in case lookup fails:

Werner_E_1-1760877084765.png

I think that returning the index j as well was done just for debugging. But in case you really need that index you could use the "match" function (see attached sheet).

You may also just use the next larger value in ND in case x is not found in that list

Werner_E_2-1760877281033.png

Similarly you could also decide to use the next smaller value or the nearest value (see attached sheet).

And of course a further option would be to use linear interpolation to deal with x-values not in the ND-list.

Werner_E_6-1760878723404.png

Prime 11 file attached

Thanks for the great help. Help to improve Mathcad programming.

I rarely use MathCAD. But, I will try to use often.

Thank you for the great reply.

I explored all the options.

I did well. BUt I get some issues.

When my input is not a listed data, I don't get the message.

I appreciate if you can shed some light on this issue.

Please see the attched Mathcad  file.

Thanks again

 

 

See file.

With a unit applied to the expressions that return the message get the question mark.  Just delete the units on the end of the expression.

Thanks a lot Terry.

I got the idea. I can, for now, continue with my previous calculation procedure.

All the best.

Your previous reply was excellent.

As I got the preliminary understanding, I moved to solve tmy problem in hand.

 

I do not like to bug you. But, at this moment, I need help for a one more time.

I still have the problem of not incrementing.

I request you help.

Mathcad (11.0.0.0) file is attached here to.

 

THANK YOU

 

Re: 008-MCAD005 PTC2

The for loop only operates once as the if statement is true first time through.  Then it returns the 1 and QSp1.

 

Perhaps if you describe in words what you are trying to achieve rather than programming we may be able to help better.

 

Cheers

Terry

Werner_E
25-Diamond I
(To:RWickrama)

Are you aware that a 'return' statement stops the execution of a program?

 

So your program stops as soon as 'Detector' gets equal or larger than QSP.1. This is already the case right the first time and so your program already stops at i=0.

BTW, it's not good programming style to alter the loop variable i of a for-loop. You should avoid the i <- i+1 assignment.

Here you see in variable YY all the results from i=1 up to i=6. Detector is larger in all cases:

Werner_E_1-1760966715311.png

 

It may not have to do with the problem you encounter but I think you made a mistake when you defined function Pvh.

Your argument rho could be omitted as its not used in the function definition. Maybe rhow was meant to be just rho?

Werner_E_0-1760966302015.png

 

This single problem gave me a lot of skills - of course because of you.

I appreciate your good karma. What can I give you in return? I may say ALL GODS BLESS YOU ALL THE TIME FOE EVER!

Werner_E
25-Diamond I
(To:RWickrama)

As Terry already explained is the problem that Prime does not allow to add a unit to a text string.

As an alternative to the text output you may use an 'impossible' return value (such as 0 in or a negative length), or you may return a NaN (not-a-number) with appropriate unit or you may create your own error-message:

Werner_E_0-1760917279443.png

 

Hi Werner_E

I like the to do with the NaN type reply. Thanks a lot.
 
StuartBruff
23-Emerald IV
(To:Werner_E)

Well, you kind of can add units to a text string via the format function.

 

2025 10 20 D.png

 

(Mathcad Express 11, hence the convoluted expressions.  Prior workings hidden to avoid hysterical laughter at my misunderstanding of what's going on)

 

Stuart

Werner_E
25-Diamond I
(To:StuartBruff)


@StuartBruff wrote:

Well, you kind of can add units to a text string via the format function.


Hmm, I would say that's rather adding units IN a string, not TO a string.

 

I wonder if it would be possible to write a function "unit2str" ?

Werner_E_1-1760981267140.png

Of course I would strive for a method that does not consist of treating all possible units individually, like

Werner_E_2-1760981415318.png

 

 

 

StuartBruff
23-Emerald IV
(To:Werner_E)


@Werner_E wrote:

@StuartBruff wrote:

Well, you kind of can add units to a text string via the format function.


Hmm, I would say that's rather adding units IN a string, not TO a string.

 

I wonder if it would be possible to write a function "unit2str" ?

Werner_E_1-1760981267140.png

Of course I would strive for a method that does not consist of treating all possible units individually, like

Werner_E_2-1760981415318.png


 

WE: "I wonder if it would be possible to write a function "unit2str" ?"

 

No.  Not at all.  Impossible, I tell you, absolut unmöglich!!

 

2025 10 21 A.png

 

  1. I've needed to convert strings to symbols and vice versa on many occasions, but never as much as I needed it for this task!  Yes, of course it's a long-standing feature request
  2. When assigning values to multiple variables, which happen to be contained in an array, it is a real pain not being able to select them all and change their Math Styles in bulk.   Given my very low attention span, setting multiple instances of 24 variable names in a vector to Unit style is not my favourite way to pass a few idle minutes.
  3. I wish PTC would fix the Text Block so that it only covers the left-hand page, rather than extending off to infinity on the Draft pages. Perhaps there is some use case that many people have for it, but I'm afraid I can't see what it might be.   IIRC, Mathsoft Mathcad also had the ability to toggle between a Text Block and a Text Box -- very useful. 
  4. I'm bored now and in need of coffee, plus I have no work ethic, so I'll maybe complete a few more completely factored derived units and tidy up the worksheet before posting it.  The conversion is largely powered by brute force and ignorance, so expect no startling insights.

 

Stuart

 

(Mathcad Express 11 displays numbers less than 10-15 as 0.   Changing the zero threshold is a Premium feature)

Oh, all right, just because you didn't ask, and I've finished my coffee and had a play with another worksheet ....

 

unit2str Mathcad Express 11 worksheet attached.  Tidied up a bit, but not complete in terms of derived units.

 

Stuart

Werner_E
25-Diamond I
(To:StuartBruff)

Wow,  impressive! Seems you had more than just one cup of coffee.
Definitely something to steal and keep 😉

StuartBruff
23-Emerald IV
(To:Werner_E)

Thank you, but, no, just the one cup.  It required less tidying up than I thought,

 

Although I did accidentally delete some work I'd done on the reverse transformation str2unit.

 

I'm half sure I did something similar a long time ago, but I couldn't find the worksheet, so I thought it was quicker to recreate it from scratch.   I've added a few more derived units to the worksheet and created a very inelegant str2unit

 

I might brood on str2unit for a little bit longer, as there is at least one bug in it that I need to fix, and a few more derived units to add.  Or, apart from the bug fix, I might not, as I have a shedload of other worksheets that I'm working on Expressifying.

 

You might notice one significant omission from the vector of frequencies, and therein lies my bug.

 

2025 10 21 E.png

 

Stuart 

Werner_E
25-Diamond I
(To:StuartBruff)


You might notice one significant omission from the vector of frequencies, and therein lies my bug.

 


Hertz without any prefix?

StuartBruff
23-Emerald IV
(To:Werner_E)


@Werner_E wrote:

You might notice one significant omission from the vector of frequencies, and therein lies my bug.

 


Hertz without any prefix?



Yes, and that's generic across all units.  After the meeting I'm in has finished, I might get a bit of time to restructure the algorithm.

 

Stuart

After dealing with a number of non-Mathcad related tasks, I got around to sorting out the problem.   I now have a lot of tidying up to do, but won't be able to get around to for at least a couple of days due to other stuff getting in the way.

 

2025 10 22 A.png

 

OOIC, does anybody know of a way to break out of a fold?

 

Stuart

I got to work on my calculations comfortably. I thank everyone for the help.

Attached here to is one of my final calculation, but WIP: I need to add some images from a Revit model.

Announcements

Top Tags