views:

79

answers:

3

Hi, 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:

* save all relevant variables using Matlab's save() method
* call a method which calculates something
* save all relevant output variables again using save()

Without changing the called method, I did another run with

* load the variables saved above and compare with the current input variables using isequal()
* call my method again with the current input variables
* load the out variables saved above and compare.

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!

A: 

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

second
but shouldn't rounding always return the same value? Or does it depend on "indeterministic" factors?
Philipp
A: 

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;
Amro
I do stream0 = RandStream('mrg32k3a','Seed',myConstantSeed); RandStream.setDefaultStream(stream0); before each run. This should be working too, shouldn't it?
Philipp
Yes its the same thing, only using a seed to initialize the state.
Amro
The only thing I can think of, is to try to compare the results of two saved runs (in case save/load are introducing some loss of precision) as @AlexFeinman suggests in the comments above..
Amro
+1  A: 

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.

Philipp

related questions