Not getting the same answer between Matlab and Simulink
2 views (last 30 days)
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:
Simulink gives me:
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".
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.