mex c file and .m file gives slightly, but not neglegible, different returns!
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
1 comentario
Respuesta aceptada
Jan
el 24 de Mzo. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!