for loop to evaluate every minute

5 views (last 30 days)
Aaron LaBrash
Aaron LaBrash on 29 Apr 2022
Answered: Riccardo Scorretti on 30 Apr 2022
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 Comments
Aaron LaBrash
Aaron LaBrash on 29 Apr 2022
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 on 30 Apr 2022
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.

Sign in to comment.

Answers (1)

Riccardo Scorretti
Riccardo Scorretti on 30 Apr 2022
% 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');

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!