1

I have the following function:

I am interested in finding all the 4 local minima of this bivariate function using code in R. How can I go about it?

  • 1
    Are you looking for numerical optimization ? In that case, `optim` can be used. You will need to change the initial point several times to find your 4 local minima – linog Apr 09 '20 at 14:30
  • yes numerical optimization should be fine @linog – Lara Ann Xiberras Apr 09 '20 at 14:38
  • thank you very much, i managed with the optim. do you know of another method though where it just outputs the local minima alone without having to enter an initial point please? @linog – Lara Ann Xiberras Apr 09 '20 at 14:46
  • Numerical routines depend on a starting point. If you want to be sure about your minimas, you need to cover different points in space. In the solution I proposed, points are randomly picked in R2 – linog Apr 09 '20 at 15:03
  • If you're happy with the answer, could you accept it ? – linog Apr 09 '20 at 15:04
  • Cool, you're welcome. In that case could you accept the answer ? [see here](https://stackoverflow.com/help/someone-answers) – linog Apr 09 '20 at 15:29
  • I think I managed to, sorry newbie haha! thanks once again – Lara Ann Xiberras Apr 10 '20 at 12:03
  • That's ok you managed to do it ! See you soon for other questions (or answers!) – linog Apr 10 '20 at 12:32

1 Answers1

1

If you are interested in numerical optimization, you have several approaches possible. The most direct one is to use optim. By default, this is a Nelder-Mead simplex method but others are implemented.

You will need to start from different starting values to converge to different end points. I can propose you the following:

func <- function(a){
  x <- a[1]
  y <- a[2]
  return(
     0.5*(x^4 - 16*x^2 + 5*x + y^4 - 16*y^2 + 5*y)
  )
}

t0 <- rnorm(100, sd = 20)
t1 <- rnorm(100, sd = 20)

points <- do.call(rbind, lapply(1:100, function(i) optim(par = c(t0[i],t1[i]), fn = func)$par))

And if you want to see graphically your solutions:

library(ggplot2)
ggplot(data.frame(points)) + geom_point(aes(x = X1, y = X2))

enter image description here

You have four local minima in this output

linog
  • 5,786
  • 3
  • 14
  • 28