Skip to main content
10-Marble
December 8, 2023
Solved

For Loop - "Units are not compatible" Error

  • December 8, 2023
  • 1 reply
  • 3758 views

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

Best answer by Werner_E

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

 

 

1 reply

23-Emerald IV
December 8, 2023

Please attach the Prime worksheet, not just a picture.

 

Success!
Luc

justhumm10-MarbleAuthor
10-Marble
December 8, 2023

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!

Werner_E25-Diamond IAnswer
25-Diamond I
December 8, 2023

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