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

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

 Accepted Answer

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 Comments

Thanks Guillaume, I tried this and unfortunately the program still ran forever. I went into debugging and am at a loss as to why the loop didn't stop after the 6th iteration.
Here's the CO2percentout1 for 19 iterations
0.400000000000000 0.482725933546219 0.575027461001154 0.307652884230573 0.309015908448280 0.309031452997298 0.309031263396262 0.309031263062314 0.309031263079593 0.309031263079521 0.309031263079521 0.309031263079521 0.309031263079521 0.309031263079521 0.309031263079521 0.309031263079521 0.309031263079521 0.309031263079521 0.309031263079521
Also, the value of the expression is 0 as is shown by the following code so I guess my while loop isn't working or something :/ Eventually my preallocation of CO2percentout1 ran out of the 1000 allocated slots
From debugging
K>> abs(CO2percentout1(i) - CO2percentout1(i-1)) > 0.001
ans =
0
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?
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

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 23 Jun 2016

Edited:

on 24 Jun 2016

Community Treasure Hunt

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

Start Hunting!