Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Bug introduced in 9 and partially persists through 10.2 or later

The specific bug described in the original question is fixed ("Kelvin" is correctly recognized as "Kelvins" now) but as shown in the answer by Willinski there is more involved problem with misinterpretation of "K" as "KelvinsDifference" in some cases. Please see this discussion for more information on the temperature difference units in Mathematica.


I'm trying to use Mathematica units more often, but I've run into the following problem:

    (*Define some unitful constants*)
    q = UnitConvert[Quantity["elementary charge"]];
    k = UnitConvert[Quantity["Boltzmann Constant"]];
    T = Quantity[300, "Kelvin"];
    V = Quantity[5, "Volt"];
    (*Do a simple calculation*)
    UnitConvert[(q V)/(k T)]

The above code outputs

    Quantity[193.409, ("Kelvins")/("KelvinsDifference")]

which really should just be 193.409, since the units cancel, but Mathematica insists on a distinction between absolute temperature and temperature differences. I understand that such a distinction is useful in conversions, but the above is quite a nuisance.

Is there a way to prevent this? I'd rather not have to put in QuantityMagnitude calls every time I need to cancel some Kelvins.

Thanks!

share|improve this question
2  
I think this behaviour has changed: Kelvin used to be interpreted by Wolfram Alpha as KelvinDifference, whereas Kelvins was recognized by Mathematica as a built-in unit and directly interpreted as Kelvin. Since a few months, Kelvin is interpreted as Kelvins, so the error appears to have gone away. (Of course, the unit of the Boltzmann constant is stilll incorrect, see Xerxes' answer.) – Martin J.H. Nov 10 '14 at 11:30
1  
@Martin Although now Wolfram|Alpha correctly interprets "Kelvin" as "Kelvins", it still incorrectly interprets "K" as "KelvinsDifference" in Quantity[1, "J/(mol*K)"] while in Quantity[300, "K"] it correctly interprets "K" as "Kelvins" (the example is from the answer by Willinski). So the bug is only partially fixed. – Alexey Popkov Sep 15 '15 at 12:26
up vote 18 down vote accepted

This appears to be a bug. The dimensions of the Boltzmann constant are incorrect. In fact, all the physical constants I checked have TemperatureUnit where they should have TemperatureDifferenceUnit. You should only have to make a substitution when making calls to physical constants in Quantity:

q = UnitConvert[Quantity["elementary charge"]];
k = (UnitConvert[Quantity["Boltzmann Constant"]] /. 
   "Kelvins" -> "KelvinsDifference");
T = Quantity[300, "Kelvin"];
V = Quantity[5, "Volt"];
UnitConvert[(q V)/(k T)]
(* 193.409 *)

EDIT: Alternatively, since Quantity seems to be calling away to Alpha, you could do

k = UnitConvert[Quantity["Boltzmann Constant (energy per temperature difference)"]]
share|improve this answer
    
Thanks! I agree: it makes a lot more sense for those to be TemperatureDifferenceUnit. And your fix solves my problem! – Sam Bader Mar 7 '13 at 1:29
    
@Xerxes It seems you misinterpreted the purpose of "KelvinsDifference", please see this discussion. The specific bug described in the question is fixed now ("Kelvin" is correctly recognized as "Kelvins" now) but as shown in the answer by Willinski there is more involved problem with misinterpretation of "K" as "KelvinsDifference" in some cases. – Alexey Popkov Sep 15 '15 at 12:34

There are other mantraps!

f = (p + a/V^2) (V - b) == R T;

params1 = {
   b -> Quantity[0.0364, "L/mol"],
   a -> Quantity[135.8*10^3, "Pa*L^2/mol^2"],
   p -> Quantity[100, "kPa"],
   R -> Quantity[8.314472, "J/(mol*K)"],(*!!!*)
   T -> Quantity[300, "K"]
   };

First@NSolve[f /. params1, V ]

NSolve::units: NSolve was unable to determine the units of quantities that appear in the input. enter image description here

params2 = {
   b -> Quantity[0.0364, "L/mol"],
   a -> Quantity[135.8*10^3, "Pa*L^2/mol^2"],
   p -> Quantity[100, "kPa"],
   R -> Quantity[8.314472, "Joules"/ ("Moles"*"Kelvins")],(*!!!*)
   T -> Quantity[300, "K"]
   };
First@NSolve[f /. params2, V ]
{0.0249254 m^3/mol}
share|improve this answer

The general method which allows to avoid such problems is to specify the units in the canonical form and do not rely on the ability of Wolfram|Alpha to interpret your input correctly.

The original problem arose due to misinterpretation of "Kelvin" as "KelvinsDifference" by Wolfram|Alpha (to which Mathematica communicates when you provide an unknown string as a name for a unit). The problem would not appear if instead of incorrect name "Kelvin" one would supply the correct name for the absolute temperature units "Kelvins".

The original bug in Wolfram|Alpha is already fixed ("Kelvin" is correctly recognized as "Kelvins" now) but as shown in the answer by Willinski there still persists more involved problem with misinterpretation of "K" as "KelvinsDifference" in some cases.

For an explanation of the role of "KelvinsDifference" in the Wolfram Language please see this discussion.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.