MATLAB Examples

Optimize 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 are used to optimize the generated code.

Contents

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, it reduces expressions to constants or removes 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_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.

currentDir = pwd;
[~,cgDir] = rtwdemodir();
rtwconfiguredemo(model,'ERT')
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)
{
  int32_T tmp;

  /* Sum: '<Root>/Sum' incorporates:
   *  Inport: '<Root>/U1'
   *  Inport: '<Root>/U2'
   *  Sum: '<Root>/Sum2'
   *  Switch: '<Root>/Switch'
   */
  tmp = U1 + U2;

  /* Switch: '<Root>/Switch' incorporates:
   *  Gain: '<Root>/Gain'
   *  Inport: '<Root>/U3'
   *  RelationalOperator: '<Root>/Relational Operator'
   *  Sum: '<Root>/Sum'
   */
  if (tmp <= k * U3) {
    /* Outport: '<Root>/Out1' incorporates:
     *  Sum: '<Root>/Sum2'
     */
    rtY.Out1 = tmp + U3;
  } else {
    /* Outport: '<Root>/Out1' incorporates:
     *  Inport: '<Root>/U1'
     *  Inport: '<Root>/U2'
     *  Product: '<Root>/Product'
     */
    rtY.Out1 = U1 * U2 * U3;
  }
}

Enable This Optimization

  1. Open the Configuration Parameters dialog box.
  2. On the Optimization pane, select Optimize using the specified minimum and maximum values.

Alternatively, you can enable this optimization by setting the command-line parameter.

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.

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

Configure and build the model using Embedded Coder.

rtwconfiguredemo(model,'ERT')
rtwbuild(model)
### Starting build procedure for model: rtwdemo_minmax
### Successful completion of build procedure for model: rtwdemo_minmax

View the optimized code from rtwdemo_minmax.c.

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)
{
  /* 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)