In this case, optim will not work because you have equality constraints. constrOptim will not work for the same reason (I tried to convert equality into two inequalities, i.e. more and less than 15, but this is not with constrOptim ).
However, there is a package dedicated to this issue, and this is Rsolnp .
You use it as follows:
#specify your function opt_func <- function(x) { 10 - 5*x[1] + 2 * x[2] - x[3] }
Output:
> solnp(c(5,5,5), + opt_func, + eqfun=equal, + eqB=15, + LB=c(0,0,0), + UB=c(100,100,100)) Iter: 1 fn: -65.0000 Pars: 14.99999993134 0.00000002235 0.00000004632 Iter: 2 fn: -65.0000 Pars: 14.999999973563 0.000000005745 0.000000020692 solnp--> Completed in 2 iterations $pars [1] 1.500000e+01 5.745236e-09 2.069192e-08 $convergence [1] 0 $values [1] -10 -65 -65 $lagrange [,1] [1,] -5 $hessian [,1] [,2] [,3] [1,] 121313076 121313076 121313076 [2,] 121313076 121313076 121313076 [3,] 121313076 121313076 121313076 $ineqx0 NULL $nfuneval [1] 126 $outer.iter [1] 2 $elapsed Time difference of 0.1770101 secs $vscale [1] 6.5e+01 1.0e-08 1.0e+00 1.0e+00 1.0e+00
Thus, the resulting optimal values ββare:
$pars [1] 1.500000e+01 5.745236e-09 2.069192e-08
which means that the first parameter is 15, and the rest are zero and zero. This is truly a global minimum in your function, since x2 adds a function, and 5 * x1 has a much larger (negative) effect than x3 on the result. Choosing 15, 0, 0 is the solution and the global minimum for the function in accordance with the constraints.
Function worked great!