# Why am i receiving "Error:Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters."? for S(K)>0

2 views (last 30 days)
Mackenzie Weeks on 19 Jan 2021
Commented: Mackenzie Weeks on 6 May 2021
%General Data
G = 32.2
%none needed for this exercise
%Problem Specific Data
Simtime = 9000;
Area = 358098; %ft^2
HMAX = 6 %ft
%Time Step
N = 100; %start with 100 then go up to make it work
DT = Simtime/(N-1);
%Initial Conditions
K = 1;
Time(K) = 0;
S(K) = 0;
H(K) = 0; %depth at beginning
QI(K) = 0; %Q inlet in the beginning
%Simulation
for K = 1:N-1;
Time(K+1) = Time(K)+DT; %time at the second node
QI(K+1) = (750/pi)*(1-cos(pi*Time(K+1)/4500));
S(K+1) = S(K)+ DT*((QI(K)+QI(K+1))/(2));
H(K+1) = S(K+1)/Area
end
%Plots/ Animations
figure(1)
plot(Time,S);
xlabel('Time (s)');
ylabel('Storage');
title ('Reservoir Filling Storage vs. Time')
figure(2)
plot(Time,H);
xlabel('Time (s)');
ylabel('Depth (ft)');
title ('Reservoir Filling Depth vs Time')
%Program for Reservoir Emptying
%Implementation
Cd = .65; %discharge coefficient
d = 2; %ft
a = (pi/((4*d)^2)); %cross sectional area
Term = Cd*a*sqrt(2*G);
DT = Simtime/(N-1); %find DT use previous one and check on that?
%Initial Conditions
K = 1;
Time(K) = 0;
S(K)= S(N); %
H(K) = H(N); %max height in previous simulation
%Simulation
for S(K)>0; %as long as storage is greater than zero
Time(K+1) = Time(K)+DT;
S(K+1) = S(K)-(Term*sqrt(H(K))*DT);
H(K+1) = S/Area;
K = (K+1);
end

Mischa Kim on 19 Jan 2021
Hi, since you do not know the number of iterations it makes more sense to use a while instead of a for loop:
while S(K)>0 %as long as storage is greater than zero
Time(K+1) = Time(K)+DT;
S(K+1) = S(K)-(Term*sqrt(H(K))*DT);
H(K+1) = S(K)/Area;
K = (K+1);
end
Mackenzie Weeks on 6 May 2021
thanks!