While loop isn't terminating even though the expression is 0

2 visualizaciones (últimos 30 días)
Kiran Prasad
Kiran Prasad el 24 de Jun. de 2016
Editada: Moe_2015 el 24 de Jun. de 2016
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

Respuesta aceptada

Moe_2015
Moe_2015 el 24 de Jun. de 2016
Editada: Moe_2015 el 24 de Jun. de 2016
I think what you need to do is the following (by the way, I don't know what your code is doing but I think it should be > 0.001 not <):
i = 1;
CO2percentout1(i) = 0; %or whatever starting value here
difference = 1; %just an arbitrary value above 0.001
while difference > 0.001
i = i + 1;
[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);
difference = abs(CO2percentout1(i) - CO2percentout1(i-1));
end
  3 comentarios
Kiran Prasad
Kiran Prasad el 24 de Jun. de 2016
This worked perfectly! Thank you!!! Could you explain why what I did didn't work?
Moe_2015
Moe_2015 el 24 de Jun. de 2016
Editada: Moe_2015 el 24 de Jun. de 2016
You're welcome! The problem was that you were putting i=i+1 as the last line in the while loop when it should have been first. Let's say we were going through the loop the first time (so i = 2). If you have i = i + 1 at the top of the loop, when you're computing CO2percentout1(i) it will be CO2percentout1(3). However, if i = i + 1 is at the bottom of the loop, you already computed CO2percentout1(i) before that line so it will be CO2percentout1(2). So when it goes back to the inequality you have CO2percentout1(3) - CO2percentou1(2) but you haven't computed CO2percentout1(3) yet.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 24 de Jun. de 2016
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 "<"
  1 comentario
Kiran Prasad
Kiran Prasad el 24 de Jun. de 2016
Thanks for your reply! I added the full pertinent section of code and would note that the other variables such as V1,V2, and V3 are user inputs. Also both of you were right about the > that was a typo when I was editing the post. Thanks!

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by