The ErrorNum class (basics)

A Japanese version is here.

Please follow this instruction to get my software on your computer.

Proper treatment of the errors is essential in data analysis. Sometimes it is troublesome to deal with error for every value you compute in your code. This Matlab class helps your data and error analysis a little easy. Take a look at the following example. (Empty lines are omitted.)

% you know two values with an error
>> en1 = ErrorNum(5, 0.3)
en1 = 
ErrorNum:
5.0 ± 0.3
>> en2 = ErrorNum(2, 0.2)
en2 = 
ErrorNum:
2.0 ± 0.2
% You can do simple arithmetics on these values.
>> en1 + en2
ans = 
ErrorNum:
7.0 ± 0.4
>> en1 * en2
ans = 
ErrorNum:
10.0 ± 1.2
>> en1 / en2
ans = 
ErrorNum:
2.5 ± 0.3

These calculations obey basic error propagation rules. The results are shown using proper significant figures that are inferred from the error, which is the leading order of the error except when the leading order is 1.

% You should not worry about values much smaller than the error.
>> ErrorNum(1.2531, 0.2)
ans = 
ErrorNum:
1.3 ± 0.2
>> ErrorNum(1.2531, 0.02)
ans = 
ErrorNum:
1.25 ± 0.02
>> ErrorNum(1.2531, 0.002)
ans = 
ErrorNum:
1.253 ± 0.002

This will avoid a typical mistake of a science beginner who unnecessarily reports a 10-digit number for every result.

The ErrorNum can also be a vector or matrix. Currently, it only does the element-wise calculation.

>> m1 = ErrorNum([5, 4; 3, 2], 0.3 * ones(2, 2))
ans = 
ErrorNum:
5.0 ± 0.3,  4.0 ± 0.3
3.0 ± 0.3,  2.0 ± 0.3
>> m1 * en1
ans = 
ErrorNum:
25 ± 2,  20.0 ± 1.9
15.0 ± 1.7,  10.0 ± 1.6

You can also run basic functions like sum or mean on the ErrorNum matrix.

>> mean(m1)
ans = 
ErrorNum:
4.0 ± 0.2,  3.0 ± 0.2
>> sum(m1, 2) % summing direction also works!
ans = 
ErrorNum:
9.0 ± 0.4
5.0 ± 0.4

If you have some data, you can create ErrorNum out of it. It calculates the mean and the standard error of the mean (SEM) automatically.

% Note that you have to specify the direction to calculate.
>> data1 = [5.3, 5.2, 5.1, 5.8, 5.5]; ErrorNum.create(data1, 2)
ans = 
ErrorNum:
5.38 ± 0.12
>> data2 = ErrorNum.create(randn(30, 5) + 5, 1)
data2 = 
ErrorNum:
4.76 ± 0.15,  4.60 ± 0.18,  4.9 ± 0.2,  4.99 ± 0.17,  5.14 ± 0.19

In the next post, I will explain the advanced usage of the ErrorNum class.

How to get my Matlab software

A Japanese version is here.

Please follow this instruction to get my software in your Matlab environment.

First, clone my Github repository to your local folder

$git clone https://github.com/shixnya/code-for-science.git

You can also download it manually from the Github page. Click on “Clone or download” and choose “Download ZIP”.

Once you have it, add the code-for-science folder to your Matlab path (follow this instruction).

The code may update. If you are using git, “git pull” will update them.

That’s it. Enjoy!