Simulink - How to create a conditional "if" block

Dear experts, @Stephen23
I have a simulink model, in this model there are two senarios. (heating and cooling)
1- heating: The 1st eq applies when the tempreature increases from 50 to 70 C
2- cooling: The 2nd eq is applied when the tempreature decreases from 70 to 50 C
So, when the tempreature goes from 50 to 70, I use the 1st eq. Until the tempreature reaches >=70 and start runing the cooling unit and using the 2nd eq. Until the tempreature reaches <=50. and so on.
the initial condition is the tempreature is 50 and start heating.
How can I create blocks for this purpose in simulink.
Thank you.

2 comentarios

Umar
Umar el 21 de Jun. de 2024
Hi Ahmad, To implement speed-dependent equations in Simulink, you can use the "Switch" block along with a combination of "Relational Operator" blocks to compare the speed values and switch between equations accordingly.
Here's a high-level overview of how you can achieve this:
Use a "Relational Operator" block to compare the speed signal with the threshold values (e.g., 50 and 70 rpm). Connect the output of the "Relational Operator" blocks to a "Switch" block to select between the two equations. Implement your equations in separate subsystems or blocks and connect them to the "Switch" block based on the condition. By setting up the conditions and connecting the blocks appropriately, you can create a dynamic system in Simulink that switches between equations based on the speed thresholds specified. This approach allows for a flexible and efficient way to model speed-dependent behavior in your simulation.
Ahmad Al-Issa
Ahmad Al-Issa el 21 de Jun. de 2024
Hi Umar, @Umar
Did you notice that many values located in both cases ? like (51, 52, ........69).
for the same value 55 rpm for example I use 1st eq and and then I use 2nd eq.

Iniciar sesión para comentar.

 Respuesta aceptada

Paul
Paul el 21 de Jun. de 2024

0 votos

Perhaps you can use a Relay block to model the desired behavior based on the heating/cooling analogy. The output of the Relay can be used to drive a Switch the controls which equation is used, or a pair of Enabled subsytems. Probably other options as well.

9 comentarios

Ahmad Al-Issa
Ahmad Al-Issa el 21 de Jun. de 2024
Hello
The relay block does not work because when speed values ​​like 55, 60, 65 fall between the maximum and minimum values. So the problem is: I want to keep using one equation until the maximum or minimum is reached.
Umar
Umar el 22 de Jun. de 2024

Hi Ahmad,

you can consider using the "Saturation" block in MATLAB Simulink. The Saturation block allows you to limit the output signal within a specified range, which aligns with your requirement of maintaining one equation until reaching the maximum or minimum values. By setting the upper and lower limits in the Saturation block, you can ensure that the output remains within these boundaries until the threshold is reached. By incorporating the Saturation block into your Simulink model, you can effectively control the behavior of your system and ensure that the equation continues to be applied until the predefined limits are exceeded. This approach provides a flexible and straightforward solution to address the issue with the relay block not working as expected.

Additionally, you may also consider implementing conditional statements or logic within MATLAB Function blocks to achieve similar functionality. By utilizing if-else conditions based on speed values, you can dynamically switch between equations until the maximum or minimum thresholds are reached.

Here is a code snippet that demonstrates how you can achieve this:

if speed >= maximum_threshold

 % Equation for maximum threshold 

output = equation_for_maximum_threshold;

elseif speed <= minimum_threshold

 % Equation for minimum threshold 

output = equation_for_minimum_threshold;

else

% Default equation when speed falls between thresholds

output = default_equation;

end

In this code snippet, the 'speed' variable represents the current speed value, 'maximum_threshold' and 'minimum_threshold' represent the maximum and minimum speed values respectively, and 'equation_for_maximum_threshold', 'equation_for_minimum_threshold', and 'default_equation' represent the equations to be used based on the speed value.

Hope this will help resolve your problem.

