Difference between revisions of "Units in Matlab"
(Created page with "==Introduction== There are several ways to use variables with units in a program. Space probes have crashed on Mars due to invalid assumptions about units for some variables....") |
|||
(52 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | __TOC__ | ||
==Introduction== | ==Introduction== | ||
+ | When working with quantities we often have to know what the units are in which the quantities are expressed. There are several ways to use variables with units in a program. | ||
− | + | Space probes have crashed on Mars due to invalid assumptions about units for some variables. So how can we avoid this type of disasters? We have to make it as simple and explicit as possible. But most importantly we have to have rules and stick to it! | |
− | + | ==SI units== | |
+ | |||
+ | In science we have the '''primary''' SI-units of the Franse Système international d'unités: | ||
<pre> | <pre> | ||
Line 14: | Line 18: | ||
Candela (cd) - Unit of luminous intensity | Candela (cd) - Unit of luminous intensity | ||
</pre> | </pre> | ||
− | |||
+ | Combinations of these units are considered '''derived''' SI-units. These are units like m/s and units that can be expressed in terms of SI-units but have their own name, like: Newton (kg.m/s²) or Pascal (N/m²). | ||
+ | |||
+ | SI-units with prefixes like ''millisecond'' or ''centimeter'' are '''not''' considered primary or derived SI-units. | ||
+ | |||
+ | ''Primary'' and ''derived'' SI-units will be here defined as '''basic SI-units'''. Units with prefixes do not fall under this definition. | ||
+ | |||
+ | Some common derived SI-units are: | ||
+ | <pre> | ||
+ | Joule (energy) | ||
+ | Coulomb (electric charge) | ||
+ | Voltage (electric potential) | ||
+ | Ohm (electric resistance) | ||
+ | Farad (electric capacitance) | ||
+ | Tesla (magnetic field strength) | ||
+ | Newton (force) | ||
+ | Watt (power) | ||
+ | Pascal (pressure) | ||
+ | </pre> | ||
+ | |||
+ | By using basic SI-units formulae can be expressed in their simplest form. | ||
+ | Examples: | ||
+ | <pre> | ||
+ | pressure = force / area | ||
+ | power = potential * current | ||
+ | </pre> | ||
+ | |||
+ | ==Programming rules== | ||
+ | Primary rules: | ||
+ | * Use basic SI-units as much as possible. | ||
+ | * Use of basic SI-units is implicit: variables like ''someLength, someTime, someVolume'' are assumed in SI-units. | ||
+ | * Variables in non-basic SI-units like ''nanometer, millisecond'' or ''liter'' should have explicit '''suffixes''' like ''someLength_nm, someTime_ms or someVolume_liter''. | ||
+ | * Use of radians for angles is implicit. When using degrees use the suffix "_deg". | ||
− | + | Secondary rules: | |
− | * | + | * If an input interface has non-basic units, convert them to basic SI-units as soon as possible after you read them from the interface. |
− | * | + | * If an output interface needs non-basic units, convert them from basic SI-units as late as possible before outputting them. |
+ | * If you need to deviate from the primary rules make it explicit with comments. | ||
− | + | summary: | |
− | + | <pre> | |
+ | Basic SI-units: implicit (preferred) | ||
+ | Other units : explicit by using suffixes | ||
+ | </pre> |
Latest revision as of 10:31, 19 March 2024
Introduction
When working with quantities we often have to know what the units are in which the quantities are expressed. There are several ways to use variables with units in a program.
Space probes have crashed on Mars due to invalid assumptions about units for some variables. So how can we avoid this type of disasters? We have to make it as simple and explicit as possible. But most importantly we have to have rules and stick to it!
SI units
In science we have the primary SI-units of the Franse Système international d'unités:
Meter (m) - Unit of length Kilogram (kg) - Unit of mass Second (s) - Unit of time Ampere (A) - Unit of electric current Kelvin (K) - Unit of temperature Mole (mol) - Unit of amount of substance Candela (cd) - Unit of luminous intensity
Combinations of these units are considered derived SI-units. These are units like m/s and units that can be expressed in terms of SI-units but have their own name, like: Newton (kg.m/s²) or Pascal (N/m²).
SI-units with prefixes like millisecond or centimeter are not considered primary or derived SI-units.
Primary and derived SI-units will be here defined as basic SI-units. Units with prefixes do not fall under this definition.
Some common derived SI-units are:
Joule (energy) Coulomb (electric charge) Voltage (electric potential) Ohm (electric resistance) Farad (electric capacitance) Tesla (magnetic field strength) Newton (force) Watt (power) Pascal (pressure)
By using basic SI-units formulae can be expressed in their simplest form. Examples:
pressure = force / area power = potential * current
Programming rules
Primary rules:
- Use basic SI-units as much as possible.
- Use of basic SI-units is implicit: variables like someLength, someTime, someVolume are assumed in SI-units.
- Variables in non-basic SI-units like nanometer, millisecond or liter should have explicit suffixes like someLength_nm, someTime_ms or someVolume_liter.
- Use of radians for angles is implicit. When using degrees use the suffix "_deg".
Secondary rules:
- If an input interface has non-basic units, convert them to basic SI-units as soon as possible after you read them from the interface.
- If an output interface needs non-basic units, convert them from basic SI-units as late as possible before outputting them.
- If you need to deviate from the primary rules make it explicit with comments.
summary:
Basic SI-units: implicit (preferred) Other units : explicit by using suffixes