[R] convergence
Jim Lemon
jim at bitwrit.com.au
Thu Apr 19 15:07:06 CEST 2007
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
More information about the R-help
mailing list