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

1 次查看(过去 30 天)
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

采纳的回答

Jan
Jan 2012-3-24
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 个评论
Nick Tsui
Nick Tsui 2012-3-24
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by