switching between 2 values
Show older comments
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 Comments
Azzi Abdelmalek
on 4 Sep 2012
what are products are you using? Matlab? stateflow?
San
on 4 Sep 2012
Answers (2)
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 Comments
San
on 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
on 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
Azzi Abdelmalek
on 4 Sep 2012
Edited: Azzi Abdelmalek
on 4 Sep 2012
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
Categories
Find more on Incremental Learning in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!