Message-ID: <3EDF687A.5010600@lancaster.ac.uk>
Date: 2003-06-05T15:57:46Z
From: Barry Rowlingson
Subject: dynamics of functions
In-Reply-To: <1F2CE8D4B0195E488213E8B8CCF71486015E4692@saffron.cfs.le.ac.uk>
Hotz, T. wrote:
> 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)
>
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