Hello everyone, I am trying to use while loop to iterate a function until convergence. But I am having problem when I try to use a fixed number of iterations. Say I want to use maximum iteration of 150. If the value don't converge within maximum iteration, show warning of no convergence. Currently I don't have non- convergence problem so I think my code works fine. But in future I may encounter such problem that are likely to not converge easily. Below is my function that is working when I don't provide maximum iteration. iter<- function (Fpi, Time, tolerance){ S = 22.4 Ts = 0.499 Ti = 0.25 K = 0.044 r<- 1.5 M = Ts- Ti Ks = 0.044 Fpt = K*Time + M*S*log(1+ Fpi/(M*S)) while((Fpt-Fpi) > tolerance) { Fpi = Fpt Fpt = K*Time + M*S*log(1+ Fpi/(M*S)) Fp0 = Fpt } return(Fpt) } x<- iter(Fpi = 0.224, Time = 0.2, tolerance = 0.000001) But I want do something like this ( conceptually) for( i in 2:itermax) { Fpt[i] = K*Time + M*S*log(1+ Fpi/(M*S)) if((Fpt[i]- Fpt[i-1])<= tolerance) break print(Fpt[i] } something like this. any kind of help is highly appreciated. thank you -- Acharya, Subodh [[alternative HTML version deleted]]
Code inline: On Fri, Jun 4, 2010 at 1:30 PM, Subodh Acharya <shoebodh at gmail.com> wrote:> Hello everyone, > > I am trying to use while loop to iterate a function until convergence. But I > am having problem when I try to use a fixed number of iterations. > Say I want to use maximum iteration of 150. If the value don't converge > within maximum iteration, show warning of no convergence. > > Currently I don't have non- convergence problem so I think my code works > fine. But in future I may encounter such problem that are likely to not > converge easily. > > Below is my function that is working when I don't provide maximum iteration. > > iter<- function (Fpi, Time, tolerance){ > S = 22.4 > Ts = 0.499 > Ti = 0.25 > K = 0.044 > r<- 1.5 > M = Ts- Ti > Ks = 0.044 > ? ? Fpt = K*Time + M*S*log(1+ Fpi/(M*S)) > ? ? ? while((Fpt-Fpi) > tolerance) { > ? ? ? ? Fpi = Fpt > ? ? ? ? ? ? Fpt = K*Time + M*S*log(1+ Fpi/(M*S)) > ? ? Fp0 = Fpt > ? ?} > ?return(Fpt) > } > x<- iter(Fpi = 0.224, Time = 0.2, tolerance = 0.000001)Add a counter variable and a second check in your `while` criteria: ... ... iter <- 0 while (((Fpt - Fpi) > tolerance) && (iter < itermax)) { Fpi = Fpt Fpt = K*Time + M*S*log(1+ Fpi/(M*S)) Fp0 = Fpt iter <- iter + 1 } -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
Hi Subodh, Minor note: please keep replies on list so everyone benefits from answers/questions. Now: On Fri, Jun 4, 2010 at 2:49 PM, Subodh Acharya <shoebodh at gmail.com> wrote:> Thanks a lot Steve, > It worked. I appreciate. But I have another question, may be thats trivial. > Say, it doesn't converge at itermax. I need to display error message saying > "values don't converge at at itermax". Is there another statement that > accompanies while for this?Perhaps you can use "warning(...)". You can check whether (or not) your Fpt - Fpi is > tolerance after you loop. If it is, you know your loop terminated because you hit itermax, and not because your algo converged: ... ... iter <- 0 while (((Fpt - Fpi) > tolerance) && (iter < itermax)) { ?Fpi = Fpt ?Fpt = K*Time + M*S*log(1+ Fpi/(M*S)) ?Fp0 = Fpt ?iter <- iter + 1 } if (Fpt - Fpi > tolerance) { warning("Algorithm reached itermax and did not converge") } ... -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact