Dear list, I would like to study the dynamics of functions using R (instead of mathematica e.g.), i.e. the behavior of points under iteration of a function. So I tried (in vain) writing a function myfunction <- function(f,n,x){...} in order to compute f^{n}(x), f^{n}(x) being the function f composed with itself n-1 times. n is a natural number, and the argument x is the abscissa of the point I would like to `follow'. Can someone give me a boost? I appreciate your help, Tobias Verbeke
Dear list, I would like to study the dynamics of functions using R (instead of mathematica e.g.), i.e. the behavior of points under iteration of a function. So I tried (in vain) writing a function myfunction <- function(f,n,x){...} in order to compute f^{n}(x), f^{n}(x) being the function f composed with itself n-1 times. n is a natural number, and the argument x is the abscissa of the point I would like to `follow'. Can someone give me a boost? I appreciate your help, Tobias Verbeke
Dear Tobias,> I would like to study the dynamics of > functions using R (instead of mathematica e.g.), > i.e. the behavior of points under iteration > of a function. > So I tried (in vain) writing a function > myfunction <- function(f,n,x){...} > in order to compute f^{n}(x), f^{n}(x) being > the function f composed with itself n-1 times. > n is a natural number, and the argument x is > the abscissa of the point I would like to > `follow'.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:> 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
Dear Thomas,> 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)Thank you very much, this certainly helps. I'm still curious, though, to know how to write the expression of my function immediately as the argument f. I can define it outside of the function> aap <- function(x) -x^3and use it as argument> iterate(aap,3,256),but I seem not to be clever enough to write a function that receives the following as input> iterate(-x^3,3,256)Thanks again, Tobias
Here is a recursive function. iterate <- function(f, n, x){ if(n==0) return(x) iterate(f, n-1, f(x)) } iterate(function(x) 1/(1+x), 10, 1) Best, Ravi. ----- Original Message ----- From: Tobias Verbeke <tobias_verbeke at skynet.be> Date: Thursday, June 5, 2003 12:04 pm Subject: Re: [R] dynamics of functions> Dear Thomas, > > > 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) > > Thank you very much, this certainly helps. > I'm still curious, though, to know how to > write the expression of my function > immediately as the argument f. > I can define it outside of the function > > aap <- function(x) -x^3 > and use it as argument > > iterate(aap,3,256), > but I seem not to be clever enough > to write a function that receives > the following as input > > > iterate(-x^3,3,256) > > Thanks again, > > Tobias > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >