Skip to content

dynamics of functions

2 messages · Hotz, T., Barry Rowlingson

#
Dear Tobias,
What about the following function?

iterate<-function(f,n,x){
  if(n==0) return(x)
  y<-x
  for(i in 1:n)y<-f(y)
  y
}
iterate(sqrt,3,256)

Hope that helps.

Best wishes

Thomas


---

Thomas Hotz
Research Associate in Medical Statistics
University of Leicester
United Kingdom

Department of Epidemiology and Public Health
22-28 Princess Road West
Leicester
LE1 6TP
Tel +44 116 252-5410
Fax +44 116 252-5423

Division of Medicine for the Elderly
Department of Medicine
The Glenfield Hospital
Leicester
LE3 9QP
Tel +44 116 256-3643
Fax +44 116 232-2976
#
Hotz, T. wrote:

            
Or the following:

iterFun <-
   function(f,n,x,...){
     if(n==0){return(x)}
     for(i in 1:n){
       x<-f(x,...)
     }
     return(x)
   }

  If you want to construct an arbitrary function without naming it, you 
can do something like this:

 > iterFun(function(z){3.9*z*(1-z)},121,.6)
[1] 0.6449397

  And the ... part means you can try different constants in your function:

 > iterFun(function(z,a){a*z*(1-z)},121,.6,a=3.9)
[1] 0.6449397
 > iterFun(function(z,a){a*z*(1-z)},121,.6,a=3.901)
[1] 0.09416016
 > iterFun(function(z,a){a*z*(1-z)},121,.6,a=3.900001)
[1] 0.7612195

  Fun with functions.

Baz