# Units of Measurement Tutorial

Use units of measurement with Symbolic Math Toolbox™. This page shows how to define units, use units in equations (including differential equations), and verify the dimensions of expressions.

### Define and Convert Units

Load units by using `symunit`.

`u = symunit;`

Specify a unit by using `u.``unit`. For example, specify a distance of `5` meters, a weight of `50` kilograms, and a speed of `10` kilometers per hour.

`d = 5*u.m`
`d = $5 \mathrm{m}\mathrm{"meter - a physical unit of length."}$`
`w = 50*u.kg`
`w = $50 \mathrm{kg}\mathrm{"kilogram - a physical unit of mass."}$`
`s = 10*u.km/u.hr`
```s =  $10 \frac{\mathrm{km}\mathrm{"kilometer - a physical unit of length."}}{\mathrm{h}\mathrm{"hour - a physical unit of time."}}$```

You can use tab expansion to find names of units. Type `u.`, press Tab, and continue typing.

Units are treated like other symbolic expressions and can be used in any standard operation or function. Units are not automatically simplified, which provides flexibility. Common alternate names for units are supported. Plurals are not supported.

Add `500` meters and `2` kilometers. The resulting distance is not automatically simplified.

`d = 500*u.m + 2*u.km`
`d = $2 \mathrm{km}\mathrm{"kilometer - a physical unit of length."}+500 \mathrm{m}\mathrm{"meter - a physical unit of length."}$`

Simplify `d` by using `simplify`. The `simplify` function chooses the unit to return.

`d = simplify(d)`
`d = $2500 \mathrm{m}\mathrm{"meter - a physical unit of length."}$`

Convert `d` to a specific unit by using `unitConvert`. Convert `d` to kilometers.

`d = unitConvert(d,u.km)`
```d =  $\frac{5}{2} \mathrm{km}\mathrm{"kilometer - a physical unit of length."}$```

For more unit conversion and unit system options, see Unit Conversions and Unit Systems.

Find the speed if the distance `d` is traveled in `50` seconds. The result has the correct units.

```t = 50*u.s; s = d/t```
```s =  $\frac{1}{20} \frac{\mathrm{km}\mathrm{"kilometer - a physical unit of length."}}{\mathrm{s}\mathrm{"second - a physical unit of time."}}$```

### Use Temperature Units in Absolute or Difference Forms

By default, temperatures are assumed to represent differences and not absolute measurements. For example, `5*u.Celsius` is assumed to represent a temperature difference of 5 degrees Celsius. This assumption allows arithmetical operations on temperature values.

To represent absolute temperatures, use kelvin, so that you do not have to distinguish an absolute temperature from a temperature difference.

Convert `23` degrees Celsius to kelvin, treating the temperature first as a temperature difference and then as an absolute temperature.

```u = symunit; T = 23*u.Celsius; diffK = unitConvert(T,u.K)```
`diffK = $23 \mathrm{K}\mathrm{"kelvin - a physical unit of temperature."}$`
`absK = unitConvert(T,u.K,'Temperature','absolute')`
```absK =  $\frac{5923}{20} \mathrm{K}\mathrm{"kelvin - a physical unit of temperature."}$```

Because the value `0` times a symbolic unit is returned as a dimensionless `0` when using `symunit`, you can use a cell array to represent `0` degrees.

For example, convert `0` degrees Celsius to degrees Fahrenheit.

```TC = {0,u.Celsius}; TF = unitConvert(TC,u.Fahrenheit,'Temperature','Absolute')```
`TF = $32 \mathrm{°F}\mathrm{"degree Fahrenheit - a physical unit of temperature."}$`

### Verify Dimensions

In longer expressions, visually checking for units is difficult. You can check the dimensions of expressions automatically by verifying the dimensions of an equation.

