Symbolic Calculations with Units

Mathematica's ability to deal with symbolic expressions, as well as numbers, allows you to use it for many kinds of mathematics. Mathematica's unit system utilizes this symbolic code base, facilitating calculus using Quantity expressions.

Solve[equation,vars]solution to an equation for x
Integrate[f,x]the indefinite integral
D[f,x]the (partial) derivative

Some symbolic mathematical operations that operate on Quantity expressions.

General

Many symbolic commands are capable of understanding units. Still, there are times when you might wish to add or remove units. These can be handled by Mathematica's general substitution mechanism.

This assigns units to each symbol. Notice that the input symbols are dimensionful, but the output symbols are dimensionless.
In[1]:=
Click for copyable input
Out[1]=
A quick way to test whether an equation is dimensionally consistent is to subtract both sides, substitute in quantity values, and check whether it evaluates to a valid Quantity object.
In[2]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
Out[3]=
In[4]:=
Click for copyable input
Out[4]=
In[5]:=
Click for copyable input
Out[5]=
These values are inconsistent with the equation, so messages are issued, and the computation returns only partially evaluated. As a result, the output has Head Plus.
In[6]:=
Click for copyable input
Out[6]=
In[7]:=
Click for copyable input
Out[7]=
This substitution replaces each quantity with just the numerical portion, leaving a purely symbolic expression.
In[8]:=
Click for copyable input
Out[8]=
In[9]:=
Click for copyable input
Out[9]=

Solve

Solve is aware of Quantity and will automatically determine the units of unknown variables within the equation. Units for variables can be specified by inserting the variable inside a Quantity expression with the desired units.

This finds the ratio of per .
In[1]:=
Click for copyable input
Out[1]=
This gives the two solutions to the equation TemplateBox[{2.2`*^12}, QuantityUnit, DisplayFunction -> (TooltipBox[{#1, "J"}, Unit: joules] & ), InterpretationFunction -> ({Quantity, [, {#1, ,, "Joules"}, ]} & )]=v^2 (TemplateBox[{34000.`}, QuantityUnit, DisplayFunction -> (TooltipBox[{#1, "kg"}, Unit: kilograms] & ), InterpretationFunction -> ({Quantity, [, {#1, ,, "Kilograms"}, ]} & )]). The solutions are given as replacements for .
In[2]:=
Click for copyable input
Out[2]=
Units can be specified by entering the unknown variable as a Quantity.
In[3]:=
Click for copyable input
Out[3]=
If Solve cannot determine the units of an unknown variable, because of ambiguity or a dimensionality conflict, it will return unevaluated.
In[4]:=
Click for copyable input
Out[4]=

Integrate

Integrate is aware of Quantity and will correctly combine the units of the integrand and integration variables. If the variable of integration is a Quantity object, then its QuantityMagnitude is used as the integration variable, with assumed units equal to its QuantityUnit. In the case of definite integration, units can be inferred from the endpoints, with no need to explicitly declare the units of the integration variable. In general, it is best to enter the input in one of two forms:

The integrand and all the integration variables are Quantity objects.
Quantity objects only appear in the limits of definite integration.

Input can be entered in other ways, but Integrate will fail if it cannot determine a consistent assignment of units for all expressions.

Calculate displacement over a given time.
In[1]:=
Click for copyable input
Out[1]=
In this form, the variable is explicitly declared to be measured in seconds.
In[2]:=
Click for copyable input
Out[2]=
Here, the variable is explicitly declared to be measured in minutes, and the endpoints are automatically converted to the correct units.
In[3]:=
Click for copyable input
Out[3]=
In[4]:=
Click for copyable input
Out[4]=
Calculate displacement symbolically.
In[5]:=
Click for copyable input
Out[5]=
Integrate a linear charge density function. Notice that the whole integrand is a Quantity, allowing an easy combination of the units.
In[6]:=
Click for copyable input
Out[6]=
If the integration variables are not declared as quantities, and quantity objects in the integrand are constants, Integrate will maintain the separated form of the integrand.
In[7]:=
Click for copyable input
Out[7]=
If you declare a unit for the integration variable, then the result will be returned as a Quantity object.
In[8]:=
Click for copyable input
Out[8]=
When the integrand has nonconstant Quantity objects as subexpressions, Integrate must infer units from the various expressions. If these inferences lead to contradictions, Integrate will fail.
In[9]:=
Click for copyable input
Out[9]=

D

D is aware of Quantity and will correctly combine the units of Quantity objects in the expression being differentiated with the units of the differentiation variables.

Compute the derivative of one quantity with respect to another.
In[1]:=
Click for copyable input
Out[1]=
Compute the second derivative of position, the acceleration.
In[2]:=
Click for copyable input
Out[2]=
D will automatically keep track of units.
In[3]:=
Click for copyable input
Out[3]=
Differentiate an expression containing several Quantity objects.
In[4]:=
Click for copyable input
Out[4]=
When differentiating a sum of Quantity objects, Expand can often provide simplification.
In[5]:=
Click for copyable input
Out[5]=
In[6]:=
Click for copyable input
Out[6]=
Computing a gradient with respect to Quantity variables.
In[7]:=
Click for copyable input
Out[7]=
New to Mathematica? Find your learning path »
Have a question? Ask support »