Not getting the same answer between Matlab and Simulink

조회 수: 3 (최근 30일)
Thomas Marullo
Thomas Marullo 2013년 6월 19일
답변: Mike Hosea 2013년 11월 2일
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