MATLAB Answers

Could someone please tell me why this code is printing(Dam level too low) for almost every output.

4 views (last 30 days)
Ned Williams
Ned Williams on 20 Sep 2021
Commented: Stephen on 20 Sep 2021
clc;clear;close
% given dimensions of dam
sa=352760; % surface area of lake in m
H=26; % height of dam wall in m
% Volume of the dam in ML
Maxvol=352760*26*10^-3; % V=sa*H
% Volume of Lake at Nov.01,2020 in ML
Newvol(1)=352760*18.8*10^-3;
% Volume of lake at 100%, 80%, 30%
p100=1*Maxvol;
p80=0.8*Maxvol;
p30=0.3*Maxvol;
% Water Out of lake every day
Qout=5.33*60*60*24*10^-3;
% set new variables to 0
a=0; % will be used for times dam was over capacity
b=0; % will be used for times dam was under 30% capacity
% Load in data (river discharge rates)
Qin=xlsread('data_RiverDischarge.xls');
matQout=[Qout]; % set up matrix for Qout value each day
% Use 'for' loop to calculate daily volume of lake
for k=1:length(Qin) % index for vector of Qin
Newvol(k+1)=Newvol(k)+(Qin(k)-Qout);
if Newvol>=p100;
Qout=3.2*(5.33*60*60*24*10^-3);
fprintf('On day %1.0f: Dam is full!\n',k)
a=a+1;
elseif Newvol>=p80 & Newvol<p100;
Qout=3.2*(5.33*60*60*24*10^-3) ;
elseif Newvol<=p80 & Newvol>=p30;
Qout=(5.33*60*60*24*10^-3) ;
else Newvol<p30;
Qout=(1/2)*Qin(k);
fprintf('On day %1.0f: Dam level is low!\n',k)
b=b+1;
end
matQout=[matQout Qout]; % store all Qout values
end
% Display a and b
fprintf ('The number of times the dam was over capacity is %5.0f \n', a)
fprintf ('The number of times the dam was under 30 percent capacity is %5.0f \n', b)
% plot relevant data points
subplot(3,1,1)
plot(Qin)
title('River Disharge into Dam Over Time')
xlabel('Days from November 01, 2020 (1-155)')
ylabel('River Disharge (ML)')
subplot(3,1,2)
plot(matQout)
title('Daily released from Dam over Time')
xlabel('Days from November, 01, 2020 (1-155)')
ylabel('Daily release (ML)')
subplot(3,1,3)
plot(Newvol)
title('Daily Volume of Dam Over Time')
xlabel('Days from November 01, 2020 (1-155)')
ylabel('Volume (ML)')
  1 Comment
Stephen
Stephen on 20 Sep 2021
Also note that
else Newvol<p30;
..
is equivalent to
else
Newvol<p30;
..
I.e. that logical comparison has absolutely no effect on the ELSE.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 20 Sep 2021
Newvol(k+1)=Newvol(k)+(Qin(k)-Qout);
Newvol is a vector.
if Newvol>=p100;
You are testing the entire vector in that statement; Newvol>p100 is going to return back a vector of values, one for each entry in the vector Newvol. And in MATLAB, when you use if, the condition is considered true only if all of the values being tested are non-0 (that is, are "true") -- if you have even one value that does not satisfy the test, the condition is false.
You do the same thing on your elseif conditions, so eventually you fall through to your else
  4 Comments
Walter Roberson
Walter Roberson on 20 Sep 2021
Newvol(k) is effectively "start of day". Newvol(k+1) is effectively "end of day". Which one do you want to give the messages for?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!