Signal inversion inside an interval

1 view (last 30 days)
Bruno Peixoto
Bruno Peixoto on 30 Oct 2020
Answered: sanidhyak on 10 Mar 2025
Hi folks,
I have a dynamic system with triggered logic.
Statement: as soon as x is between two values, do not alter y. At the limit x = x_min or x = x_max, invert the signal of y. The variable x has a saturation function on x = x_min and x = x_max.
Attempt: I have tried to use the Interval test block. This block has inverted logic and triggers only if the value is outside the predefined interval. Hence, if one defines an infinitesimal epsilon near to the boundary, as soon as x is epsilon of it, inverts the signal y.
Problem: If the next integration step results also on a value inside this epsilon-interval, which makes the signal invert very quickly.
If there is an ellegant way to solve it, I thank sincerely.

Answers (1)

sanidhyak
sanidhyak on 10 Mar 2025
I understand that you are trying to implement a triggered logic system in MATLAB where y should remain unchanged when x is within a specific range and invert only when x reaches x_min or x_max.
Your current approach using the Interval Test Block causes rapid toggling due to repeated integration steps inside the epsilon threshold. To resolve this issue, I suggest two solutions:
Solution 1: MATLAB “Function Block” Approach
You can use a MATLAB Function Block with a persistent variable to ensure y toggles only when x reaches x_min or x_max. Kindly refer to the following function:
function y = control_logic(x, y_prev, x_min, x_max)
% Define a persistent variable to store the previous state of y
persistent y_state;
if isempty(y_state)
y_state = y_prev; % Initialize y with the given previous value
end
% Check if x is at the boundaries and invert y
if x <= x_min || x >= x_max
y_state = -y_state;
end
% Output the updated y value
y = y_state;
end
This code retains the previous value of y using a persistent variable and toggles y only at x_min or x_max, preventing rapid oscillations.
Solution 2: Stateflow Chart Approach
Alternatively, you can use a Stateflow Chart for a more structured approach. Follow these steps:
  1. Open Simulink and add a Stateflow Chart”.
  2. Define three states:
- "Hold" (when x is within bounds).
- "Invert_Positive" (when “x = x_max).
- "Invert_Negative" (when x = x_min).
3. Set up transitions:
- If “x = x_max, switch to "Invert_Positive" and set y = -y.
- If “x = x_min, switch to "Invert_Negative" and set y = -y.
- Otherwise, stay in "Hold".
This would prevent rapid toggling by ensuring the inversion occurs only at strict boundary conditions and remains stable during integration.
Kindly refer to the following MATLAB documentations for further references:
Cheers & Happy Coding!

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!