我如何优化R中的两个变量函数 [英] How can I optimize a two variable function in R

查看:237
本文介绍了我如何优化R中的两个变量函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试优化以下功能,但没有成功:

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))}

正在尝试将"s"和"t"的功能最小化为"func()".

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

显然,函数"optim()"不适用于多个变量.

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

非常感谢您!

推荐答案

optim适用于多个变量,但是要优化的函数必须将向量作为参数,而不是一对数字:

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

这篇关于我如何优化R中的两个变量函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