MATLAB: Fmincon nonlinear optimization with some factorial error

fminconnonlinear optimization

hi i am a matlab beginner and i am trying to use fmincon to solve a 2-dimensional optimization problem
there is always some error with the factorial part: (i set k as non-negative values but why still exists error?)
Error using factorial (line 20)
N must be an array of real non-negative integers.
Error in expectedprofit (line 19)
denominator = h+k*power(rho,k)/[(k-rho)*factorial(k)];
Error in fmincon (line 535)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in main (line 9)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub);
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
my function code is:
function [ y ] = expectedprofit(x)
k = x(1);
s = x(2);
potential_arrival_rate=50;
lambda=potential_arrival_rate*s;
mu=1;
c=20;
K=1000;
rho=lambda/mu;
h=0;
for t=0:k-1
h=h+power(rho,t)/factorial(t);
end;
denominator = h+k*power(rho,k)/[(k-rho)*factorial(k)];
numerator = k*power(rho,k)/[(k-rho)*factorial(k)];
C = numerator/denominator;
waitingtime = C/[mu*(k-rho)];
y=-lambda*[1-s-c*waitingtime-power(k,2)/(K*lambda)];
end
and my fmincon code is:
fun = @expectedprofit;
x0 = [1,0.1];
A = [-1,100];
b = 0;
Aeq = [];
beq = [];
lb = [0,0];
ub = [100,1];
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub);

Best Answer

Think about what you are doing. But, first, READ THE ERROR MESSAGE.
"Error using factorial (line 20)
N must be an array of real non-negative integers."
You have this line:
denominator = h+k*power(rho,k)/[(k-rho)*factorial(k)];
what is k?
k = x(1);
k is one of the optimization variables.
The factorial function requires that the input be an integer. That you required k to be non-negative is not relevant. The operative word is INTEGER.
fmincon is not restricted to integers. In fact, you cannot force fmincon to use only integers. IF you tried, that would create a discontinuous objective.
You must either use an optimizer that can work with integer variables, OR you must use the extension of factorial to the real line, i.e., the gamma function. (BE CAREFUL THERE, because there is an offset of 1 in that transformation.)