Not getting the same answer between Matlab and Simulink

3 views (last 30 days)
Thomas Marullo
Thomas Marullo on 19 Jun 2013
Answered: Mike Hosea on 2 Nov 2013
I have a Matlab function, A = M \ (x * V), where M is a matrix, x is a scalar and V is a vector
I also implemented this in Simulink using both a Matlab Embedded Function and a native block way in Simulink. Simulink is in Fixed time Discrete mode, so no solver.
I'm not getting the same answer between the two programs.
Matlab gives me:
-8.999136545983721e-04
-1.616938508965329e-14
-5.172927505793807e-06
Simulink gives me:
-8.999136545983721e-04
-1.616938508965380e-14
-5.172927505793800e-06
The last two values are different. And over time this error propagates through my model making the difference larger. I narrowed the problem down to the "\" left handed matrix division not giving the same results in Simulink as in Matlab.
Is there something with precision that I need to set so that Matlab and Simulink gives the same answer out to the same number of decimal points? All checks I do show that all the input and output variables are of class "double".
  3 Comments
Thomas Marullo
Thomas Marullo on 19 Jun 2013
Here is a link to an example of this problem. You can see the results differ and the error gets worse over time. https://dl.dropboxusercontent.com/u/600821/ComparisonProblem.zip

Sign in to comment.

Answers (1)

Mike Hosea
Mike Hosea on 2 Nov 2013
Considering complexity of the operation, those are the same answers. All meaningful requirements on floating point numbers are expressed in terms of tolerances. Probably the condition number of your coefficient matrix is 100 or perhaps 1000. Often people do want to get all the same bits because, for them, having any difference triggers some expensive bureaucratic reaction to re-validate the system. Unfortunately, the only reliable way to get the same bits is to use the same (single-threaded) binary on the same hardware in the same state. Generating C code that is in some sense equivalent to MATLAB and then compiling it and running it in a different context, let alone possibly on different hardware, cannot reliably yield all the same floating point bits. Sometimes you will get lucky but often not.

Community Treasure Hunt

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

Start Hunting!