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

조회 수: 3 (최근 30일)
%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
Mischa Kim 2021년 1월 19일
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
Mischa Kim
Mischa Kim 2021년 1월 19일
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;

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulation and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by