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

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 4일
what are products are you using? Matlab? stateflow?
San
San 2012년 9월 4일
basic functions of matlab.

댓글을 달려면 로그인하십시오.

답변 (2개)

José-Luis
José-Luis 2012년 9월 4일
편집: José-Luis 2012년 9월 4일

0 개 추천

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
San 2012년 9월 4일
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 2012년 9월 4일
편집: José-Luis 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
San 2012년 9월 4일
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 2012년 9월 4일
편집: José-Luis 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
Azzi Abdelmalek 2012년 9월 4일
편집: Azzi Abdelmalek 2012년 9월 4일

0 개 추천

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

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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

San
2012년 9월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by