Create A Rate-Based Model

A rate-based model is a model with explicitly scheduled subsystems. You can schedule a subsystem with a periodic execution rate by specifying the Sample time parameter for an Inport block connected to the Subsystem block, the Subsystem block, or a block within the Subsystem block where Sample time can be specified (for example, a Delay block).

Note

Using Continuous time blocks such as Integrator blocks are not allowed. Instead use discrete time equivalent blocks

Consider the following model with two atomic Subsystem blocks. Subsystem1 multiplies its input by 2 while Subsystem2 multiplies its input by 4.

  1. Open the Inport 1 dialog box. On the Signal Attributes tab, set the Sample time to 0.2.

  2. Open the Inport 2 dialog box. On the Signal Attributes tab, set the Sample time to 0.4.

  3. If a rate-based model has multiple rates, single tasking is not allowed. Select the check box for the configuration parameter Treat each discrete rate as a separate task.

Multi-Tasking and Multi-Rate Model for Code Generation

Selecting single-tasking versus multi-tasking and single-rate versus multi-rate controls entry points in the generated code.

Configuration ParameterExplicitly Scheduled RatesGenerated Code Entry Points

Single-tasking

Single-rate

Subsystem1

Subsystem2

One entry-point function called periodically every 0.2 seconds

void model_component_step(void)
{
   model_component_Y.Out1 = 2.0 * model_component_U.In1;
   model_component_Y.Out2 = 4.0 * model_component_U.In2;
}

Single-tasking

Multi-rate

Subsystem1

Subsystem2

One entry-point function called periodically every 0.2 seconds, a schedule counter in the function determines which rates execute at which sample times.

void model_component_step(void)
{
   model_component_Y.Out1 = 2.0 * model_component_U.In1;
   if (model_component_M->Timing.TaskCounters.TID[1] == 0) {
       model_component_Y.Out2 = 4.0 * model_component_U.In2;
   }
   rate_scheduler();
}

Multi-tasking

Single-rate

Subsystem1

Subsystem2

One entry-point function, the same code as single-tasking with a single-rate. Not a use case, but the code generates without an error

Multi-tasking

Multi-rate

Subsystem1

Subsystem2

Two entry-point functions one called periodically every 0.2 seconds and the other called periodically every 0.4 seconds. Rates are executed using a prioritized preemptive multitasking scheme. Faster rates are assigned higher priorities and thus executed first.

void model_component_step0(void)
{
   model_component_Y.Out1 = 2.0 * model_component_U.In1;
}

void model_component_step1(void)
{
   model_component_Y.Out2 = 4.0 * model_component_U.In2;
}