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.
Load units by usingsymunit
.
u = symunit;
Specify a unit by usingu.
unit
. For example, specify a distance of5
meters, a weight of50
kilograms, and a speed of10
kilometers per hour. In displayed output, units are placed in square brackets[]
.
d = 5*u.m w = 50*u.kg s = 10*u.km/u.hr
d = 5*[m] w = 50*[kg] s = 10*([km]/[h])
Use tab expansion to find names of units. Typeu.
, pressTab, 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.
Add500
meters and2
kilometers. The resulting distance is not automatically simplified.
d = 500*u.m + 2*u.km
d = 2*[km] + 500*[m]
Simplifyd
by usingsimplify
. Thesimplify
function automatically chooses the unit to simplify to.
d = simplify(d)
d = (5/2)*[km]
Instead of automatically choosing a unit, convertd
to a specific unit by usingunitConvert
. Convertd
to meters.
d = unitConvert(d,u.m)
d = 2500*[m]
There are more unit conversion and unit system options. SeeUnit Conversions and Unit Systems.
Find the speed if the distanced
is crossed in50
seconds. The result has the correct units.
t = 50*u.s; s = d/t
s = 50*([m]/[s])
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.
Convert23
degrees Celsius to kelvin, treating it first as a temperature difference and then as an absolute temperature.
u = symunit; T = 23*u.Celsius; diffK = unitConvert(T,u.K)
diffK = 23*[K]
absK = unitConvert(T,u.K,'Temperature','absolute')
absK = (5923/20)*[K]
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
, wherev
represents velocity,a
represents acceleration, ands
represents distance. Assumes
is in kilometers and all other units are in SI base units. To demonstrate dimension checking, the units ofa
are intentionally incorrect.
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*([m]^2/[s]^2) == v0^2*([m]^2/[s]^2) + (2*a*s)*(([km]*[m])/[s])
Observe the units that appear ineqn
by usingfindUnits
. The returned units show that both kilometers and meters are used to represent distance.
findUnits(eqn)
ans = [ [km], [m], [s]]
Check if the units have the same dimensions (such as length or time) by usingcheckUnits
with the'Compatible'
input. MATLAB®assumes symbolic variables are dimensionless.checkUnits
returns logical0
(false
), meaning the units are incompatible and not of the same physical dimensions.
checkUnits(eqn,'Compatible')
ans = logical 0
Looking ateqn
, the accelerationa
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, usecheckUnits
with the'Consistent'
input.checkUnits
returns logical0
(false
) because meters and kilometers are both used to represent distance ineqn
.
checkUnits(eqn,'Consistent')
ans = logical 0
Converteqn
to SI base units to make the units consistent. RuncheckUnits
again.eqn
has both compatible and consistent units.
eqn =unitConvert(eqn,'SI')
eqn =v^2*([m]^2/[s]^2) == v0^2*([m]^2/[s]^2) + (2000*a*s)*([m]^2/[s]^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 usingseparateUnits
.
[eqn,units] = separateUnits(eqn)
eqn =v^2 == v0^2 + 2000*a*s units = 1*([m]^2/[s]^2)
You can return the original equation with units by multiplyingeqn
withunits
and expanding the result.
expand(eqn*units)
ans = v^2*([m]^2/[s]^2) == v0^2*([m]^2/[s]^2) + (2000*a*s)*([m]^2/[s]^2)
To calculate numeric values from your expression, substitute for symbolic variables usingsubs
, and convert to numeric values usingdouble
orvpa
.
Solveeqn
forv
. Then find the value ofv
wherev0 = 5
,a = 2.5
, ands = 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 just as in standard equations. This section shows how to use units in differential equations by deriving the velocity relationsv=v0+atand starting from the definition of acceleration .
代表symbolica加速度的定义lly using SI units. Given that the velocityV
has units,V
must be differentiated with respect to the correct units asT = t*u.s
and not justt
.
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) = a*([m]/[s]^2) == diff(V(t), t)*(1/[s])
Because the velocityV
is unknown and does not have units,eqn1
has incompatible and inconsistent units.
checkUnits(eqn1)
ans = struct with fields: Consistent: 0 Compatible: 0
Solveeqn1
forV
with the condition that the initial velocity isv0. The result is the equationv(t)=v0+at.
syms v0 cond = V(0) == v0*u.m/u.s; eqn2 = V == dsolve(eqn1,cond)
eqn2(t) = V(t) == v0*([m]/[s]) + a*t*([m]/[s])
Check that the result has the correct dimensions by substitutingrhs(eqn2)
intoeqn1
and usingcheckUnits
.
checkUnits(subs(eqn1,V,rhs(eqn2)))
ans = struct with fields: Consistent: 1 Compatible: 1
Now, derive
. Because velocity is the rate of change of distance, substituteV
with the derivative of distanceS
. Again, given thatS
has units,S
must be differentiated with respect to the correct units asT = t*u.s
and not justt
.
syms S(t) eqn2 = subs(eqn2,V,diff(S,T))
eqn2(t) = diff(S(t), t)*(1/[s]) == v0*([m]/[s]) + a*t*([m]/[s])
Solveeqn2
with the condition that the initial distance covered is0
. Get the expected form ofS
by usingexpand
.
cond2 = S(0) == 0; eqn3 = S == dsolve(eqn2,cond2); eqn3 = expand(eqn3)
eqn3 (t) = S (t) = = t * v0 * [m] + ((* t ^ 2) / 2) * [m]
You can use this equation with the units in symbolic workflows. Alternatively, you can remove the units by returning the right side usingrhs
, separating units by usingseparateUnits
, and using the resulting unitless expression.
[S units] = separateUnits(rhs(eqn3))
S(t) = (a*t^2)/2 + v0*t units(t) = [m]
When you need to calculate numeric values from your expression, substitute for symbolic variables usingsubs
, and convert to numeric values usingdouble
orvpa
.
Find the distance traveled in8
seconds wherev0 = 20
anda = 1.3
. Convert the result to double.
S = subs(S,[v0 a],[20 1.3]); dist = S(8); dist = double(dist)
dist = 201.6000
checkUnits
|findUnits
|isUnit
|newUnit
|separateUnits
|symunit2str
|unitConversionFactor
|unitConvert