rfminsearch

fminsearch on a single variable


Using R's help page example on fminsearch as a starting point:

# Rosenbrock function
rosena <- function(x, a) 100*(x[2]-x[1]^2)^2 + (a-x[1])^2  # min: (a, a^2)

fminsearch(rosena, c(-1.2, 1), a = sqrt(2))
# x = (1.414214 2.000010) , fval = 1.239435e-11

I want to evaluate something like this but with only one variable such as:

 rosena <- function(x, a) 100*(x[1]-x[1]^2)^2 + (a-x[1])^2 

but when I run

fminsearch(rosena, c(1), a = sqrt(2))

It gives the error: Error in X[2:d1, ] : incorrect number of dimensions

fminsearch seems to want a vector of length greater than or equal to 2, but no less, however for this example, the vector requires length 1

Note: fminsearch is in the "pracma" package


Solution

  • It looks like a bug in the pracma package.

    The anms function is dropping a dimension upon a subscript, relevant excerpts:

    d <- length(x0) # i.e. 1
    d1 <- d + 1 # i.e. 2
    ...
    X <- matrix(0, nrow = d1, ncol = d)
    ...
    X <- X[o, ] # could put drop = FALSE here
    

    I think you should post a bug with the author of the package.