A for loop that will run until a specific mathematical value is found

조회 수: 4 (최근 30일)
Hello, I am very new to Matlab and I am pretty much sure that my issue is a very simple one; still couldn't find a clear solution so wanted to ask: I want to calculate some internal hydraulic parameters of a marine outfall diffuser pipe, and for the for loop, I want it to continue until the project discharge Q is a certain value, like 350 l/s. The code is here, which is not written by me, and in the code the number of ports discharging the effluent is known, and the discharge is unknown. I want it to be the opposite, the discharge is known and the number of ports required to discharge that amount of effluent should be determined by looking at the number of iterations of course. Thanks in advance!
g=9.81; % (m/s2)
Vmin=0.55; % (m/s)
Vmax=1.2; % (m/s)
f=0.03; % dimensionless
L=5; % (m)
hf(1)=0; % (m)
e(1)=0.23; % (m)
cq(1)=0.91; % dimensionless
D(1:10)=0.15; %(m)
%First Port
a(1)=pi*(D(1))^2./4; % (m2)
q(1)=cq(1)*a(1)*sqrt(2*g*e(1)); %[m3/s]
Q(1)=q(1); % (m3/s)
d(1)=floor(100*(sqrt(4*Q(1)/(pi*Vmin))))/100; % (m)
V(1)=Q(1)/(pi*(d(1)^2./4)); % (m/s)
U(1)=q(1)/(pi*D(1)^2./4); % (m/s)
%Ports 2-10
for i=2:10
hf(i)=f*(L/d(i-1))*(V(i-1)^2./(2*g)); % (m)
e(i)=e(i-1)+hf(i); % (m)
cq(i)=0.975*(1-((V(i-1))^2)/(2*g*e(i)))^(3/8); % boyutsuz
a(i)=pi*(D(i))^2./4; % (m2)
q(i)=cq(i)*a(i)*sqrt(2*g*e(i)); % (m3/s)
Q(i)=Q(i-1)+q(i); % (m3/s)
d(i)=d(i-1); % (m)
V(i)=Q(i)/(pi*(d(i)^2./4)); % (m/s)
while (V(i) >= Vmax)
d(i)=floor(100*(sqrt(4*Q(i)/(pi*Vmin))))/100;
V(i)=Q(i)/(pi*(d(i)^2./4));
end
U(i)=q(i)/(pi*D(i)^2./4); % (m/s)
end
%Results
results=zeros(10,9);
results(:,1)=transpose(hf);
results(:,2)=transpose(e);
results(:,3)=transpose(cq);
results(:,4)=transpose(q);
results(:,5)=transpose(Q);
results(:,6)=transpose(d);
results(:,7)=transpose(V);
results(:,8)=transpose(U);
results(:,9)=transpose(Q)*1000;
max(q)/min(q)
results
  댓글 수: 2
Dyuman Joshi
Dyuman Joshi 2023년 10월 4일
It's not clear to me what the code does or what you want to do.
"and for the for loop, I want it to continue until the project discharge Q is a certain value, like 350 l/s."
Look into break or you can use a while loop instead of for loop.
%Results
results=zeros(10,9);
results(:,1)=transpose(hf);
results(:,2)=transpose(e);
results(:,3)=transpose(cq);
results(:,4)=transpose(q);
results(:,5)=transpose(Q);
results(:,6)=transpose(d);
results(:,7)=transpose(V);
results(:,8)=transpose(U);
results(:,9)=transpose(Q)*1000;
This can be modified to
results = [hf;e;cq;q;Q;d;V;U;1000*Q].'
Sam Chak
Sam Chak 2023년 10월 4일
When an object is subjected to the force of gravity and undergoes motion, whether it involves the discharge of a substance from a pipe over time or the transport of a substance in a pipe across a certain distance during a specific time interval, such behavior is deemed dynamic rather than static. Consequently, there must exist a set of differential equations that can elucidate this motion. However, I am unable to discern such equations within your code. It is plausible that you have discretized the system and incorporated it within the confines of a for-loop.
Nevertheless, it is highly desirable to have access to the mathematical representation of the continuous-time motion system model. This is how we characterize natural motion. Furthermore, algorithms are derived directly from the mathematical model, providing a means to cross-verify the accuracy of your code implementation.

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

채택된 답변

Alan Stevens
Alan Stevens 2023년 10월 4일
You could try replacing the "for i = 2:10 " loop by something like
nports = 1;
while Q(i)<Qdesired
nports = nports + 1
for i = 2:nports
% etc
end
end
  댓글 수: 1
Mahmut Cenk
Mahmut Cenk 2023년 10월 4일
Cheers mate, and thanks to others who answered as well. This one worked perfectly.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by