Difference between revisions of "Units in Matlab"

From biophysics
Jump to navigation Jump to search
 
(49 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.
  
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 as possible!
+
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==
 
==SI units==
  
We have the SI-units of the Franse Système international d'unités:
+
In science we have the '''primary''' SI-units of the Franse Système international d'unités:
  
 
<pre>
 
<pre>
Line 17: Line 19:
 
</pre>
 
</pre>
  
Combinations of these units like m/s are also considered basic SI-units, and also units that can be expressed in terms of SI-units but have their own name like: Newton (kg.m/s²) or Pascal (N/).
+
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/).
  
SI-units with prefixes are '''not''' considered basic SI-units.
+
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==
 
==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".
  
The main rules are:
+
Secondary rules:  
* Variables like someLength, someTime, someVolume should always be in SI-units.
+
* 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.
* Variables in non SI-units like nanometer, millisecond or gallon should have suffixes like someLength_nm, someTime_ms or someVolume_gallon.
+
* If an output interface needs non-basic units, convert them from basic SI-units as late as possible before outputting them.
* Use only variables in basic SI-units inside a program as much as possible.
+
* If you need to deviate from the primary rules make it explicit with comments.
* Do calculations only on basic SI-units as much as possible.
+
 
* Avoid use SI-units with prefixes like nanometer or millisecond as much as possible.
+
summary:
* If an input interface has non-standard SI units, convert them to SI-units as soon as you read them from the interface.
+
<pre>
* If an output interface needs non-standard SI units, convert the from SI-units as late as possible, just before outputting them.
+
Basic SI-units: implicit (preferred)
* When you need a different unit for some reason in you program, always make it explicit by using a suffix.
+
Other units  : explicit by using suffixes
* Use radians for angles
+
</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