优化两个参数的函数 [英] 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
我有一个带有两个参数(a,b)的函数F,我必须找到该函数的最大值.
I have a function F with two parameters (a,b) and I have to find the maximum of this function.
现在,我这样做:
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 ?
推荐答案
尝试一下,其中F
是您的函数,c(0,0)
是初始猜测,c=2
和d=pi/3
是馈给F
的固定参数,
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)
这篇关于优化两个参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文