Problem with cycle within a cycle output

1 vue (au cours des 30 derniers jours)
Tomas White
Tomas White le 22 Mai 2022
Commenté : Star Strider le 22 Mai 2022
I have a bit of a problem with this code, the first loop cycles through a matrix with two columns and four rows and the second loop cycles through another matrix with four columns and eight rows. The second cycle inputs each combination that can be made with one row from the first matrix and all the rows from the second one until it finishes. At this point the second loop ends and exits to the first loop where it jumps back to the start and repeats the process again with the remaining three rows. In total there should be 32 results assuming n=4, but instead its just generating 8 results (result vector corresponds to "w1"). Any help is much appreciated.
clc
clear
%%Inputs:
ar=5; %aspect ratio
lamda=1; %taper ratio, (= 1 because cr = ct)
swept=45; %wing swept angle
n=4; %panle number
%%Table 7.2,s:
panelv=(1:n);
panely=panelv';
longxt=(tand(swept)*0.5+(1/ar));
panelx=(0:n-1);
%x-components
xm=((longxt/n)+(1/(2*n))*panelx+0.0375)';
x1n=((panelv-1)*(0.5/n)*(tand(swept))+((1/ar)/4))';
x2n=((panelv)*(0.5/n)*(tand(swept))+((1/ar)/4))';
%y-components-s
yms=((panely-1)*(0.5/n)+((0.5/n)/2));
y1ns=((panely-1)*(0.5/n));
y2ns=((panely)*(0.5/n));
%y-components-p
ymp=(((panely-1)*(0.5/n)+((0.5/n)/2))*-1);
y1np=((panely-1)*(0.5/n)*-1);
y2np=((panely)*(0.5/n)*-1);
%Table 7.2-s display
m72=[xm,yms,x1n,y1ns,x2n,y2ns];
%disp(m72);
%Table 7.2-p display
m72p=[xm,ymp,x1n,y1np,x2n,y2np];
%disp(m72p);
%Table 7.2 xm,ym - s
m72xmym=[xm,yms];
%disp(m72xmym);
%Table 7.2 x1,y1,x2,y2 - s
m72xiy1x2y2=[x1n,y1ns,x2n,y2ns];
%disp(m72xiy1x2y2);
%Table 7.2 x1,y1,x2,y2 - p
m72xiy1x2y2p=[x1n,y1np,x2n,y2np];
%disp(m72xiy1x2y2p);
c=[m72xiy1x2y2p;m72xiy1x2y2];
disp(c);
%loops
i=1;
j=1;
h=1;
while j<=n
at(j)=(m72xmym(j,1));
bt(j)=(m72xmym(j,2));
a=at';
b=bt';
while h<=2*n
x1c(h)=(c(h,1));
y1c(h)=(c(h,2));
x2c(h)=(c(h,3));
y2c(h)=(c(h,4));
w1(j,h)=(1/(((a(j,1))-(c(h,1)))*((b(j,1))-(c(h,4)))-((a(j,1))-...
(c(h,3)))*((b(j,1))-(b(j,1)))))*((((c(h,3))-(c(h,1)))*...
((a(j,1))-(c(h,1)))+((c(h,4))-(c(h,2)))*((b(j,1))-...
(c(h,2))))/(sqrt(((a(j,1))-(c(h,1)))^2+((b(j,1))-...
(c(h,2)))^2))-(((c(h,3))-(c(h,1)))*((a(j,1))-(c(h,3)))+...
((c(h,4))-(c(h,2)))*((b(j,1))-(c(h,4))))/(sqrt(((a(j,1))-...
(c(h,3)))^2+((b(j,1))-(c(h,4)))^2)));
h=h+1;
end
j=j+1;
end
disp(w1);

Réponse acceptée

