0

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

Sunhwa
  • 125
  • 1
  • 7
  • 1
    Methods in functions like `fminsearch` or `anms` are not appropriate for one-dimensional minimization (same for `optim` in Base R). For univariate functions use `optimize` in Base R or, e.g., `fminbnd` in *pracma*. A warning will be added to `fminsearch` to clearify this for the user. – Hans W. Jan 29 '18 at 04:15
  • 1
    why not have an error (e.g. a `stopifnot`) instead of a `warning` if it's anyways gonna crash further down the code – RolandASc Jan 29 '18 at 09:58

1 Answers1

1

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.

RolandASc
  • 3,863
  • 1
  • 11
  • 30