My while loop doesn't seem to end, though the expression is satisfied.

조회 수: 5 (최근 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

채택된 답변

Guillaume
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
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?
Kiran Prasad
Kiran Prasad 2016년 6월 23일
편집: Kiran Prasad 2016년 6월 24일
I thought about doing that. While that works in this case, for another function I'm writing I'll need to use a while loop (since the number of iterations will be contingent on a condition). Thanks so much for trying to help!
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개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by