[R] Can't get function to run iteratively

From: Uzuner, Tolga <tolga.uzuner_at_csfb.com>
Date: Fri 08 Apr 2005 - 20:08:10 EST


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.


R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Apr 08 20:16:18 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:05 EST