When does loop end?
显示 更早的评论
Hi, sorry for making it a little long but I wanted to ask a clear specific question.
I'm working on this flow dynamics problem and I'm getting different results using my approach than the expected results.
I spent the last few days trouble shooting the problem and I believe the issues is coming from a For Loop nested with IF statements.
Here's a sketch from my 2-D grid where I'm writing code to try to determine boundary conditions for flow between the grids.

My issues has been with the diagonal cells, namely: A,C,G & I.
The IF statements share some conditions with the B,D,F& H as you can see in my code below
Here's my code:
Imax = 15;
Jmax = 15;
for i=1:Imax*Jmax
if i<=Imax || mod(i,Imax)==1 % No-flow boundary conditions
lamdaWA(i)=0;
lamdaTA(i)=0;
else
if P(i,t)>=P(i-Imax,t)
lamdaWA(i)=lamdaW(i,t);
lamdaTA(i)=lamdaT(i,t);
else
lamdaWA(i)=lamdaW(i-Imax-1,t);
lamdaTA(i)=lamdaT(i-Imax-1,t);
end
end
if i<=Imax
lamdaWB(i)=0;
lamdaTB(i)=0;
else
if P(i,t)>=P(i-Imax,t)
lamdaWB(i)=lamdaW(i,t);
lamdaTB(i)=lamdaT(i,t);
else
lamdaWB(i)=lamdaW(i-Imax,t);
lamdaTB(i)=lamdaT(i-Imax,t);
end
end
if i<=Imax || mod(i,Imax)==0
lamdaWC(i)=0;
lamdaTC(i)=0;
else
if P(i,t)>=P(i-Imax,t)
lamdaWC(i)=lamdaW(i,t);
lamdaTC(i)=lamdaT(i,t);
else
lamdaWC(i)=lamdaW(i-Imax+1,t);
lamdaTC(i)=lamdaT(i-Imax+1,t);
end
end
if mod(i,Imax)==1
lamdaWD(i)=0;
lamdaTD(i)=0;
else
if P(i,t)>=P(i-1,t)
lamdaWD(i)=lamdaW(i,t);
lamdaTD(i)=lamdaT(i,t);
else
lamdaWD(i)=lamdaW(i-1,t);
lamdaTD(i)=lamdaT(i-1,t);
end
end
if mod(i,Imax)==0
lamdaWF(i)=0;
lamdaTF(i)=0;
else
if P(i,t)>=P(i+1,t)
lamdaWF(i)=lamdaW(i,t);
lamdaTF(i)=lamdaT(i,t);
else
lamdaWF(i)=lamdaW(i+1,t);
lamdaTF(i)=lamdaT(i+1,t);
end
end
if i>(Imax*Jmax)-Imax || mod(i,Imax)==1
lamdaWG(i)=0;
lamdaTG(i)=0;
else
if P(i,t)>=P(i+Imax,t)
lamdaWG(i)=lamdaW(i,t);
lamdaTG(i)=lamdaT(i,t);
else
lamdaWG(i)=lamdaW(i+Imax-1,t);
lamdaTG(i)=lamdaT(i+Imax-1,t);
end
end
if i>(Imax*Jmax)-Imax
lamdaWH(i)=0;
lamdaTH(i)=0;
else
if P(i,t)>=P(i+Imax,t)
lamdaWH(i)=lamdaW(i,t);
lamdaTH(i)=lamdaT(i,t);
else
lamdaWH(i)=lamdaW(i+Imax,t);
lamdaTH(i)=lamdaT(i+Imax,t);
end
end
if i>(Imax*Jmax)-Imax || mod(i,Imax)==0
lamdaWI(i)=0;
lamdaTI(i)=0;
else
if P(i,t)>=P(i+Imax,t)
lamdaWI(i)=lamdaW(i,t);
lamdaTI(i)=lamdaT(i,t);
else
lamdaWI(i)=lamdaW(i+Imax+1,t);
lamdaTI(i)=lamdaT(i+Imax+1,t);
end
end
end
My specific question is: When does the For loop jump to the next item? That means from i=1 to 1=2?
For example, when i = 5, does the code read the first 3 IF statements for A,B,C or does it just read the first one, jump to the end of the FOR loop and repeat for i=6?
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 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!