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!