Problem with cycle within a cycle output
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Tomas White
el 22 de Mayo de 2022
Comentada: Star Strider
el 22 de Mayo de 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);
0 comentarios
Respuesta aceptada
Star Strider
el 22 de Mayo de 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);
%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
disp(w1);
.
2 comentarios
Más respuestas (1)
Voss
el 22 de Mayo de 2022
Editada: Voss
el 22 de Mayo de 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
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
Also, both of those while loops can be for loops.
0 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!