While loop isn't terminating even though the expression is 0
이전 댓글 표시
Hi, I'm utterly perplexed as to why the following while loop will not terminate. Any help would be appreciated.
CO2percentout1 = 1:1000;
CO2percentout1(:)=0;
CO2percentout1(1) = CO2_percent_feed-.6*CO2_percent_feed;
CO2percentout2 = CO2_percent_feed-.5*CO2_percent_feed;
CO2percentout3 = CO2_percent_feed-.3*CO2_percent_feed;
i=2;
c1 = 1:49;
c2 = 1:49;
c3 = 1:49;
while abs(CO2percentout1(i) - CO2percentout1(i-1)) > 0.001
[CO2percentout1(i),Algae_in_2,c1] = stage1(CO2percentout2,V1,tot_gas_flow,Height1,C01,Water_flow);
[CO2percentout2,Algae_in_3,c2]=stage2(CO2percentout3,V2,tot_gas_flow, Height2, C02,Water_flow,Algae_in_2);
[CO2percentout3,Algae_Product,c3] =stage3(CO2_percent_feed,V3,tot_gas_flow,Height3,C03,Water_flow,Algae_in_3);
i=i+1;
end;
the values of CO2percentout1 for 30 iterations are the following:
0.400000000000000 0.496842690178588 0.577390642895070 0.658829848072415 0.658142131647976 0.658142519746066 0.658142563142344 0.658142562747119 0.658142562747534 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550 0.658142562747550
채택된 답변
추가 답변 (1개)
Walter Roberson
2016년 6월 24일
1 개 추천
What is the initial value of i, and what is CO2percentout1(i-1) for that initial i ?
With the values you post, I would expect the loop to never get started if i starts out less than about 4, but if it starts out greater than that then because the adjacent values are closer than 0.001, the loop will not stop. You appear to have defined a convergent algorithm and asked it to stop iterating when it does not converge. I suspect you want ">" rather than "<"
카테고리
도움말 센터 및 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!