Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return values differ depending on vector size

Tags:

matlab

I noticed that for example the log(x) function returns slightly different values when called with vectors of different sizes in MATLAB.

Here is a minimal working example:

x1 = 0.1:0.1:1;
x2 = 0.1:0.1:1.1;

y1 = log(x1);
y2 = log(x2);

d = y1 - y2(1:length(x1));

d(7)

Executing returns:

>> ans = 
  -1.6653e-16

The behaviour seems to start when the vector becomes greater than 10 entries.

Although the difference is very small, when being scaled by a lot of operations using large vectors, the errors became big enough to notice.

Does anyone have an idea what is happening here?

like image 385
Martijn Arts Avatar asked Dec 29 '25 09:12

Martijn Arts


1 Answers

The differences exist in x1 and x2 and those errors are propagated and potentially accentuated by log.

max(abs(x1 - x2(1:numel(x1))))
% 1.1102e-16

This is due to the inability of floating point number to represent your data exactly. See here for more information.

like image 102
Suever Avatar answered Dec 30 '25 23:12

Suever