Search code examples
matlabnumerical

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:

  • 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!


Solution

  • 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.