2

I need to minimize the objective function shown below, the variables in green boxes will be introduced to the formula and the variables in red boxes needed to be optimized and there will be a starting value for each. as of yet the optimization is not constrained. I put the formula here not to wait for a code but for the responses to have an idea about the function. What I've done so far: I searched the threads, I've tried the nlm command on a toy function:

fn =function(x,a) {sum(100*a+(2*x^2+5*x-7))}
nlm(fn , a<-c(10),x<- c(100), hessian=TRUE)

but I couldn't get the value for the optimum (a) and I doubt that I've some errors in the formula, I am using this formula as a starting point to tackle the formula below. What I am looking for is can any one point me to the suitable function in R that I will start from.

The Obj. Function

4

2 回答 2

2

通常在参数列表中调用带有赋值运算符的 R 函数会产生失败。这是一个<-=. 我不会认为这会起作用:

 nlm(fn , a =c(10), x = c(100), hessian=TRUE)  # and it didn't

错误消息提供信息,告诉您缺少参数 p:

> fn =function(x,p) {sum(100*p[1]+(2*x^2+5*x-7))}
> nlm(fn , p=c(10),x = c(100), hessian=TRUE)
$minimum
[1] -4988507

$estimate
[1] -50090

$gradient
[1] 100

$hessian
     [,1]
[1,]    0

$code
[1] 5

$iterations
[1] 6
于 2013-07-24T22:18:18.233 回答
1

我实际上建议不要将不需要优化的额外参数放入您的函数中。如果参数名称复杂,则更容易搞砸。

我会像这样扭曲你的功能:

fn = function(x,a) {sum(100*a+(2*x^2+5*x-7))}
f = function(x) {fn(x, 100)}
nlm(f, 10, hessian=TRUE)
于 2016-01-27T23:07:11.787 回答