20
$\begingroup$

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!

$\endgroup$
2
  • 2
    $\begingroup$ 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.) $\endgroup$ Commented Nov 10, 2014 at 11:30
  • 1
    $\begingroup$ @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. $\endgroup$ Commented Sep 15, 2015 at 12:26

3 Answers 3

19
$\begingroup$

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)"]]
$\endgroup$
2
  • $\begingroup$ Thanks! I agree: it makes a lot more sense for those to be TemperatureDifferenceUnit. And your fix solves my problem! $\endgroup$
    – Sam Bader
    Commented Mar 7, 2013 at 1:29
  • $\begingroup$ @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. $\endgroup$ Commented Sep 15, 2015 at 12:34
2
$\begingroup$

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}
$\endgroup$
0
2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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