https://github.com/cran/pracma
Tip revision: f0ba8a5d88ce30d08c8312fd9b882f2ff051d91b authored by Hans W. Borchers on 25 August 2018, 21:00:11 UTC
version 2.1.5
version 2.1.5
Tip revision: f0ba8a5
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.")
else if (length(x0) == 1)
stop("Don't use 'fminsearch' for one-dimensional minimization.")
scl <- if(minimize) 1 else -1
fun <- match.fun(f)
f <- function(x) scl * fun(x, ...)
if (dfree) {
opt <- anms(f, x0, tol = tol, maxfeval = 10*maxiter)
} else {
opt <- cgmin(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))
}