First, define the kinematic equation ${\mathit{v}}^{2}={\mathit{v}}_{0}^{2}+2\mathit{as}$, where $\mathit{v}$ represents velocity, $\mathit{a}$ represents acceleration, and $\mathit{s}$ represents distance. Assume $\mathit{s}$ is in kilometers and all other units are in SI base units. To demonstrate dimension checking, the units of $\mathit{a}$ are intentionally set incorrectly.

```syms v v0 a s u = symunit; eqn = (v*u.m/u.s)^2 == (v0*u.m/u.s)^2 + 2*a*u.m/u.s*s*u.km```
```eqn =  ${v}^{2} \frac{{\mathrm{m}\mathrm{"meter - a physical unit of length."}}^{2}}{{\mathrm{s}\mathrm{"second - a physical unit of time."}}^{2}}={{v}_{0}}^{2} \frac{{\mathrm{m}\mathrm{"meter - a physical unit of length."}}^{2}}{{\mathrm{s}\mathrm{"second - a physical unit of time."}}^{2}}+2 a s \frac{\mathrm{km}\mathrm{"kilometer - a physical unit of length."} \mathrm{m}\mathrm{"meter - a physical unit of length."}}{\mathrm{s}\mathrm{"second - a physical unit of time."}}$```

Observe the units that appear in `eqn` by using `findUnits`. The returned units show that both kilometers and meters are used to represent distance.

`findUnits(eqn)`
`ans = $\left(\begin{array}{ccc}\mathrm{km}\mathrm{"kilometer - a physical unit of length."}& \mathrm{m}\mathrm{"meter - a physical unit of length."}& \mathrm{s}\mathrm{"second - a physical unit of time."}\end{array}\right)$`

Check if the units have the same dimensions (such as length or time) by using `checkUnits` with the `'Compatible'` input. MATLAB® assumes symbolic variables are dimensionless. `checkUnits` returns logical `0` (`false`), meaning the units are incompatible and not of the same physical dimensions.

`checkUnits(eqn,'Compatible')`
```ans = logical 0 ```

Looking at `eqn`, the acceleration `a` has incorrect units. Correct the units and recheck for compatibility again. `eqn` now has compatible units.

```eqn = (v*u.m/u.s)^2 == (v0*u.m/u.s)^2 + 2*a*u.m/u.s^2*s*u.km; checkUnits(eqn,'Compatible')```
```ans = logical 1 ```

Now, to check that each dimension is consistently represented by the same unit, use `checkUnits` with the `'Consistent'` input. `checkUnits` returns logical `0` (`false`) because meters and kilometers are both used to represent distance in `eqn`.

`checkUnits(eqn,'Consistent')`
```ans = logical 0 ```

Convert `eqn` to SI base units to make the units consistent. Run `checkUnits` again. `eqn` has both compatible and consistent units.

`eqn = unitConvert(eqn,'SI')`
```eqn =  ${v}^{2} \frac{{\mathrm{m}\mathrm{"meter - a physical unit of length."}}^{2}}{{\mathrm{s}\mathrm{"second - a physical unit of time."}}^{2}}={{v}_{0}}^{2} \frac{{\mathrm{m}\mathrm{"meter - a physical unit of length."}}^{2}}{{\mathrm{s}\mathrm{"second - a physical unit of time."}}^{2}}+2000 a s \frac{{\mathrm{m}\mathrm{"meter - a physical unit of length."}}^{2}}{{\mathrm{s}\mathrm{"second - a physical unit of time."}}^{2}}$```
`checkUnits(eqn)`
```ans = struct with fields: Consistent: 1 Compatible: 1 ```

After you finish working with units and only need the dimensionless equation or expression, separate the units and the equation by using `separateUnits`.

`eqn = separateUnits(eqn)`
`eqn = ${v}^{2}={{v}_{0}}^{2}+2000 a s$`

To calculate numeric values from your expression, substitute for symbolic variables using `subs`, and convert to numeric values using `double` or `vpa`.

Solve `eqn` for `v`. Then find the value of `v` where ${\mathit{v}}_{0}=5$ , $\mathit{a}=2.5$ , and $\mathit{s}=10$. Convert the result to double.

