[英]Using optim() - two equations two unknowns R
I am trying to use optim() to solve & maxim. 我正在尝试使用optim()解决和最大化。 the following equation system: 以下方程组:
model <- function(z) {
q1 <- z[1]
q2 <- z[2]
return ((10-2*(q1+q2))*q1-k1*q1)
return ((10-2*(q2+q1))*q2-k2*q2)
}
The paramters/variables are defined as follows: 参数/变量定义如下:
A <- rep(0,2)
lowerb <- rep(0,2)
upperb <- rep(6,2)
k1 <- 3
k2 <- 2
and the rest as follows: 其余的如下:
optim(A, model, gr=NULL, method="L-BFGS-B", lower = lowerb, upper = upperb, control = list(fnscale = -1, trace = 1))$par
I do not receive an error, however, the maxima are supposed to be at q1=1 and q2=1,5 and not at 1,75 and 0, as R is stating at the moment. 我没有收到错误,但是,正如R所指出的那样,最大值应该位于q1 = 1和q2 = 1,5而不是1,75和0。 Since I need to apply this to a much more complex equation system, I did use an easy equation to start with, which one can easily calculate manually. 由于我需要将其应用于更为复杂的方程式系统,因此我确实使用了一个简单的方程式作为起点,可以轻松地手动进行计算。
Help is appreciated! 感谢帮助! Thanks a lot! 非常感谢!
k1 <- 3
k2 <- 2
q2 <- 0
q2 <- 0
for (i in 1:20) {
#For 1
objective1 <- function(q) ((10-2*(q+q2))*q-k1*q)
result1 <- optimize(objective1, c(0,10), maximum = TRUE)
q1 <- result1$maximum
#For 2
objective2 <- function(q) ((10-2*(q1+q))*q-k2*q)
result2 <- optimize(objective2, c(0,10), maximum = TRUE)
q2 <- result2$maximum
}
q1
q2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.