switching between 2 values

Hi all,
My question is, how can I manage to achieve numbers fluctuating between two values. What I want to achieve is a temperature sensor. I want to set 2 temperature values, say 19-22. So, once the temperature reaches 22, I want the heater turns off and remains off until temperature drops to 19 degrees. After that heater turns on and remains its operation until 22.
I don't have predefined temperature values, so at each step T is calculated where it depends on heat input from the heater.
I have tried a couple if statements but the temperature is fluctuating either around 19 or 22 and tends to stay at one of these values.
Any helping hand will be appreciated, thanks

2 Kommentare

Azzi Abdelmalek
Azzi Abdelmalek am 4 Sep. 2012
what are products are you using? Matlab? stateflow?
San
San am 4 Sep. 2012
basic functions of matlab.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

José-Luis
José-Luis am 4 Sep. 2012
Bearbeitet: José-Luis am 4 Sep. 2012

0 Stimmen

Maybe a sine wave plus some noise would do the trick?
numSteps = 1000;
temp = 20.5 + 3.*sin([1:numSteps]') + 3*randn(numSteps,1);
plot(temp);
Temperature mean would be 20.5 degrees, the amplitude of the temperature change would be of 3 degrees plus some random change. And if you want the values to fall strictly between 19 and 22:
temp(temp>22) = 22;
temp(temp<19) = 19;
And if you only one 19's and 22's;
temp = rand(numSteps,1);
temp(temp > 0.5) = 22;
temp(temp <= 0.5) = 19;
Cheers!

4 Kommentare

San
San am 4 Sep. 2012
Thanks Jose,
But what is important for me is indeed the status of the heater. And using the net heat transfer, I will calculate the temperature for the next step.
José-Luis
José-Luis am 4 Sep. 2012
Bearbeitet: José-Luis am 4 Sep. 2012
I misunderstood the question, maybe this is what you meant.
Let's say you are currently warming your system:
isCooling = false;
isWarming = true;
counter = 1;
numSteps = 1000; %or whatever
while (counter < numSteps)
while isWarming
%Calculate T
counter = counter + 1;
if counter > numSteps
break;
end
if T > 22
isWarming = false;
isCooling = true
end
end
while isCooling
counter = counter + 1;
if counter > numSteps
break;
end
%Calculate T
if T < 19
isCooling = false;
isWarming = true;
end
end
end
Using two booleans is probably unnecessary but I left them in for clarity's sake.
San
San am 4 Sep. 2012
it seems that it doesn't respond to any of the boundaries. It is at off mode all the time. It is weird though.
Could we use for loop instead of the main while loop?
José-Luis
José-Luis am 4 Sep. 2012
Bearbeitet: José-Luis am 4 Sep. 2012
I am not sure I understand your question. Maybe the number of steps is insufficient. If you want to go through a certain number of cycles of cooling and warming, you could try:
isCooling = false;
isWarming = true;
counter = 1;
numCycles = 20; %or whatever
while (counter < numCycles)
while isWarming
%Calculate T
if T >= 22
counter = counter + 1;
isWarming = false;
isCooling = true
end
end
while isCooling
%Calculate T
if T =< 19
counter = counter + 1;
isCooling = false;
isWarming = true;
end
end
end

Melden Sie sich an, um zu kommentieren.

Azzi Abdelmalek
Azzi Abdelmalek am 4 Sep. 2012
Bearbeitet: Azzi Abdelmalek am 4 Sep. 2012

0 Stimmen

t=1:500;y=20+4*sin(0.05*t)
action=0
for k=1:length(t)
if y(k)<19
action(k)=1
elseif y(k)>22
action(k)=0
end
end
plot(t,y);hold on ; plot(action,'r');grid

1 Kommentar

San
San am 4 Sep. 2012
Hi Azzi,
unfortunately temperature is not given. So, if the heater doesn't operate, there is no way that the temperature rises.

Melden Sie sich an, um zu kommentieren.

Gefragt:

San
am 4 Sep. 2012

Community Treasure Hunt

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

Start Hunting!

Translated by