for loop to evaluate every minute

조회 수: 5 (최근 30일)
Aaron LaBrash
Aaron LaBrash 2022년 4월 29일
답변: Riccardo Scorretti 2022년 4월 30일
I have a for loop written that evaluates the volume of water in a tank given a new fill rate every hour, one pump that turns on/off based on percentage fill, and one pump that it given on/off per hour. I need to make it run a new iteration every minute, but I am unsure how to do so while still breaking it into hours. Any help is greatly appreciated.
data =
20 1
20 1
0 0
375 0
200 0
20 1
50 0
150 0
50 1
60 1
100 0
150 0
180 1
170 0
100 1
250 0
100 1
210 0
170 1
150 0
50 1
25 1
25 1
50 0
s = 1000;
pC = 60;
v = s*pC*.01;
a1 = s.*0.85; % tank 85% full
a2 = s.*0.20; % tank 20% full
kt = 1:length(data);
for n = 1:length(kt)
if v>=(a1);
a = 1;
elseif v<=(a2);
a = 0;
else a = 0;
end
if data(n,2) == 1;
b = 1;
else
b = 0;
end
v = v+(data(n,1))-150.*a-100.*b
end
  댓글 수: 11
Aaron LaBrash
Aaron LaBrash 2022년 4월 29일
yes, that worked great. I did have another issue, where I was instructed to pump a (first if loop) to turn on at 85% and run until it reaches 20%, but I don't know if that's even possible.
Riccardo Scorretti
Riccardo Scorretti 2022년 4월 30일
For this, I'm afraid I cannot help. Do you mean "if it's even possible" physically?
By the way, if you are satisfied with the code, I'll post it as answer so the question can be closed.

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

답변 (1개)

Riccardo Scorretti
Riccardo Scorretti 2022년 4월 30일
% Setup variables
data = [
20 1
20 1
0 0
375 0
200 0
20 1
50 0
150 0
50 1
60 1
100 0
150 0
180 1
170 0
100 1
250 0
100 1
210 0
170 1
150 0
50 1
25 1
25 1
50 0
];
s = 1000;
pC = 60;
v = s*pC*.01;
a_ = [0]; % Just to record a and b
b_ = [0];
% Run the program
a1 = s.*0.85; % tank 85% full
a2 = s.*0.20; % tank 20% full
% kt = 1:length(data);
kt = 1:size(data,1);
for nh = 1:length(kt)
for nm = 1 : 60
if v(end)>=(a1);
a = 1;
elseif v(end)<=(a2);
a = 0;
else
a = 0;
end
if data(nh,2) == 1;
b = 1;
else
b = 0;
end
% v = v+data(nh,1)/60-150/60.*a-100/60.*b
v(end+1) = v(end) + data(nh,1)/60-150/60.*a-100/60.*b;
a_(end+1) = a;
b_(end+1) = b;
end
end
figure
subplot(2, 1, 1);
plot((1:numel(v))/60, v) ; hold on
plot([1 numel(v)]/60, [a1 a1], 'r--');
plot([1 numel(v)]/60, [a2 a2], 'r--');
grid on
xlabel('time (hour)') ; ylabel('volume');
subplot(2, 1, 2);
plot((1:numel(v))/60, a_, (1:numel(v))/60, b_) ; grid on ; legend('a', 'b')
xlabel('time (hour)') ; ylabel('a, b');

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by