Paul
Paul el 22 de Jun. de 2024
Here's a simple example. The output of the relay goes high when the sine wave exceeds 70 and stays high until the sine wave is less than 50. At that point the relay goes low and it stays low until the sine wave exceeds 70. Is that not the desired behavior? It sounds just like: "When the temperature reaches 70 I turn on the cooling until it reaches 50 and then turn on the heating until it reaches 70 and so on." When the relay is high, do cooling. When the relay is low, do heating.
Sam Chak
Sam Chak el 22 de Jun. de 2024
The following simulation is designed to verify that your solution using the Relay block works! @Ahmad Al-Issa can use this config to get the output value from the Relay block for using it as a Switch-Case Flag attribute.
By the way, do you know the mathematical model of the Relay block? I want to test this out in MATLAB.
Ahmad Al-Issa
Ahmad Al-Issa el 22 de Jun. de 2024
Editada: Ahmad Al-Issa el 22 de Jun. de 2024
Thank you very much, it works now with relay and switch blocks
Umar
Umar el 22 de Jun. de 2024
We did it as a team. Now it’s time to celebrate
Paul
Paul el 22 de Jun. de 2024
I don't know how Simulink implements the relay block. Giving it not much thought, wouldn't it be something like:
if output = high
if input < lower threshold
output = low
endif
else ! output is low
if input > higher threshold
output = high
endif
endif
Additional logic needed to determine the initial output of the relay.
Ahmad Al-Issa
Ahmad Al-Issa el 22 de Jun. de 2024
Editada: Ahmad Al-Issa el 22 de Jun. de 2024
Thanks Umar for your contribution.
Umar
Umar el 23 de Jun. de 2024
No problem Ahmad, as a team working together, you did achieve your goal. Paul was really helpful throughout this process. Kudos to Paul for his efforts.

Iniciar sesión para comentar.

Más respuestas (1)

Sam Chak
Sam Chak el 21 de Jun. de 2024

0 votos

Firstly, could you determine the rate of change of speed (referred to as "acceleration" in physics)? This information is necessary to ascertain whether the speed is increasing from 50 rpm to 70 rpm or decreasing from 70 rpm to 50 rpm, and to apply the corresponding equation.
If your model does not directly measure acceleration, you may need to estimate it using mathematical functions or Simulink blocks. If you have a specific mathematical function, please provide it.
Strictly speaking, only the sign of acceleration is needed. If acceleration is positive, Equation #1 should be applied; if negative, Equation #2.
There are two additional issues to address:
  1. What happens if the speed remains constant at any value between 50 rpm and 70 rpm (e.g., 60 rpm) at the initial simulation? Is there a third equation for this scenario?
  2. If the speed increases linearly from 50 rpm and Equation #1 is applied, but then decreases from 60 rpm onwards without reaching 70 rpm, should Equation #1 or Equation #2 be used?

4 comentarios

Ahmad Al-Issa
Ahmad Al-Issa el 21 de Jun. de 2024
Editada: Ahmad Al-Issa el 21 de Jun. de 2024
Hello @Sam Chak
the operation happen lik the figure I uploaded, so the acceleration is not always positive from 50 to 70 rpm
and the same from 70 to 50 is not always negative. because it can fluctuating some digrees for example 50 to 51 and return to 50.5
the only condition is when reach to 70 i use 2nd eq and when i reach to 50 i use 1st eq. regardingless the speed remain constat or not. because the speed soon or later will reach to the 70.
you can consider it like a cooling something or heating some thing. when the temperature reach 70 i run the cooling until reach 50 and then run heating until reach 70 and so on.
Sam Chak
Sam Chak el 21 de Jun. de 2024
Thank you for your clarification. Please consider describing the logic using multiple IF–THEN rules and update the question so that people understand what you are asking for. At this point, I am still unable to fully grasp it.
For instance, if the speed fluctuates between 55 rpm and 65 rpm (not yet drop below 50 rpm and not yet rise above 70 rpm), should Equation #1 or Equation #2 be used?
I have a feeling that your switching logic must maintain a persistent memory of which Equation is being used in the "existing state" when the speed fluctuates between 55 rpm and 65 rpm.
Ahmad Al-Issa
Ahmad Al-Issa el 21 de Jun. de 2024
To your question (For instance, if the speed fluctuates between 55 rpm and 65 rpm (not yet drop below 50 rpm and not yet rise above 70 rpm), should Equation #1 or Equation #2 be used?)
Please just look at the figure I uploaded. If the speed fluctuates in the upward curve so I used the first equation. If it fluctuates in downward curves I will use the second equation.
Because I assume from time = 0 that the speed value is 50 so I will use the first equation. And I'll wait until I get 70 and start using the second equation.
Just imagine cooling or heating something. When the temperature reaches 70 I turn on the cooling until it reaches 50 and then turn on the heating until it reaches 70 and so on.
assume the tempreature is 50 at t = 0.
Ahmad Al-Issa
Ahmad Al-Issa el 21 de Jun. de 2024
Editada: Ahmad Al-Issa el 21 de Jun. de 2024
Hello @Sam Chak
I change the text of my question, please look on it again.
yes you are right for this (I have a feeling that your switching logic must maintain a persistent memory of which Equation is being used in the "existing state" when the speed fluctuates between 55 rpm and 65 rpm.)

Iniciar sesión para comentar.

Categorías

Más información sobre General Applications en Centro de ayuda y File Exchange.

Productos

Versión

R2021b

Preguntada:

el 21 de Jun. de 2024

Editada:

el 23 de Jun. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by