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 ______________________________________________ R-help@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.