Philipp
Philipp

Reputation: 11814

Numerical problems in Matlab: Same input, same code -> different output?

I am experiencing problems when I compare results from different runs of my Matlab software with the same input. To narrow the problem, I did the following:

Without changing the called method, I did another run with

I can't believe the comparison in the last "line" detects slight differences. The calculations include single and double precision numbers, the error is in the magnitude of 1e-10 (the output is a double number).

The only possible explanation I could imagine is that either Matlab looses some precision when saving the variables (which I consider very unlikely, I use the default binary Matlab format) or that there are calculations included like a=b+c+d, which can be calculated as a=(b+c)+d or a=b+(c+d) which might lead to numerical differences.

Do you know what might be the reason for the observations described above?

Thanks a lot!

Upvotes: 4

Views: 1716

Answers (3)

Philipp
Philipp

Reputation: 11814

it really seems to be caused by the single/double mix in the calculations. Since I have switched to double precision only, the problem did not occur anymore. Thanks to everybody for your thoughts.

Upvotes: 1

Amro
Amro

Reputation: 124553

To ensure reproducible results, especially if you are using any random generating functions (either directly or indirectly), you should restore the same state at the beginning of each run:

%# save state (do this once)
defaultStream = RandStream.getDefaultStream;
savedState = defaultStream.State;
save rndStream.mat savedState

%# load state (do this at before each run)
load rndStream.mat savedState
defaultStream = RandStream.getDefaultStream();
defaultStream.State = savedState;

Upvotes: 0

second
second

Reputation: 28637

these could be rounding errors. you can find the floating point accuracy of you system like so:

>> eps('single')

ans =

  1.1921e-07

On my system this reports 10^-7 which would explain discrepancies of your order

Upvotes: 0

Related Questions