## Rosenbrock function
rosenbrock <- function(x) {
n <- length(x)
x1 <- x[2:n]
x2 <- x[1:(n-1)]
sum(100*(x1-x2^2)^2 + (1-x2)^2)
}
nelder_mead(c(0,0,0,0), rosenbrock)
# $xmin
# [1] 1 1 1 1
# $fmin
# [1] 8.802801e-25
# $nfeval
# [1] 678
# $restarts
# [1] 0
nelder_mead(c(0,0,0,0), rosenbrock, rep(-0.5, 4), rep(0.5, 4))
# $xmin
# [1] 0.50000000 0.26221321 0.07797600 0.00608026
# $fmin
# [1] 1.667875
# $nfeval
# [1] 1501
# $restarts
# [1] 3
Run the code above in your browser using DataLab