for loop to evaluate every minute

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

Can you post the variable data, so we can run the code?
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 = 600
v = s*pC*.01
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?
that is correct. The part that is stumping me is that my final outputs need to be volume max, min, and average per hour. I haven't been able to figure out how to get it to evaluate per minute and still be able to separate into each hour.
Wait ... there is a problem: the initial value of v is 6000, but I understand that the maximum is s = 1000. Can you check?
pC should be 60, not 600. For this value to work I had to add "else a = 0" to the end of the first if statement
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');
It it is OK, after you can get rid of recording and plotting and adapt to your specific needs.
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.
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)

% 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

Products

Release

R2021b

Tags

Asked:

on 29 Apr 2022

Answered:

on 30 Apr 2022

Community Treasure Hunt

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

Start Hunting!