Using Units and Physical Constants in Mathematica

Mathematica has the ability to handle many calculations with units, and to perform conversions among different units.  It also has many built-in constants and other data that are useful in doing calculations.

Clear["Global`*"]

Basic Units

Imagine finding the volume of a box (2m) x (3m) x (5 cm).  (Note the mixed units.)  Enter the quantities in Mathematica using the ‘Quantity’ function.  Mathematica will automatically suggest some units once you start typing.  Note that units in Mathematica are always plural.

l = Quantity[2., "Meters"]

w = Quantity[3., "Meters"]

h =  Quantity[5., "Centimeters"]

volume = l * w * h

You can convert to other volume units with UnitConvert.

UnitConvert[volume, "Liters"]

UnitConvert[volume, "Centimeters"^3]

UnitConvert[volume, "Gallons"]

If you just want to convert to SI units, you can simply use “SI”:

UnitConvert[volume, "SI"]

Physical Constants

Mathematica has many constants built in, though sometimes you have to guess exactly how it will be expressed.  For example, Boltzmann’s constant k is:

k = Quantity[1, "BoltzmannConstant"]

Mathematica simply writes the standard letter k for this constant.  To see what it is numerically,
use UnitConvert[ ]:

UnitConvert[k, "SI"]

There are two quirks with this answer.  First, the set of units displayed is not always the most useful.  If you know the units you want, you can request them directly.  If  you simply want the traditional SI base units (kg, m, s, A, K), use “SIBase”.

UnitConvert[k, "Joules"/"Kelvins"]
UnitConvert[k, "SIBase"]

The second quirk is that for constants defined exactly in SI (such as the speed of light or Boltzmann’s constant) Mathematica returns an exact integer result.  You usually want to convert that to a numerical approximation with the N[ ] function.

UnitConvert[N[k], "Joules"/"Kelvins"]

Physical and Chemical Data

Mathematica has access to a wealth of data.  Some is built in, but most requires an on-line connection to fetch the data from Wolfram’s servers.

For example, data about the elements is available through the ElementData function.  Here is how to get the mass of a helium atom.  The result is returned in terms of the atomic mass unit, but you can convert it to other forms:

m = ElementData["Helium", "AtomicMass"]

UnitConvert[m, "Kilograms"]

UnitConvert[m, "Megaelectronvolts"/"SpeedOfLightSquared"]

You can get a list of all available properties by using the “Properties” key:

ElementData["Helium", "Properties"]

Data about chemical compounds is available through the ChemicalData[ ] function.  For example, the mass of the water molecule is (where ‘u’ is the Atomic Mass Constant)

mH2O = ChemicalData["Water", "MolecularMass"]

Quantity[1, "AtomicMassConstant"]

UnitConvert[%, "Kilograms"]

Calculations

Mathematica can do calculations with units and will perform appropriate operations on units.  It will refuse to do calculations where the units do not agree.

Room Temperature

k = Quantity[1, "BoltzmannConstant"]

T = Quantity[300., "Kelvins"]

UnitConvert[k T, "Joules"]

UnitConvert[k T, "Electronvolts"]

k + T

For a longer example, consider problem 2.35.  The final step is calculating a temperature from a complex combination of constants.  In this problem, we are considering Helium initially at “room temperature” and “atmospheric pressure”.

Clear[Tz, h, k, m, Ti, pi]

Tz = ((h^2)/(2 (E^(5/3)) k m π (((((k Ti)/pi)))^(2/3))))

Ti = Quantity[300, "Kelvins"]

pi = Quantity[1, "Atmospheres"]

k = Quantity[1, "BoltzmannConstant"]

h = Quantity[1, "PlanckConstant"]

m = ElementData["Helium", "AtomicMass"]

Tz

UnitConvert[Tz, "Kelvins"]