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 vues (au cours des 30 derniers jours)
%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

Réponse acceptée

Mischa Kim
Mischa Kim le 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
  3 commentaires
Mischa Kim
Mischa Kim le 19 Jan 2021
Correct. This is because S is a vector. Are you trying to access the k-th component in this command? See also code snippet above
H(K+1) = S(K)/Area;

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Simulation and Analysis dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by