# In Matlab, how can I be consistent with units?

I am modeling some aerodynamics equations and am using meters / centimeters, kilograms, and seconds.

I've heard that, "matlab doesn't know units". So, how can I make sure that it does? Just simply by being consistent in all of my code?

E.g., if I run an ode solver in matlab for a time scale of [0 30], can I think of it as matlab giving me ode solutions for "30 seconds"?

Thanks,

• Another suggestion is to drop MATLAB. Julia (through Unitful.jl), Maple, and Mathematica have good support for units, so if you really want to make sure units are correct and dimensional analysis is always correct you should use something like that. Oct 25 '17 at 15:01
• Another suggestion is just to drop MATLAB. Oct 25 '17 at 19:16

I would say that you have, mainly, two methods:

1. Being consistent in all your code, as already suggested in another answer. For that purpose, I always keep a table like this one with me, since it might prove really useful. 1. Use nondimensional equations. That way, all my parameters and variables are consistent already. For that purpose, I suggest reference 1.

References

1. Langtangen, Hans Petter, and Geir K. Pedersen. Scaling of Differential Equations. Springer, 2016.
• My personal preference is to non-dimensionalize everything. This has two advantages, 1. the number of parameters to keep track of is reduced to a small set of characteristic values for these equation and 2. these characteric values give you additional info on the relative importance of certain terms in your equation. This may ultimately let you simplify your equation. Oct 26 '17 at 13:04

Just simply by being consistent in all of my code?

Yes this is the only way. Matlab or any other programming language does not know about units. They only know about numbers.

As an example consider incompressible flow. If you set your velocity in m/sec, length in meters (how you generate the grid), pressure in Newton/m^2, kinematic viscosity in m^2/sec, then the time would be in seconds.

You can build code which will respect units. E.g., openfoam does this. You must define your own data types and operations on them. Each data type should have an associated unit. The program should warn you if you try to add/subtract two variables of different units. Even then, it is your responsibility to set these variables with correct numerical values. Suppose v should be in m/sec and you have to set it to 1 m/sec. If you say 1 m/sec = 100 cm/sec and you set v = 100, then that would be wrong, and no program can detect such that mistake.

• It's not fair to say that no programming language understands units. It's just that Matlab's type system is incredibly under-utilized (and not very powerful). Since a "unit" is simply a set of semantics attached to a value, this sounds like a data type to me! Good libraries (eg Boost.Units boost.org/doc/libs/1_65_0/doc/html/boost_units.html) enforce strong compile-time consistency of arithmetic operations on quantities with units and provide automatic conversion from one set of units to another. Oct 25 '17 at 4:42
• Python has very nice packages for units. So that's not correct (x2) they actually have even conversion options on the fly. Oct 25 '17 at 18:40

You can work with classes. There's a book called "What every engineer should know about MATLAB and Simulink" by Biran, and the problem you're describing is the example that is given in the OOP chapter.

In short, he defines a class of "physicalProperty" whose objects have properties which are the power of the basic units for length, mass and time. Whenever a value is created, you must specify the power of each basic unit (e.g. if you want to specify a length, the power of meters will be 1 and the powers of second and kg will be 0). Then, mathematical operations are defined such that multiplication/division is always allowed, resulting in the summation/subtraction of the different powers belonging to both operands, and addition/subtraction is only allowed between objects having the same powers for all units (i.e. if you try to subtract 5m from 1s you'll get an error).

The implementation should not be difficult if the idea is clear. Good luck! l

• That is quite a good idea actually. Thanks for you answer.
– Our
Nov 23 '17 at 5:05

In research and teaching, mostly use dimensionless equations. A neat way in aerodynamics is to put the free stream values as $p=1, U=M, \rho=\gamma$ (so that $a=1$) But if you are designing something to be made, use units and take care to get them right\$