https://github.com/cran/pracma
Tip revision: c79a04b5074656b36e591191eb8137b70a349932 authored by Hans W. Borchers on 30 June 2014, 00:00:00 UTC
version 1.7.0
version 1.7.0
Tip revision: c79a04b
fminsearch.R
##
## f m i n s e a r c h . R
##
fminsearch <- function(f, x0, ..., minimize = TRUE, dfree = TRUE,
maxiter = 1000, tol = .Machine$double.eps^(2/3)) {
if (!is.numeric(x0))
stop("Argument 'x0' must be a numeric vector.")
scl <- if(minimize) 1 else -1
fun <- match.fun(f)
f <- function(x) scl * fun(x, ...)
if (dfree) {
opt <- nelder_mead(x0, f, maxfeval = 5*maxiter, tol = tol)
} else {
opt <- fletcher_powell(x0, f, maxiter = maxiter, tol = tol)
}
xopt <- opt$xmin; fopt <- opt$fmin
if (! minimize) fopt <- -fopt
return(list(xval = xopt, fval = fopt, niter = opt$niter))
}