```v = solve(eqn,v); v = v(2); % choose the positive solution vSol = subs(v,[v0 a s],[5 2.5 10]); vSol = double(vSol)```
```vSol = 223.6627 ```

### Use Units in Differential Equations

Use units in differential equations just as in standard equations. This section shows how to use units in differential equations by deriving the velocity relations $\mathit{v}={\mathit{v}}_{0}+\mathit{at}$ and ${\mathit{v}}^{2}={\mathit{v}}_{0}^{2}+2\mathit{as}$ starting from the definition of acceleration $\mathit{a}=\frac{\mathit{dv}}{\mathit{dt}}$.

Represent the definition of acceleration symbolically using SI units. Given that the velocity `V` has units, you must differentiate `V` with respect to the correct units as `T = t*u.s` and not just `t`.

```syms V(t) a u = symunit; T = t*u.s; % time in seconds A = a*u.m/u.s^2; % acceleration in meters per second eqn1 = A == diff(V,T)```
```eqn1(t) =  ```

Because the velocity `V` is unknown and does not have units, `eqn1` has incompatible and inconsistent units.

`checkUnits(eqn1)`
```ans = struct with fields: Consistent: 0 Compatible: 0 ```

Solve `eqn1` for `V` with the condition that the initial velocity is ${\mathit{v}}_{0}$. The result is the equation $\mathit{v}\left(\mathit{t}\right)={\mathit{v}}_{0}+\mathit{at}$.

```syms v_0 cond = V(0) == v_0*u.m/u.s; eqn2 = V == dsolve(eqn1,cond)```
```eqn2(t) =  $V\left(t\right)=\left({v}_{0} \mathrm{m}\mathrm{"meter - a physical unit of length."}+a t \mathrm{m}\mathrm{"meter - a physical unit of length."}\right) \frac{1}{\mathrm{s}\mathrm{"second - a physical unit of time."}}$```

Check that the result has the correct dimensions by substituting `rhs(eqn2)` into `eqn1` and using `checkUnits`.

`checkUnits(subs(eqn1,V,rhs(eqn2)))`
```ans = struct with fields: Consistent: 1 Compatible: 1 ```

Now, derive ${\mathit{v}}^{2}={\mathit{v}}_{0}^{2}+2\mathit{as}$. Because velocity is the rate of change of distance, substitute `V` with the derivative of distance `S`. Again, given that `S` has units, you must differentiate `S` with respect to the correct units as `T = t*u.s` and not just `t`.

```syms S(t) eqn2 = subs(eqn2,V,diff(S,T))```
```eqn2(t) =  ```

Solve `eqn2` with the condition that the initial distance covered is `0`. Get the expected form of `S` by using `expand`.

```cond2 = S(0) == 0; eqn3 = S == dsolve(eqn2,cond2); eqn3 = expand(eqn3)```
```eqn3(t) =  $S\left(t\right)=\frac{a {t}^{2}}{2} \mathrm{m}\mathrm{"meter - a physical unit of length."}+{v}_{0} t \mathrm{m}\mathrm{"meter - a physical unit of length."}$```

You can use this equation with the units in symbolic workflows. Alternatively, you can remove the units by returning the right side using `rhs`, separating units by using `separateUnits`, and using the resulting unitless expression.

`[S units] = separateUnits(rhs(eqn3))`
```S(t) =  $\frac{t \left(2 {v}_{0}+a t\right)}{2}$```
`units(t) = $\mathrm{m}\mathrm{"meter - a physical unit of length."}$`

When you need to calculate numeric values from your expression, substitute for symbolic variables using `subs`, and convert to numeric values using `double` or `vpa`.

Find the distance traveled in `8` seconds where `v_0 = 20` and `a = 1.3`. Convert the result to double.

```S = subs(S,[v_0 a],[20 1.3]); dist = S(8); dist = double(dist)```
```dist = 201.6000 ```