Sample time configuration in Simulink - Time & Space Complexity
We want to understand how the execution time changes when we configure sample times in a Simulink model.
How does changing sample times affect the number of operations the model performs?
Analyze the time complexity of this sample time setup in Simulink.
% Sample time configuration example
block.SampleTime = 0.1; % Fixed-step sample time
for i = 1:1000
output(i) = block.Input(i) * 2; % Operation at each sample
end
This code sets a fixed sample time and processes 1000 samples, performing a simple operation each time.
Look at what repeats in the code.
- Primary operation: Multiplying each input sample by 2.
- How many times: 1000 times, once per sample.
As the number of samples increases, the operations increase proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 multiplications |
| 100 | 100 multiplications |
| 1000 | 1000 multiplications |
Pattern observation: Doubling the number of samples doubles the operations.
Time Complexity: O(n)
This means the time to run grows directly with the number of samples processed.
[X] Wrong: "Changing the sample time does not affect how many operations run."
[OK] Correct: A smaller sample time means more samples in the same period, so more operations happen.
Understanding how sample time affects execution helps you design efficient models and explain performance in real projects.
"What if the sample time was variable instead of fixed? How would that change the time complexity?"
Practice
Solution
Step 1: Understand sample time meaning
Sample time defines the frequency at which a block runs during simulation.Step 2: Match sample time to options
Only How often a block executes during simulation describes execution frequency, others describe unrelated features.Final Answer:
How often a block executes during simulation -> Option BQuick Check:
Sample time = execution frequency [OK]
- Confusing sample time with block appearance
- Thinking sample time controls data type
- Assuming sample time affects block size
Solution
Step 1: Identify fixed sample time syntax
Fixed sample time is set by a positive number representing seconds, e.g., 0.1.Step 2: Evaluate options
'-1' means inherited, 'inf' means continuous, 'triggered' is not a numeric value.Final Answer:
Set Sample time to '0.1' -> Option CQuick Check:
Fixed sample time = positive number [OK]
- Using '-1' for fixed sample time
- Confusing 'inf' with fixed sample time
- Entering non-numeric values for sample time
Solution
Step 1: Calculate executions for Block A
Block A runs every 0.05 seconds. Total simulation time is 0.2 seconds. Executions occur at t=0, 0.05, 0.1, 0.15 (not at 0.20 since simulation ends at 0.2).Step 2: Divide total time by sample time
Number of executions = (0.2 / 0.05) = 4 (executions at 0, 0.05, 0.1, 0.15).Final Answer:
4 times -> Option AQuick Check:
Executions at t=0, 0.05, 0.1, 0.15 = 4 times [OK]
- Forgetting the initial execution at t=0
- Confusing Block A's sample time with Block B's
- Counting execution at simulation end time incorrectly
Solution
Step 1: Understand '-1' sample time meaning
Sample time '-1' means inherit from parent or driving block.Step 2: Identify why inheritance fails
If block is inside a triggered subsystem, inheritance may not work because triggered subsystems have their own timing.Final Answer:
The block is inside a triggered subsystem -> Option DQuick Check:
Triggered subsystem blocks may block inheritance [OK]
- Thinking '-1' is invalid
- Ignoring subsystem context
- Assuming continuous sample time causes issue
Solution
Step 1: Understand trade-off between speed and accuracy
Larger sample times reduce executions but may reduce accuracy; smaller sample times increase accuracy but slow simulation.Step 2: Apply selective sample time setting
Assign larger sample times to less critical blocks and smaller to critical blocks to balance speed and accuracy.Final Answer:
Use a larger fixed sample time for less critical blocks and smaller for critical ones -> Option AQuick Check:
Selective sample time balances speed and accuracy [OK]
- Setting all blocks to continuous slows simulation
- Using inherited sample time everywhere loses control
- Triggering all blocks may cause unnecessary executions
Ready to prove it?
Pass the test and this pattern moves unknown
