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 个)
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 个评论
San
2012-9-4
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
2012-9-4
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
2012-9-4
编辑:Azzi Abdelmalek
2012-9-4
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
类别
在 帮助中心 和 File Exchange 中查找有关 Incremental Learning 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!