Problem with cycle within a cycle output
조회 수: 10 (최근 30일)
이전 댓글 표시
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
채택된 답변
Star Strider
2022년 5월 22일
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);
.
추가 답변 (1개)
Voss
2022년 5월 22일
편집: Voss
2022년 5월 22일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!