Matlab divides with the same value in every iteration

2 vues (au cours des 30 derniers jours)
Mahmut Cenk
Mahmut Cenk le 3 Nov 2023
Commenté : Voss le 3 Nov 2023
Hello everyone, I know the title isn't explaining a lot, but I just couldn't summarize it. Before starting, I want to say that I am a beginner at Matlab. The thing is, in the attached code, in each iteration of the for loop I want to calculate the ratio x/A(i) and display this ratio in the 14th column of the results matrix. For some reason when I write y=x/A(i), although A values are being calculated correctly in each iteration, it gives me completely irrelevant values for "y" so I decided to write area formula instead of A(i). But this only partially solved the problem. In each iteration, Matlab calculates x correctly (which is the cumulative sum of a(i) values up to that iteration) but it divides "x" with the latest D value it finds. Say in a code where there are 5 iterations, in each of these 5 iterations it divides "x" by the D(5) value like in the example below. I want it to divide x(i) by D(i). Writing x(i) instead of x causes other problems since sizes of the indices aren't compatible but when I write a(i) instead of a to fix that, I can't take the cumulative sums of a values. It has been long and my English I am afraid isn't that good, but I hope you can understand the issue and help me. Thanks in advance.
Here is the example result:
results =
Columns 1 through 10
0 1.3200 0 0.9100 0.0177 0.0177 0.0818 4.6310 0.0818 0.3403
0.0054 1.3254 0.0163 0.9690 0.0177 0.0353 0.0873 4.9414 0.1692 0.4892
0.0047 1.3301 0.0194 0.9679 0.0177 0.0530 0.0874 4.9443 0.2565 0.6024
0.0032 1.3333 0.0170 0.9688 0.0177 0.0707 0.0876 4.9549 0.3441 0.6977
0.0058 1.3392 0.0304 0.9638 0.0177 0.0884 0.0873 4.9401 0.4314 0.7812
Columns 11 through 16
0.4167 0.4000 0.1257 0.0249 0.6512 81.8371
0.5991 0.5500 0.1257 0.0499 0.7120 169.1589
0.7378 0.7000 0.2376 0.0748 0.6666 256.5313
0.8545 0.7000 0.3848 0.0997 0.8941 344.0914
0.9568 0.9500 0.3848 0.1247 0.6086 431.3906
%% Sabitler ve Kabuller
g=9.81;
hgelgit=0.5;
he=0.3;
Vmin=0.6;
Vmax=0.9;
f=0.02;
s=5;
Cq(1)=0.91;
rho0=1000;
rhoa=1025;
H=20;
Lana=1500;
d(1:5)=0.15;
hf(1)=0;
%% Başlangıç Hidrolik Yükü
h=(rhoa*(H+hgelgit+he))/(rho0);
E(1)=h-H;
%% İlk Çıkış Ucu
a(1)=pi*(d(1))^2./4;
q(1)=Cq(1)*a(1)*sqrt(2*g*E(1));
Q(1)=q(1);
D(1)=floor(20*(sqrt(4*Q(1)/(pi*Vmin))))/20;
A(1)=pi*D(1)^2./4;
V(1)=Q(1)/(pi*(D(1)^2./4)); %[m/s]
hizyuk(1)=0;
U(1)=q(1)/(pi*d(1)^2./4);
Dmax(1)=(sqrt(4*Q(1)/(pi*Vmin)));
Dmin(1)=(sqrt(4*Q(1)/(pi*Vmax)));
%% Diğer Çıkış Uçları
for i=2:5
hf(i)=f*(s/D(i-1))*(V(i-1)^2./(2*g));
E(i)=E(i-1)+hf(i);
Cq(i)=0.975*(1-((V(i-1))^2)/(2*g*E(i)))^(3/8);
a(i)=pi*(d(i))^2./4;
x=cumsum(a);
q(i)=Cq(i)*a(i)*sqrt(2*g*E(i));
Q(i)=Q(i-1)+q(i);
Dmax(i)=(sqrt(4*Q(i)/(pi*Vmin)));
Dmin(i)=(sqrt(4*Q(i)/(pi*Vmax)));
D(i)=D(i-1);
A(i)=pi*D(i)^2./4;
V(i)=Q(i)/(pi*(D(i)^2./4));
hizyuk(i)=V(i-1)^2./(2*g*E(i));
while (V(i) >= Vmax)
D(i)=floor(20*(sqrt(4*Q(i)/(pi*Vmin))))/20;
V(i)=Q(i)/(pi*(D(i)^2./4));
end
y=x/(pi*D(i)^2./4);
U(i)=q(i)/(pi*d(i)^2./4);
end
%% Sonuçlar
results=zeros(5,16);
results(:,1)=transpose(hf);
results(:,2)=transpose(E);
results(:,3)=transpose(hizyuk);
results(:,4)=transpose(Cq);
results(:,5)=transpose(a);
results(:,6)=transpose(x);
results(:,7)=transpose(q);
results(:,8)=transpose(U);
results(:,9)=transpose(Q);
results(:,10)=transpose(Dmin);
results(:,11)=transpose(Dmax);
results(:,12)=transpose(D);
results(:,13)=transpose(A);
results(:,14)=transpose(y);
results(:,15)=transpose(V);
results(:,16)=transpose(Q)*1000;
max(q)/min(q)
ans = 1.0699
results
results = 5×16
0 1.3200 0 0.9100 0.0177 0.0177 0.0818 4.6310 0.0818 0.3403 0.4167 0.4000 0.1257 0.0249 0.6512 81.8371 0.0054 1.3254 0.0163 0.9690 0.0177 0.0353 0.0873 4.9414 0.1692 0.4892 0.5991 0.5500 0.1257 0.0499 0.7120 169.1589 0.0047 1.3301 0.0194 0.9679 0.0177 0.0530 0.0874 4.9443 0.2565 0.6024 0.7378 0.7000 0.2376 0.0748 0.6666 256.5313 0.0032 1.3333 0.0170 0.9688 0.0177 0.0707 0.0876 4.9549 0.3441 0.6977 0.8545 0.7000 0.3848 0.0997 0.8941 344.0914 0.0058 1.3392 0.0304 0.9638 0.0177 0.0884 0.0873 4.9401 0.4314 0.7812 0.9568 0.9500 0.3848 0.1247 0.6086 431.3906

Réponse acceptée

Voss
Voss le 3 Nov 2023
You are overwriting the variable y on each iteration of the loop.
for i = 2:5
% ...
y=x/(pi*D(i)^2./4);
% ...
end
So after the loop, the value of y is the value calculated during the last iteration, which is the ratio of x on the last iteration (1x5 vector, cumulative sum of a) and that expression involving D(i), which, on the last iteration, is D(5).
Since you want to be using x(i) and D(i) (instead of x(i) and D(5)), you could:
  • Calculate only the ith element of y from the ith elements of x and D in each iteration: y(i) = x(i)/(pi*D(i)^2./4);. This would require calculating y(1) separately, e.g., before the loop like you do with other variables.
  • Or, better (because it is an array operation, which is what MATLAB is designed for): calculate all of y one time, after the loop: y=x./(pi*D.^2./4);
  2 commentaires
Mahmut Cenk
Mahmut Cenk le 3 Nov 2023
Cheers mate, you saved my life :)
Voss
Voss le 3 Nov 2023
Glad to help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by