My while loop doesn't seem to end, though the expression is satisfied.
7 次查看(过去 30 天)
显示 更早的评论
Hi, I'm coding an iterative model and am having problems with the following while loop. I'm trying to get the loop to stop running once CO2percentout1(i)=CO2percentout(i-1) within a tolerance. I've defined the CO2percentout1 to be from 1:1000.
- I've defined i to be 2 initially.
- From manually inspecting the code the CO2percentout1 seems to converge within 15 iterations to at least 4 decimal place accuracy but still the code doesn't finish executing.
- All variables except the ones that are inputs for the other functions(i.e. CO2percentout2,Algae_in_2) have been inputted and those that are iteratively determined have been given initial guesses outside the loop.
Any help is much appreciated, Kiran
function [Algae_Product,c] = Threestage(tot_gas_flow_LPM,CO2_percent_feed,Water_flow,C01,C02,C03,V1_L,V2,V3,H1,H2,H3)
V1=V1_L;
Height1=H1;
Height2 = H2;
Height3= H3;
tot_gas_flow=tot_gas_flow_LPM;
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
%for i = 1:50
[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
c=[c1 ;c2 ;c3];
end
0 个评论
采纳的回答
Guillaume
2016-6-23
ismembertol is not really designed to compare just two numbers, it's more to find if a number is within the range of several numbers. You're using a hammer to crack a nut.
I suspect the problem is that (as per the doc) the tolerance used is not 0.001 but much greater. If your C02percentout is 1000, the actual tolerance is 10. That is because the tolerance is scaled by the magnitude of the numbers.
To stop that:
while ~ismembertol(CO2percentout1(i), CO2percentout1(i-1), .001, 'DataScale', 1)
But better (and probably faster):
while abs(CO2percentout1(i) - CO2percentout1(i-1)) > 0.001
4 个评论
Guillaume
2016-6-23
Well, you can always convert the loop into a for loop to ensure it does not iterate more than a 1000 times.
for iter = 2:1000
[CO2percentout1(iter), 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);
if abs(CO2percentout1(iter) - CO2percentout1(iter-1)) > 0.001
break;
end
end
But that shouldn't make any difference if the condition is fulfilled before the 1000th iteration.
There must be something special going on with your code. Perhaps you can attach it?
更多回答(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!