Subsystem masking in Simulink - Time & Space Complexity
We want to understand how the time it takes to run a Simulink model changes when using subsystem masking.
Specifically, how does adding a mask affect the execution time as the model grows?
Analyze the time complexity of this Simulink subsystem with a mask.
Subsystem Mask:
- Input block
- Processing blocks inside
- Mask initialization code
- Output block
This subsystem takes inputs, runs some processing inside, and outputs results, all wrapped in a mask.
Look for repeated actions inside the masked subsystem.
- Primary operation: Processing blocks inside the subsystem run once per simulation step.
- How many times: Once per step, but mask initialization runs only once at start.
As the number of blocks inside the masked subsystem grows, the processing time grows roughly in proportion.
| Input Size (blocks inside) | Approx. Operations |
|---|---|
| 10 | 10 processing steps + 1 mask init |
| 100 | 100 processing steps + 1 mask init |
| 1000 | 1000 processing steps + 1 mask init |
Pattern observation: The mask initialization cost is small and fixed; the main cost grows linearly with the number of blocks inside.
Time Complexity: O(n)
This means the execution time grows linearly with the number of blocks inside the masked subsystem.
[X] Wrong: "Masking adds a big extra cost every simulation step regardless of subsystem size."
[OK] Correct: Mask initialization runs once, not every step; the main cost depends on the blocks inside, not the mask itself.
Understanding how subsystem masking affects execution helps you design efficient models and explain performance trade-offs clearly.
"What if the mask included complex initialization code that runs every simulation step? How would the time complexity change?"
Practice
masking a subsystem in Simulink?Solution
Step 1: Understand what masking does
Masking a subsystem hides its internal blocks and connections from the user.Step 2: Identify the main benefit
This makes the subsystem easier to use by showing only relevant parameters and controls.Final Answer:
To hide the internal details and provide a simple interface -> Option BQuick Check:
Masking = Hide complexity [OK]
- Thinking masking speeds up simulation
- Confusing masking with code generation
- Assuming masking changes solver settings
Solution
Step 1: Locate mask creation menu
In Simulink, you right-click the subsystem and select Mask > Create Mask to start masking.Step 2: Add parameters in mask editor
Inside the mask editor, you can add parameters to customize the subsystem interface.Final Answer:
Right-click subsystem > Mask > Create Mask > Add parameter -> Option DQuick Check:
Mask parameters added via Mask Editor [OK]
- Trying to add parameters by adding blocks inside subsystem
- Using Properties instead of Mask menu
- Double-clicking subsystem does not add mask parameters
gainValue = str2double(get_param(gcb, 'GainParam')); set_param([gcb '/Gain'], 'Gain', num2str(gainValue));
What does this code do when the mask parameter
GainParam changes?Solution
Step 1: Read the initialization code
The code reads the mask parameter 'GainParam' as a string, converts it to a number, then sets the internal Gain block's 'Gain' parameter.Step 2: Understand the effect on the subsystem
Changing the mask parameter updates the internal block's gain value accordingly.Final Answer:
It updates the internal Gain block's Gain parameter to the new value -> Option AQuick Check:
Mask init code updates internal block parameters [OK]
- Confusing parameter update with subsystem reset
- Thinking parameter name changes
- Assuming syntax error due to semicolon (semicolon is optional in MATLAB)
Threshold. The initialization code is:threshold = get_param(gcb, 'Threshold'); set_param([gcb '/Compare'], 'Threshold', threshold);
But the internal Compare block's threshold does not update. What is the likely error?
Solution
Step 1: Check internal block parameter names
The Compare block in Simulink does not have a parameter named 'Threshold'; it uses 'RelationalOperator' and inputs for comparison.Step 2: Identify why setting 'Threshold' fails
Since 'Threshold' is not a valid parameter, set_param silently fails or has no effect.Final Answer:
The Compare block does not have a 'Threshold' parameter -> Option AQuick Check:
Block parameters must match internal block's actual names [OK]
- Assuming all mask parameters map directly to internal block parameters
- Not checking block documentation for parameter names
- Ignoring type conversions when needed
Solution
Step 1: Design mask parameters for user input
Use a popup parameter for filter type and a numeric parameter for cutoff frequency to simplify user choices.Step 2: Use initialization code to update internal blocks
In the mask initialization code, read these parameters and set the internal filter blocks' types and cutoff frequencies accordingly.Final Answer:
Add popup mask parameter for filter type, numeric parameter for cutoff, then in init code set internal blocks' parameters accordingly -> Option CQuick Check:
Mask parameters + init code = dynamic internal block setup [OK]
- Not using mask parameters for user input
- Trying to change mask parameters during simulation
- Avoiding masking by creating multiple subsystems
Ready to prove it?
Pass the test and this pattern moves unknown
