Contents

Optimizing Generated Code Using Specified Minimum and Maximum Values

This example shows how the minimum and maximum values specified on signals and parameters in a model can be used to optimize the generated code.

Overview

The specified minimum and maximum values usually represent environmental limits, such as temperature, or mechanical and electrical limits, such as output ranges of sensors.

This optimization uses these values to streamline the generated code, for example, by reducing expressions to constants or by removing dead branches of conditional statements.

NOTE: Make sure the minimum and maximum values that you specify are valid limits. Otherwise, this optimization might result in numerical mismatch with simulation.

The benefits of optimizing the generated code are:

  • Reducing the ROM and RAM consumption.

  • Improving the execution speed.

Review Minimum and Maximum Information

Consider the model rtwdemo_minmaxrtwdemo_minmax. In this model, there are minimum and maximum values specified on Inports and on the gain parameter of the Gain block.

model = 'rtwdemo_minmax';
open_system(model);

Generate Code Without This Optimization

First, generate code for this model without considering the min and max values. Create a temporary folder (in your system's temporary folder) for the build and inspection process.

currentDir = pwd;
[~,cgDir] = rtwdemodir();

Build the model using Embedded Coder.

rtwbuild(model)
### Starting build procedure for model: rtwdemo_minmax
### Successful completion of build procedure for model: rtwdemo_minmax

A portion of rtwdemo_minmax.c is listed below.

cfile = fullfile(cgDir,'rtwdemo_minmax_ert_rtw','rtwdemo_minmax.c');
rtwdemodbtype(cfile,'/* Model step', '/* Model initialize', 1, 0);
/* Model step function */
void rtwdemo_minmax_step(void)
{
  /* Switch: '<Root>/Switch' incorporates:
   *  Gain: '<Root>/Gain'
   *  Inport: '<Root>/U1'
   *  Inport: '<Root>/U2'
   *  Inport: '<Root>/U3'
   *  RelationalOperator: '<Root>/Relational Operator'
   *  Sum: '<Root>/Sum'
   */
  if (U1 + U2 <= k * U3) {
    /* Outport: '<Root>/Out1' incorporates:
     *  Sum: '<Root>/Sum2'
     */
    rtY.Out1 = (U1 + U2) + U3;
  } else {
    /* Outport: '<Root>/Out1' incorporates:
     *  Product: '<Root>/Product'
     */
    rtY.Out1 = U1 * U2 * U3;
  }

  /* End of Switch: '<Root>/Switch' */
}

Enable This Optimization

  1. Open the Configuration Parameters dialog box.

  2. In the dialog, under Code generation, select Optimize using the specified minimum and maximum values.

Alternatively, you may use the command-line API to enable the optimization:

set_param(model, 'UseSpecifiedMinMax', 'on');

Generate Code With This Optimization

In the model, with the specified minimum and maximum values for U1 and U2, the sum of U1 and U2 has a minimum value of 50. Considering the range of U3 and the specified minimum and maximum values for the Gain block parameter, the maximum value of the Gain block's output is 40.

Therefore, the output of the Relational Operator block remains false, and the output of the Switch block remains the product of the three inputs.

Build the model using Embedded Coder.

rtwbuild(model)
### Starting build procedure for model: rtwdemo_minmax
### Successful completion of build procedure for model: rtwdemo_minmax

A portion of rtwdemo_minmax.c is listed below. Observe the optimized code.

rtwdemodbtype(cfile,'/* Model step', '/* Model initialize', 1, 0);
/* Model step function */
void rtwdemo_minmax_step(void)
{
  /* Outport: '<Root>/Out1' incorporates:
   *  Inport: '<Root>/U1'
   *  Inport: '<Root>/U2'
   *  Inport: '<Root>/U3'
   *  Product: '<Root>/Product'
   *  Switch: '<Root>/Switch'
   */
  rtY.Out1 = U1 * U2 * U3;
}

Close the model and cleanup.

bdclose(model)
rtwdemoclean;
cd(currentDir)
Was this topic helpful?