Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Check for compatible dimensions and consistent units

`C = checkUnits(expr)`

`C = checkUnits(expr,'Compatible')`

`C = checkUnits(expr,'Consistent')`

`C = checkUnits(`

checks `expr`

)`expr`

for
compatible dimensions and consistent units and returns a structure
containing the fields `Consistent`

and `Compatible`

.
The fields contain logical `0`

(`false`

)
or logical `1`

(`true`

) depending
on the check results.

`expr`

has compatible dimensions if all terms
have the same dimensions, such as length or time. `expr`

has
consistent units if all units of the same dimension can be converted
to each other with a conversion factor of 1.

Check the dimensions of an equation or expression. The dimensions are checked to confirm that the equation or expression is valid.

Verify the dimensions of the equation

$$A\text{}\frac{\text{m}}{\text{s}}=B\text{}\frac{\text{kg}}{\text{s}}$$

by using `checkUnits`

with the option `'Compatible'`

. MATLAB^{®} assumes
that symbolic variables are dimensionless. The `checkUnits`

function
returns logical `0`

(`false`

) because
the dimensions of the equation are not compatible.

u = symunit; syms A B eqn = A*u.m/u.s == B*u.kg/u.s; checkUnits(eqn,'Compatible')

ans = logical 0

Replace `u.kg`

with `u.m`

by
using `subs`

and repeat the check. Because the
dimensions are now compatible, `checkUnits`

returns
logical `1`

(`true`

).

eqn = subs(eqn,u.kg,u.m); checkUnits(eqn,'Compatible')

ans = logical 1

Checking units for consistency is a stronger
check than compatibility. Units are consistent when all units of the
same dimension can be converted to each other with a conversion factor
of 1. For example, 1 Newton is consistent with 1 kg m/s^{2} but
not with 1 kg cm/s^{2}.

Show that `1`

Newton is consistent with `1`

kg
m/s^{2} by checking `expr1`

but
not with `1`

kg cm/s^{2} by
checking `expr2`

.

u = symunit; expr1 = 1*u.N + 1*u.kg*u.m/u.s^2; expr2 = 1*u.N + 1*u.kg*u.cm/u.s^2; checkUnits(expr1,'Consistent')

ans = logical 1

checkUnits(expr2,'Consistent')

ans = logical 0

Show the difference between compatibility and consistency by
showing that `expr2`

has compatible dimensions but
not consistent units.

checkUnits(expr2,'Compatible')

ans = logical 1

Check multiple equations or expressions by
placing them in an array. `checkUnits`

returns
an array whose elements correspond to the elements of the input.

Check multiple equations for compatible dimensions. `checkUnits`

returns ```
[1
0]
```

, meaning that the first equation has compatible dimensions
while the second equation does not.

u = symunit; syms x y z eqn1 = x*u.m == y*u.m^2/(z*u.m); eqn2 = x*u.m + y*u.s == z*u.m; eqns = [eqn1 eqn2]; compatible = checkUnits(eqns,'Compatible')

compatible = 1×2 logical array 1 0

Check for both compatible dimensions and consistent
units of the equation or expression by using `checkUnits`

.

Define the equations for x- and y-displacement of a moving projectile. Check their units for compatibility and consistency.

u = symunit; g = 9.81*u.cm/u.s^2; v = 10*u.m/u.s^2; syms theta x(t) y(t) x(t) = v*cos(theta)*t; y(t) = v*sin(theta)*t + (-g*t^2)/2; S = checkUnits([x y])

S = struct with fields: Consistent: [1 0] Compatible: [1 1]

The second equation has compatible dimensions but inconsistent
units. This inconsistency is because `g`

incorrectly
uses cm instead of m. Redefine `g`

and check the
equations again. The second equation now has consistent units.

g = 9.81*u.m/u.s^2; y(t) = v*sin(theta)*t + (-g*t^2)/2; S = checkUnits([x y])

S = struct with fields: Consistent: [1 1] Compatible: [1 1]

`findUnits`

| `isUnit`

| `newUnit`

| `separateUnits`

| `str2symunit`

| `symunit`

| `symunit2str`

| `unitConversionFactor`