mex c file and .m file gives slightly, but not neglegible, different returns!

1 view (last 30 days)
I have a MALTAB script doing some iterative algorithm give a return. This .m file was also coded as a mex c file which gives same return. Both returns is a 28374 X 1 vector, the data type is double in mex c file.
I ran these two files, and the returns are slightly different! And the difference amplifies as number of the iteration increases. For example, at 2nd iteration, the error is about 25; but at 5th iteration, the error goes up to about 500;
I also looked at the returns by plotting them. They look similar, apparently have similar pattern/shape. But they are just not the same, obviously.
I have spent much time double checking my code, especially for the mex c code, but I did not spot anything wrong yet.
I am using my windows 7 laptop to run the program on a Ubuntu remotely.
I have no idea what could be the cause of the problem. Anyone has any similar experience? Big edien? little endien? Any pointer is appreciated !
Nick

Accepted Answer

Jan
Jan on 24 Mar 2012
This is a usual effect of the limited floating point precision. Even for a simple addition of three doubles the order matters:
(a + b) + c ~= a + (b + c) % in general
Trigonometric functions, e.g. ACOS and LOG10 reply slightly different results also, if the arguments are inside intervals of numerical instabilities.
The growing of the difference with the number of iterations mean, that the method is instable.
  1 Comment
Nick Tsui
Nick Tsui on 24 Mar 2012
One thing I want to mention is, it was in each iteration, those two functions (coded in mex c file and .m file) were both called, for the purpose of comparison. Those two functions don't have any iteration algorithm inside themselves.
Inside the two functions, there are only some math, like add, subtract, times, divide, etc. In any of the two functions, it is basically to get a 78400 X 28374 matrix, then sum over the column.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!