How can I prevent the calculation of intermediary results while code generation with the embedded coder?
Show older comments
For example I have created a Simulink subsystem with the following M-Function.
function y = fcn(u1,u2)
%#codegen
p1 = 0.13285;
p2 = 999.1;
p3 = 9.806;
p4 = 132.535;
p = p1*p2*p3;
y = p2*p4*u1 - p*u2;
end
After running code generation with "C++ Code --> Build this Subsystem" the Embedded Coder create the following code (extract from Subsystem.cpp).
/* Model step function */
void Subsystem_step(void)
{
/* Outport: '<Root>/Out1' incorporates:
* Inport: '<Root>/In1'
* Inport: '<Root>/In2'
* MATLAB Function: '<S1>/MATLAB Function'
*/
/* MATLAB Function 'Subsystem/MATLAB Function': '<S2>:1' */
/* '<S2>:1:11' */
Subsystem_Y.Out1 = 132415.7185 * Subsystem_U.In1 - 1301.5546456099999 *
Subsystem_U.In2;
}
As you can see, the coder calculates intermediary results without using the absolute values in the output equation. For complex systems the inaccuracy of the calculation generates a divergence that produces instability. What chances in the configuration parameters are necessary to prevent this simplification of calculation while code generation?
current configuration: * system target file: ert.tlc (Visual C/C++ Solution File) * language: C++ * compiler optimization level: optimization off * prioritized objectives: safety precation, debugging * code replacement library: C++ (ISO) * shared code placement: auto * parentheses level: maximum
4 Comments
Walter Roberson
on 31 May 2013
What "inaccuracy in the calculation" ? p1*p2 is going to produce the same result whether done at compilation time or at run time.
Kaustubha Govind
on 31 May 2013
I have the same question as Walter. What you're seeing is an optimization, since the 3 quantities in your code are constants. One way to avoid the optimization might be to pass those quantities in as tunable parameters instead.
Ralf
on 31 May 2013
Walter Roberson
on 31 May 2013
There is no difference between precalculation of p1*p2*p3 and run-time calculation, provided the architecture stays constant.
Are you compiling on one brand of processor (e.g., Intel) but executing on another (e.g., AMD) ? If so then Yes, in such a case you could get a one bit difference for p1*p2*p3 (unless the rounding settings were different for the two systems.)
There can be a difference between p1*p2*p3*u2 and u2*p1*p2*p3 due to round-off, as order of operations is important in floating point. MATLAB uses left-to-right evaluation for expressions of the same precedence.
If these kinds of differences are critical you should be considering using the fixed-point toolbox.
Accepted Answer
More Answers (0)
Categories
Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!