Hi,
I'm new to R and have a problem with a little test program (see below). Why doesn't <<- in function rk4
The reason is lexical scoping (see FAQ), and I suggest to implement rk4 as function with a return value (see # !!!). BTW there are already a rk4 (and lsoda) functions in the odesolve package.
Thomas P.
Your example formally corrected:
rk4 <- function(x,y,f,h) { n <- length(y) hh <- h*0.5 k1 <- f(x,y) k2 <- f(x,y+k1*hh) k3 <- f(x,y+k2*hh) k4 <- f(x,y+k3*h) y + h/6.0*(k1 + 2.0*k2 + 2.0*k3 + k4) # !!! }
rkf <- function(x,y) { -y }
rktest <- function(){ y <- 1.0 x <- 0.0 h <- 0.1 for(i in 1:10) { y <- rk4(x,y,rkf,h) # !!! print(y) } }
rktest()
______________________________________________ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html