[R] Help with Iterator

Nick Sabbe nick.sabbe at ugent.be
Tue Nov 9 09:30:54 CET 2010


I guess what you want is:
- change the line :
	xnew<-f(xold,data)
into
	xnew<-f(xold,data, itel)
- change your mat function to take itel as an extra parameter:
	mat<-function (x, data=NULL, itel) {return (1+x^itel)}

That should do the trick (though I haven't checked whether the rest of your
code is OK)


Nick Sabbe
--
ping: nick.sabbe at ugent.be
link: http://biomath.ugent.be
wink: A1.056, Coupure Links 653, 9000 Gent
ring: 09/264.59.36

-- Do Not Disapprove



-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of zhiji19
Sent: dinsdag 9 november 2010 9:02
To: r-help at r-project.org
Subject: [R] Help with Iterator


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.

______________________________________________
R-help at r-project.org 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.



More information about the R-help mailing list