hie.. how can i write a loop that makes algorithm keeps repeating until a solution is converged?do i use a for loop? i know that we can use for loop to ask for a number of repetitions, but how to use it to ask the algorithm to keep repeating until a solution is converged? Thanks -- View this message in context: http://www.nabble.com/convergence-tf3606834.html#a10076822 Sent from the R help mailing list archive at Nabble.com.
No, use a while loop. Something like change <- 1 while(abs(change) > .001 ){ do stuff change <- updated change }> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of rach.s > Sent: Thursday, April 19, 2007 8:00 AM > To: r-help at stat.math.ethz.ch > Subject: [R] convergence > > > hie.. > how can i write a loop that makes algorithm keeps repeating > until a solution is converged?do i use a for loop? i know > that we can use for loop to ask for a number of repetitions, > but how to use it to ask the algorithm to keep repeating > until a solution is converged? > Thanks > -- > View this message in context: > http://www.nabble.com/convergence-tf3606834.html#a10076822 > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Hi,> how can i write a loop that makes algorithm keeps repeating until a solution > is converged?do i use a for loop? i know that we can use for loop to ask for > a number of repetitions, but how to use it to ask the algorithm to keep > repeating until a solution is converged?I don't know what your problem is, and I am far from being a specialist, but it seems to me that in this case you often use a variable, for example 'epsilon', which measures your "level of convergence". Then you could you use a loop which repeats while this measure is above a certain threshold, for example : while (epsilon > 0.0001) { ... repeated expressions ... } Hope that helps, -- Julien
On 19-Apr-07 12:00:17, rach.s wrote:> > hie.. > how can i write a loop that makes algorithm keeps repeating > until a solution is converged?do i use a for loop? i know > that we can use for loop to ask for a number of repetitions, > but how to use it to ask the algorithm to keep repeating > until a solution is converged? > ThanksThere are various ways round this, but a 'for' loop with a fixed number of iterations is not usully one of them! The simplest is to use while(). A possibly strategy is Y.old <- initial.Y while(TRUE){ Y <- compute.Y(Y.old, ...) if(abs(Y - Y.old) < small.number) break Y.old <- Y } This will loop indefinitely until the convergence criterion abs(Y - Y.old) < small.number is met, and then stop. Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 19-Apr-07 Time: 14:01:51 ------------------------------ XFMail ------------------------------
rach.s wrote:> hie.. > how can i write a loop that makes algorithm keeps repeating until a solution > is converged?do i use a for loop? i know that we can use for loop to ask for > a number of repetitions, but how to use it to ask the algorithm to keep > repeating until a solution is converged?gregory.liebniz<-function(tol=0.0001) { pi.diff<-1 iter<-0 numer<-1 last.pi<-0 pi4<-0 while(pi.diff > tol) { pi4<-pi4+numer/(2*iter+1) this.pi<-pi4*4 pi.diff<-abs(this.pi-last.pi) last.pi<-this.pi iter<-iter+1 numer<- -numer } return(this.pi) } What you want is probably a while loop, testing for a level of convergence like this simple method for calculating pi. Jim
Maybe Matching Threads
- [PATCH 1/1] O2net: Disallow o2net accept connection request from itself.
- What is a better way to deal with lag/difference and loops in time series using R?
- [Bridge] [PATCH net-next 06/16] net: bridge: Add a tracepoint for MDB overflows
- cronbach's alpha
- smbd fails to start after upgrade to version 4.11.6