Stephen
Stephen

Reputation: 305

Optimization of a function of 2 parameters

My problem is probably common, but I don't know how to adapt the answer to this problem

I have a function F with two parameters (a,b) and I have to find the maximum of this function.

For now, I do that :

optimization_of_F<-function(a,b){
  solution=c(0,0,0) #initialization 
  a=seq(0, 5, by=0.1)
  b=seq(0.1, 5, by =0.1)

  for (d in a){ 
    for (k  in b){ 
      if  ( F(d, k) > solution[1] )  {
        solution[1]= F(d,k)
        solution[2]= d
        solution[3]= k
      }  
    }     
  }
  return(solution)
}

But this way is too long and I'd like to optimize it. How can I do that ?

Upvotes: 4

Views: 14574

Answers (2)

baptiste
baptiste

Reputation: 77096

Try this, where F is your function, c(0,0) an initial guess, c=2 and d=pi/3 are fixed parameters fed to F,

optim(c(0,0), F, c=2, d=pi/3)

Upvotes: 7

Se&#241;or O
Se&#241;or O

Reputation: 17412

I feel like this will get you closer to what you are looking for:

a=seq(0, 5, by=0.1)
b=seq(0.1, 5, by =0.1)
outer(a, b, FUN=F)

Upvotes: 2

Related Questions