How can I optimize a two variable function in R

与世无争的帅哥 提交于 2020-05-29 11:44:05

问题


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.

Thank you very much!


回答1:


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


来源:https://stackoverflow.com/questions/49436722/how-can-i-optimize-a-two-variable-function-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!