# [R] Optimization problem with constraints

Matthias Graser TexasRanger.Shaft at gmx.de
Mon Jun 2 15:15:57 CEST 2008

```I'm trying to da an optimization for the followig function

Zwischenwert <- function (x)
{
lambda<-x[1];
mu<-x[2];
gammal<-x[3];
mud<-x[4];
Mittelwert <-0;
for(t in 0:(T-1))
{
for(i in 0:(n-1))
{
for(j in i:(n-1))
{
Mittelwert <- Mittelwert +( phi[i+1,j+1,t+1]*((j-i)*log(lambda)-log(factorial(j-i))-lambda));
};
};
};
};

{
lambda<-x[1];
mu<-x[2];
gammal<-x[3];
mud<-x[4];
dlambda<-0;
dmu<-0;
dgamma<-0;
dmud<-0;
for (t in 0:(T-1))
{
for (i in 0:(n-1))
{
for (j in i:(n-1))
{
dlambda<-dlambda-phi[i+1,j+1,t+1]*(-1+(j-i)/lambda);
};
};
};
for (t in 1:(T-1))
{
for (i in 0:(n-1))
{
};
};
};

lambda, gammal und gammd need to be larger than 0, so I've tried different constrained methods like L-BFGS-B via optim and constrOptim, but both seem to have problems with my functions. The standard optim-method works, but sometimes it returns negative values for lambda, gammal and gammad.
Those phi and phisums are known at runtime, n and T can be pretty large, so maybe R simple can't handel it.

Many thanks
Matthias

--

```