Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimization of a function of 2 parameters

Tags:

optimization

r

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 ?

like image 383
Stephen Avatar asked Jan 13 '23 22:01

Stephen


1 Answers

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)
like image 150
baptiste Avatar answered Jan 19 '23 12:01

baptiste