Skip to content
Prev 262838 / 398502 Next

Constrained Optimization

I am trying to optimize a simple function, but I want to optimize it for
values of k between 0 and 1. 

I read the help file for constrOptim function but it is always giving me
the error
"Error in ui %*% theta : non-conformable arguments"

My starting value is 0.4...i also tried c(0.4). simulation is the function name pasted below (It returns a scalar, as per the requirements of the constrOptim call). 
ui is the bound (0,0) to (1,1)

What am I doing wrong?


Thanks in advance!
function(k)
{
sum = 0
firstnum<-runif(1)

if(firstnum > k)
{
	sum<-sum + firstnum
}
else
{
	sum<-sum + firstnum+runif(1)
}

if(sum>1)
{
	sum<-0
}

return (sum)
}