Add two constants with same units using UnitConvert?

Posted 7 months ago
1154 Views
|
10 Replies
|
5 Total Likes
|
 Although the units are the same, the scalers will not add, what do I need to do to make this work? atm = N[UnitConvert[Quantity[1, "Atmospheres"], "PSI"]] gage = 8.7 lbf/in^2 atm + gage Answer
10 Replies
Sort By:
Posted 7 months ago
 Look at QuantityUnit@atm it is ("PoundsForce")/("Inches")^2. So if you define gage=Quantity[2.4, ("PoundsForce")/("Inches")^2] everything is fine. Answer
Posted 7 months ago
 Thanks Michael, that worked atm = N[UnitConvert[Quantity[1, "Atmospheres"], "PSI"]] gage = Quantity[8.7, ("PoundsForce")/("Inches")^2] atm + gage but I am left in the same situation, if I want to multiple by area then I have to put area into "Quantity." Is there anyway to strip the code so that the result or output from "Quantity" is just the scalar and units and not the other stuff, so that this output can work more easily with other scalar and units???? Answer
Posted 7 months ago
 You get the scalar by applying QuantityMagnitude at the result, e.g.: ft = Quantity[1, "foot"] (* output: 1 ft*) QuantityMagnitude@ ft (* output: 1*) Answer
Posted 7 months ago
 Thanks again, I tried.. atm = UnitConvert[Quantity[1, "Atmospheres"], "PSI"] // N atmN = QuantityMagnitude@atm * QuantityUnit@atm gage = 8.7 lbf/in^2 atmN + gage I got units in Quantity are different than the units in QuantityUnits and I still can not add them, Is here a possible work around???? Answer
Posted 7 months ago
 Hi Raymond,Have you tried the built-in unit discovery using ctrl+=? Mathematica understands units so you can focus on the relations such as force = pressure x area. At the end, you can use UnitConvert to convert to whatever is required. A screen capture is shown below where you could use almost any oddball English unit and easily convert to SI at the end.  Answer
Posted 7 months ago
 Beautiful Tim -- didn't know about cntrl+= or understand it. thank for the demonstration, I think that is about as simple as I can get it, instead of using units like variables and confusing Mathematica in the process. I will work with what you suggested and see how well that works out. Answer
Posted 7 months ago
 In your notebook take a look at the syntax coloring in the gage = expression. Both lbf and in are blue, which means they are interpreted as undefined symbols. Try atm = UnitConvert[Quantity[1, "Atmospheres"], "lbf/in^2"] // N gage = Quantity[8.7, "lbf/in^2"] atm + gage Answer
Posted 7 months ago
 Tim, I tried using "Cntrl+=" with Bernoulli Equation using the English system,but after two hours I still can't get it to work. Mathematica will not put the gravitational constant "32.2 lbm ft / (lbf s^2)" into units and any units described by "Cntrl+=" will not compute with 'variables' of the same names zA = 0 Quantity[1, "Feet"] prA = 10 Quantity[1, (("PoundsForce")/(("Inches")^2))] velA = UnitConvert[ Quantity[7.761668264705552, ("Meters")/("Seconds")], "ft/s"] \[Gamma]wtr = 62.4 Quantity[1, (("Pounds")/(("Feet")^3))] g = UnitConvert[Quantity["StandardAccelerationOfGravity"], "Ft/s^2"] // N gc = 32.2 *(lb ft)/(lbf s^2) energTA = prA/\[Gamma]wtr + velA^2/(2 gc) + zA/gc zA = 0 Quantity[1, "Feet"] prA = 10 Quantity[1, (("PoundsForce")/(("Inches")^2))] velA = UnitConvert[ Quantity[7.761668264705552, ("Meters")/("Seconds")], "ft/s"] \[Gamma]wtr = 62.4 Quantity[1, (("Pounds")/(("Feet")^3))] g = UnitConvert[Quantity["StandardAccelerationOfGravity"], "Ft/s^2"] // N gc = 32.174 Quantity[1, "Pounds"]* Quantity[1, (("Feet")/("PoundsForce" ("Seconds")^2))] energTA = prA/\[Gamma]wtr + velA^2/(2 gc) + zA/gc My result the result should be something close to 32.7 ft lbf / lbm Answer
Posted 7 months ago
 Hi Raymond,Here a couple of comments. First, the gray coloring indicates that there is more than what is displayed. You can use InputForm to take a look like so zA = 20 Quantity[1, "Feet"] zA // InputForm (* Should return: Quantity[20,"Feet"]*) So, $zA$ is not equal to $20 ft$, but it is equal to Quantity[20,"Feet"] . So, dividing $zA$ by (20 ft) will not return 1, but the following: zA/(20 ft) // InputForm (* Quantity[1,"Feet"]/ft *) Now, if you use the quantity framework, then you should not need to concern yourself with $g_c$. That was introduced before they had figured out that force could be expressed in terms of fundamental dimensions of Mass, Length, and Time. Mathematica knows that a pound force and a pound mass are different quantities. It looks like you dropped your gravity term in your energy expression. I reproduced Bernoulli's principle in pressure form so that it will be easier to convert to a standard unit like pressure. $$\rho (\frac{{{v^2}}}{2} + gz) + p = {\text{Constant}}$$Recall, that I suggested that we save the unit conversion for the end. Also, having incompatible units is Mathematica's way of telling you that your equation is dimensionally inconsistent. I rewrote your equations like so zA = 20 Quantity[1, "Feet"] prA = 10 Quantity[1, (("PoundsForce")/(("Inches")^2))] velA = Quantity[7.761668264705552, ("Meters")/("Seconds")] \[Gamma]wtr = 62.4 Quantity[1, (("Pounds")/(("Feet")^3))] g = Quantity["StandardAccelerationOfGravity"] pressTA = \[Gamma]wtr ( velA^2/(2) + g zA) + prA UnitConvert[pressTA, "PSI"] UnitConvert[pressTA, "Pascals"] (* 23.03350000868825lbf/(in)^2*) (*158810.39217209682Pa*) `To summarize, gray text indicates the displayed is wrapped in Quantity, Mathematica eliminates the need for $g_c$, Incompatible units suggest that you check your works, and save UnitConvert until the end. Answer
Posted 7 months ago
 Tim, thank you kindly for all your work, I am slowly learning, didn't know that you could ignore the gravitational constant when using Quantity, does make life a little easier. Answer