Star Strider
Star Strider le 22 Mai 2022
If I understand correctly what you want to do, in every ‘j’ iteration, ‘h’ needs to be reinitialised, so:
h = 1;
while h<=2*n
CODE
end
With that change, see if the code now does what you want it to —
%%Inputs:
ar=5; %aspect ratio
lamda=1; %taper ratio, (= 1 because cr = ct)
swept=45; %wing swept angle
n=4; %panle number
%%Table 7.2,s:
panelv=(1:n);
panely=panelv';
longxt=(tand(swept)*0.5+(1/ar));
panelx=(0:n-1);
%x-components
xm=((longxt/n)+(1/(2*n))*panelx+0.0375)';
x1n=((panelv-1)*(0.5/n)*(tand(swept))+((1/ar)/4))';
x2n=((panelv)*(0.5/n)*(tand(swept))+((1/ar)/4))';
%y-components-s
yms=((panely-1)*(0.5/n)+((0.5/n)/2));
y1ns=((panely-1)*(0.5/n));
y2ns=((panely)*(0.5/n));
%y-components-p
ymp=(((panely-1)*(0.5/n)+((0.5/n)/2))*-1);
y1np=((panely-1)*(0.5/n)*-1);
y2np=((panely)*(0.5/n)*-1);
%Table 7.2-s display
m72=[xm,yms,x1n,y1ns,x2n,y2ns];
%disp(m72);
%Table 7.2-p display
m72p=[xm,ymp,x1n,y1np,x2n,y2np];
%disp(m72p);
%Table 7.2 xm,ym - s
m72xmym=[xm,yms];
%disp(m72xmym);
%Table 7.2 x1,y1,x2,y2 - s
m72xiy1x2y2=[x1n,y1ns,x2n,y2ns];
%disp(m72xiy1x2y2);
%Table 7.2 x1,y1,x2,y2 - p
m72xiy1x2y2p=[x1n,y1np,x2n,y2np];
%disp(m72xiy1x2y2p);
c=[m72xiy1x2y2p;m72xiy1x2y2];
disp(c);
0.0500 0 0.1750 -0.1250 0.1750 -0.1250 0.3000 -0.2500 0.3000 -0.2500 0.4250 -0.3750 0.4250 -0.3750 0.5500 -0.5000 0.0500 0 0.1750 0.1250 0.1750 0.1250 0.3000 0.2500 0.3000 0.2500 0.4250 0.3750 0.4250 0.3750 0.5500 0.5000
%loops
i=1;
j=1;
h=1;
while j<=n
j % Check 'j' (Delete Later)
at(j)=(m72xmym(j,1));
bt(j)=(m72xmym(j,2));
a=at';
b=bt';
h = 1; % Initialise 'h'
while h<=2*n
h % Check 'h' (Delete Later)
x1c(h)=(c(h,1));
y1c(h)=(c(h,2));
x2c(h)=(c(h,3));
y2c(h)=(c(h,4));
w1(j,h)=(1/(((a(j,1))-(c(h,1)))*((b(j,1))-(c(h,4)))-((a(j,1))-...
(c(h,3)))*((b(j,1))-(b(j,1)))))*((((c(h,3))-(c(h,1)))*...
((a(j,1))-(c(h,1)))+((c(h,4))-(c(h,2)))*((b(j,1))-...
(c(h,2))))/(sqrt(((a(j,1))-(c(h,1)))^2+((b(j,1))-...
(c(h,2)))^2))-(((c(h,3))-(c(h,1)))*((a(j,1))-(c(h,3)))+...
((c(h,4))-(c(h,2)))*((b(j,1))-(c(h,4))))/(sqrt(((a(j,1))-...
(c(h,3)))^2+((b(j,1))-(c(h,4)))^2)));
h=h+1;
end
j=j+1;
end
j = 1
h = 1
h = 2
h = 3
h = 4
h = 5
h = 6
h = 7
h = 8
j = 2
h = 1
h = 2
h = 3
h = 4
h = 5
h = 6
h = 7
h = 8
j = 3
h = 1
h = 2
h = 3
h = 4
h = 5
h = 6
h = 7
h = 8
j = 4
h = 1
h = 2
h = 3
h = 4
h = 5
h = 6
h = 7
h = 8
disp(w1);
5.5747 4.7800 -0.3390 -0.0372 -20.1270 -17.5302 0.2751 0.0179 0.9979 0.8529 1.3682 -0.2263 0.6369 -20.1270 -17.5302 0.2751 0.3323 0.3066 0.2919 0.6452 0.0276 0.6369 -20.1270 -17.5302 0.1486 0.1418 0.1325 0.1434 0.0044 0.0276 0.6369 -20.1270
.
  2 commentaires
Tomas White
Tomas White le 22 Mai 2022
Thank you! that worked
Star Strider
Star Strider le 22 Mai 2022
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

Voss
Voss le 22 Mai 2022
Modifié(e) : Voss le 22 Mai 2022
You've got to re-initialize h to 1 before each h loop; otherwise you just get h=9 after the first time the h loop completes.
As it is now:
n = 4;
j = 1;
h = 1;
while j <= n
disp(sprintf('j = %d',j))
while h <= 2*n
disp(sprintf('h = %d',h))
h = h+1;
end
j = j+1;
end
j = 1
h = 1 h = 2 h = 3 h = 4 h = 5 h = 6 h = 7 h = 8
j = 2 j = 3 j = 4
With h = 1 in the right place:
n = 4;
j = 1;
while j <= n
h = 1;
disp(sprintf('j = %d',j))
while h <= 2*n
disp(sprintf('h = %d',h))
h = h+1;
end
j = j+1;
end
j = 1
h = 1 h = 2 h = 3 h = 4 h = 5 h = 6 h = 7 h = 8
j = 2
h = 1 h = 2 h = 3 h = 4 h = 5 h = 6 h = 7 h = 8
j = 3
h = 1 h = 2 h = 3 h = 4 h = 5 h = 6 h = 7 h = 8
j = 4
h = 1 h = 2 h = 3 h = 4 h = 5 h = 6 h = 7 h = 8
Also, both of those while loops can be for loops.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by