[R] Can't get function to run iteratively

Uzuner, Tolga tolga.uzuner at csfb.com
Fri Apr 8 12:08:10 CEST 2005


Trying to do something very simple...

> numerical.grad
function(func,x, eps=1e-12) {
#  very simple (crude) numerical approximation
  f <-func(x)
  df <-1:length(x)
  for (i in 1:length(x)) {
    dx <- x
    dx[i] <- dx[i] +eps 
    df[i] <- (func(dx)-f)/eps
   }
df
}
> test<-function(x){x^2}
> y<-seq(0.1,0.5,0.1)
> numerical.grad(test,y)
[1] 0.1999997 0.0000000 0.0000000 0.0000000 0.0000000
Warning messages: 
1: number of items to replace is not a multiple of replacement length 
2: number of items to replace is not a multiple of replacement length 
3: number of items to replace is not a multiple of replacement length 
4: number of items to replace is not a multiple of replacement length 
5: number of items to replace is not a multiple of replacement length 
> 


Any thoughts why this doesn't seem to be working ?


==============================================================================
This message is for the sole use of the intended recipient. If you received
this message in error please delete it and notify us. If this message was
misdirected, CSFB does not waive any confidentiality or privilege. CSFB
retains and monitors electronic communications sent through its network.
Instructions transmitted over this system are not binding on CSFB until they
are confirmed by us. Message transmission is not guaranteed to be secure.




More information about the R-help mailing list