Search code examples
roptimizationminimizationfrequency-distribution

How can I optimize a two variable function


I have been trying to optimize the following function, but without success:

parametros <- data.frame(ap=c(11.1, 7.07, 6.3, 4.75, 4, 3.35), 
                         fx=c(41.2012, 39.3732, 25.2912, 10.3455, 1.2253, 0.4017))
xm <- 11.2

fxcalc <- function(s, t) {(1 - (1 - (parametros$ap/xm)^s)^t)*100}
suma <- function(s, t) {(parametros$fx - fxcalc(s, t))^2}

func <- function(s, t) {sum(suma(s, t))}

Being func() the function I am trying to minimize for s and t.

Apparently the function "optim()" wouldn't work with more than one variable.


Solution

  • optim works for several variables, but the function you want to optimize must take a vector as parameter, not a pair of numbers:

    func <- function(st){
      s <- st[1]
      t <- st[2]
      sum(suma(s,t))
    }
    
    optim(c(0,0), func) # 0 and 0 initial values of s and t