[R] constraint in constrOptim

Paul Smith phhs80 at gmail.com
Thu Aug 2 21:58:05 CEST 2007


On 8/2/07, Paul Smith <phhs80 at gmail.com> wrote:
> > I'm using the function constrOptim together with the "SANN" method and
> > my objective function (f) has two parameters. One of the parameters
> > needs be into (2^(-10), 2^4) range and the other into (2^(-2), 2^12)
> > range. How can I do it using constrOptim??
>
> I think that you, André, do not need constrOptim; optim is enough. See
>
> ?optim

The following example shows how to use optim to calculate the maximum of

f(x,y) := (x-y)^2

s.t. 0 <= x,y <= 1.

Paul

-------------------------------

myfunc <- function(x) {
  x1 <- x[1]
  x2 <- x[2]
  (x1-x2)^2
}

mygrad <- function(x) {
  x1 <- x[1]
  x2 <- x[2]
  c(2, -2) * c(x1, x2)
}

optim(c(0.5,0.5),myfunc,mygrad,lower=c(0,0),upper=c(1,1),method="L-BFGS-B",control=list(fnscale=-1))



More information about the R-help mailing list