R/grcentral.R

Defines functions grcentral

Documented in grcentral

grcentral <- function(par, userfn, fbase=NULL, env=optsp, ...) {
   # Central difference gradient approximation
   if (is.null(fbase)) fbase <- userfn(par, ...)  # ensure we function value at par
   eps <- env$deps
   df <- rep(NA, length(par))
   teps <- 0.5 * eps * (abs(par) + eps)
   for (i in 1:length(par)) {
      dp <- par
      dp[i]<-dp[i]+teps[i]
      dm <- par
      dm[i]<-dm[i]-teps[i]
      df[i] <- 0.5*(userfn(dp, ...) - userfn(dm,...))/teps[i]
   }
   df
}
 

Try the optextras package in your browser

Any scripts or data that you put into this service are public.

optextras documentation built on Dec. 31, 2019, 3:01 a.m.