Not getting the same answer between Matlab and Simulink

2 次查看(过去 30 天)
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 个评论
Thomas Marullo
Thomas Marullo 2013-6-19
The command prompt gives me the same answer as the Matlab code. It differs from the Simulink code response.
Thomas Marullo
Thomas Marullo 2013-6-19
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

请先登录,再进行评论。

回答(1 个)

Mike Hosea
Mike Hosea 2013-11-2
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.

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by