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

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

For Loop - "Units are not compatible" Error

justhumm
5-Regular Member

For Loop - "Units are not compatible" Error

Stupid question...I'm just getting back into MathCAD (Prime 7.0) after several years absence...


I'm trying to see if create a simple routine that will automatically strip the units from a variable and display only the numeric portion...so I can turn it into a string and use it in text functions. Essentially what I have right now...


The routine divides out the SIUnitsOf(variable) and multiplies that by a conversion factor that it sequentially looks up in a matrix.


And it divides the variable by the conversion factor's base unit (from the same matrix).


When I type out the procedure for an individual case, it gives the expected answer.


When I try to put that procedure into a "for" loop, it gives me an unexpected "These units are not compatible" error.


...can anyone see what am I doing wrong?

AKHscrap_1.jpg

1 ACCEPTED SOLUTION

Accepted Solutions

1) better don't use manually typed in constants but rather expressions like m/ft

Werner_E_0-1702075931688.png

 

2) The error stems from the "Round" functions. Both arguments of this functions must be of the same dimension. When in your loop i=2, then the first argument c/Cfactor in the second Round command has the unit s^2/lb, but the second argument TOL is dimensionless. Hence the error

 

3) Even without that error your function does not work as you may expect

Werner_E_1-1702076468079.png

This means that the condition j=k never is true!

The reason seem to be round-off error which occur by the internal unit conversion:

Werner_E_0-1702119879937.png

Without the numerical round off errors (probably by choosing a lower value for TOL) the condition j=k would be true in any case (as long as the dimension is length). So you could not find out if the variable initially was defined in inches or feet - the first column in your table (feet) what always 'win'.

 

 

Generally said - once you assign a variable a value with a unit, there is no way that you can later determine which unit the variable was defined in as its stored already in base unit.

So when you want to strip the unit you have to tell which unit.

Easiest way is to simply divide the quantity by the desired unit, but of course you can make a function out of it:

Werner_E_2-1702076780576.png

 

 

View solution in original post

5 REPLIES 5
LucMeekes
23-Emerald III
(To:justhumm)

Please attach the Prime worksheet, not just a picture.

 

Success!
Luc

justhumm
5-Regular Member
(To:LucMeekes)

MathCAD prime 7.0 worksheet attached.

The end result of the procedure isn't exactly what I want, but I'd like to nail down the programming error, for now.

Thanks!

1) better don't use manually typed in constants but rather expressions like m/ft

Werner_E_0-1702075931688.png

 

2) The error stems from the "Round" functions. Both arguments of this functions must be of the same dimension. When in your loop i=2, then the first argument c/Cfactor in the second Round command has the unit s^2/lb, but the second argument TOL is dimensionless. Hence the error

 

3) Even without that error your function does not work as you may expect

Werner_E_1-1702076468079.png

This means that the condition j=k never is true!

The reason seem to be round-off error which occur by the internal unit conversion:

Werner_E_0-1702119879937.png

Without the numerical round off errors (probably by choosing a lower value for TOL) the condition j=k would be true in any case (as long as the dimension is length). So you could not find out if the variable initially was defined in inches or feet - the first column in your table (feet) what always 'win'.

 

 

Generally said - once you assign a variable a value with a unit, there is no way that you can later determine which unit the variable was defined in as its stored already in base unit.

So when you want to strip the unit you have to tell which unit.

Easiest way is to simply divide the quantity by the desired unit, but of course you can make a function out of it:

Werner_E_2-1702076780576.png

 

 

justhumm
5-Regular Member
(To:Werner_E)

Thanks for the feedback @Werner_E . I missed that the Round function arguments had to be the same.

 

...I was assuming the second argument was strictly numerical, regardless of the first arguments units...ooops!

 

That being said, I had been finding what you pointed out...that (in my example) the length units would always defer to the first column of the table. Maybe you could point me to a good source of information.

 

When a variable is defined ( x := 2.5 m for instance) I assume there's some marker that tells MathCAD that variable "x" is defined with units of length and to use the native / base SI units for display.

 

I am wondering if there is a way (perhaps via Python scripting or C# programming) of accessing those properties of the variable.

 

Thoughts?

Yeah the round function will only accept dimensionless arguments and the arguments of the Round function must have compatible units. The help is clear with this (which not always is the case) but I found it out by trial and error, not by reading the help 😉

 

I can't think of an  easy and practical way to find out which unit a variable originally way defined in. Prime does not store that variable but converts the value to base units when you define a variable. So you somehow would have to trace back the sheet until you arrive at the first definition (last definition from top down) of that variable. Similar to what the symbolic engine does. But the symbolics does not know anything about units and treats them as unknowns variables but I don't think you can use it to extract the unit used at the time of definition.

Furthermore Prime still is not scriptable so you can't write a script to be initiated from within a live worksheet.

Of course you could write a script which examines a saved worksheet. Its just a zipped file with some xml files. Its not publicly documented anywhere as far as I am aware but you may find out yourself how to read it and how to insert a region with an expression to it. So your script might be able to look up the definition of variables and the units used there and insert regions with that units which then could be used for stripping.
Quite a lot of work for a simple task ...

Top Tags