Search code examples
roptimizationmaximization

optimization (Maximization) in R


I have one function that wants to optimize (Maximize) in R, any one could help me. My equation is as follow:

Y=-10.6134 -3.477*X1 +4.743*X2 +56.10*X3 -0.07671*X4 +0.1005*X1^2-0.0529*X2^2 -25.741*X3^2 +0.000279*X4^2 -0.0984*X1*X2 -1.351*X1*X3 -0.00407*X1*X4

With these limitations:

3 <= X1 <= 11
25 <= X2 <= 45 
0.1 <= X3 <= 10  
10 <= X4 <= 200 

I already optimize that with “solver” function in “Excel” and also I try some packages such as “optim” and “nlminb” but they don’t work for me. Please help me; I really need it for my university project. Tnx


Solution

  • Define your function

    f<-function(X1,X2,X3,X4){
        -10.6134 -3.477*X1 +4.743*X2 +56.10*X3 -0.07671*X4 +
        0.1005*X1^2-0.0529*X2^2 -25.741*X3^2 +0.000279*X4^2 -
        0.0984*X1*X2 -1.351*X1*X3 -0.00407*X1*X4
    }
    

    Write your function as a one-argument function as this is needed for optim, and put a minus as your optimizing

    f2 <- function(x) -f(x[1],x[2], x[3],x[4])
    

    Use Optim

    optim(par=c(6,35,5,100),fn=f2,lower = c(3,25,0.1,10),upper = c(11,45,10,200),method = "L-BFGS-B")