for loop to evaluate every minute
Show older comments
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
Riccardo Scorretti
on 29 Apr 2022
Edited: Riccardo Scorretti
on 29 Apr 2022
Can you post the variable data, so we can run the code?
Aaron LaBrash
on 29 Apr 2022
Aaron LaBrash
on 29 Apr 2022
Riccardo Scorretti
on 29 Apr 2022
Up to my understanding:
- data(:,1) = filling rate per hour
- data(:,2) = 1 or 0 whether the second pump is on / off during the whole hour
- in the loop you are computing v each hour, and you would like to compute v at each minute, assuming that the same input data (= per hour) is given
Is that right?
Aaron LaBrash
on 29 Apr 2022
Riccardo Scorretti
on 29 Apr 2022
Wait ... there is a problem: the initial value of v is 6000, but I understand that the maximum is s = 1000. Can you check?
Aaron LaBrash
on 29 Apr 2022
Like this? The idea is that it is enough to divide by 60 all rates (so as to convert in rate/minute instead of rate/hour) and add an inner loop for minutes. It is not very elegant, but it should work.
% 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');
Riccardo Scorretti
on 29 Apr 2022
It it is OK, after you can get rid of recording and plotting and adapt to your specific needs.
Aaron LaBrash
on 29 Apr 2022
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.
Answers (1)
% 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
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
