Why is the Switch block selecting the incorrect output?

In the figure below, I see that the switch condition evaluates to TRUE (1 ~= 0). This implies that the 'SwitchA' block should output the value of it's 1st input port i.e. [0.1111 0]. However it outputs [0 0] which is the value of the 3rd input port of the 'SwitchA' block.
However, when I replace the 3rd Input port of the 'SwitchA' block with any constant value, say 1, 'SwitchA' starts behaving correctly.
Also I have noticed that the 'SwitchA' block outputs the correct value when it's first input port has the value [0 0].
Why is the Switch block not functioning properly?

 Accepted Answer

The observed behavior is related to the output data type setting of the 'SwitchA' block. In the original model with the unit delay connected to the input port 3 of the 'SwitchA' blcok, Simulink uses an output data type of 'int16'. The 'SwitchA' block indeed selects the correct output port (1), however, the value 0.1111 suffers a precision loss in 'int16' representation and gets rounded down to 0. Therefore the output is [0 0] which coincidentally matches input port (3) value of the 'SwitchA' block. When a constant block is connected to the input port 3 of the 'SwithA' block, notice that Simulink uses a data type of higher precession ('double'). Therefore, the value 0.1111 does not incur any precision loss and the correct output is displayed.
There are multiple ways to solve this issue:
  1. Instead of having the output data type in the 'SwitchA' block set to 'Inherit via internal rule', you can set it to a specific data type with higher precision such as 'double'. You can also set it to 'Inherit: Same as first input' but keep in mind that you will have the same issue if the 3rd input of the 'SwitchA' block requires greater precision to represent the output compared to the value of the first input.
  2. In the initial case, when 'SwitchA' output data type is set to 'inherit via internal rule', Simulink uses multiple heuristics to determine the appropriate data type. This means that the data type of other blocks in the model can influence this selection such as the output data type of the 'SwitchB' block, data type of the 'Constant' [0 0] connected to the first input of this 'SwitchB' block. Consequently, you can resolve this issue by:
    1. Setting the output data type of the 'Constant' connected to the 'SwitchB' block to inherit from 'constant value' or by back propagation or a set data type of higher precision such as 'double' as in this case it is currently set to 'int16'.
    2. Setting the output data type of the 'SwitchB' block to 'double' or other fixed data type of higher precision.
To view signal data type please navigate to 'Debug' tab in the Simulink toolstrip > Information Overlays > Port > select 'Base Data Types'.
Please find some more information on precision loss detection in our MathWorks documentation: https://www.mathworks.com/help/fixedpoint/overflow-and-precision-loss-detection.html

More Answers (0)

Categories

Products

Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!