Here’s an example of how to use the units function.

Example: Mixed Units

Question

If a conical space capsule has a diameter of 5.1 meters, a height of 129 in, and weighs 30,000 lbm, what is its density? Express your answer in kg/m^3.

Solution

This problem requires first converting the givens into a single unit system. Either will work, so lets use metric. Next, we need to calculate the volume of the capsule. Finally, we divide the mass by volume to get the density. The volume of a cone is given by:

\[V = \frac{1}{3} \pi r^2 h\]

In MATLAB, this looks like:

%% Units Example
%
% This is a word problem with a mix of metric and Imperial units.
%
% *Author:* Kip Hart
% *Last Modified:* 02 MAR 2025

%% Givens
U = units('metric');

d = 5.1*U.M;
h = 129*U.IN;
m = 30e3*U.LBM;

%% Volume and Density
r = d/2;
V = (1/3)*pi*r^2*h;
rho = m/V;

%% Answer
answer = rho/(U.KG/U.M^3);  % divide by units you want the answer in
disp(['Density: ' num2str(answer) ' kg/m^3'])

The units() function generates a structure containing unit conversion factors. After running this script, the result in the Command Window is:

Density: 609.8942 kg/m^3

This function is useful for containing unit conversion factors. In practice, you can write scripts/functions that implement equations without specifying units (e.g. $F=m a$). You apply units to the inputs, the equations create an output, then you divide by the units you want your answer to be in.