[R] Help with Iterator

zhiji19 zhiji19 at gmail.com
Tue Nov 9 09:01:30 CET 2010


Dear Experts,

The following is my "Iterator". When I try to write a new function with
itel, I got error. 

This is what I have: 
> supDist<-function(x,y) return(max(abs(x-y)))
> 
> myIterator <- function(xinit,f,data=NULL,eps=1e-6,itmax=5,verbose=FALSE) {
+ xold<-xinit
+ itel<-0
+ repeat {
+   xnew<-f(xold,data)
+   if (verbose) {
+     cat(
+     "Iteration: ",formatC(itel,width=3, format="d"),
+     "xold: ",formatC(xold,digits=8,width=12,format="f"),
+     "xnew: ",formatC(xnew,digits=8,width=12,format="f"),
+     "\n"
+     )
+     }  
+     if ((supDist(xold,xnew) < eps) || (itel == itmax)) {
+       return(xnew)
+     }
+   xold<-xnew; itel<-itel+1
+   }
+ }

> mat<-function (x, data=NULL) {return (1+x^itel)}
> myIterator(3, f=mat, verbose=TRUE)
Error in f(xold, data) : object 'itel' not found


Can anyone please help me to fix the error?
-- 
View this message in context: http://r.789695.n4.nabble.com/Help-with-Iterator-tp3033254p3033254.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list