MATLAB: Use fmincon to solve a complex nonlinear optimization: Not enough input arguments.

fminconnonlinear optimization

hi
I am trying to use fmincon to solve a nonlinear optimization question. But the objective function is so complex that I cannot easily use x(1) and x(2) to describe. So i used a function file to link the fun. But there is always some error:Not enough input arguments.
i thought the error is because the obj function is too complex (maybe?). Please help me, thx!!
function [ y ] = expectedprofit(k,s)
potential_arrival_rate=50;
lambda=potential_arrival_rate*s;
mu=4;
c=200;
K=100;
rho=lambda/mu;
syms t;
h=symsum(power(rho,t)/factorial(t), t,0,k-1);
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 = @(k,s)expectedprofit;
lb = [0,0];
ub = [100,1];
A = [-1,100];
b = 0;
Aeq = [];
beq = [];
x0 = [5,0.2];
[k,s] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub);
the error message:
Not enough input arguments.
Error in expectedprofit (line 5)
lambda=potential_arrival_rate*s;
Error in @(k,s)expectedprofit
Error in fmincon (line 535)
initVals.f = feval(funfcn{3},X,varargin{:});
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.

Best Answer

Without looking at your code in detail, I think that you made the error of using two different inputs, k and s, to fmincon. As well-documented, Optimization Toolbox™ solvers take ONE input, usually called x. But this input can be a vector or array, so it is easy to convert your function:
function y = expectedprofit(x)
k = x(1);
s = x(2);
% The remainder of your code goes here verbatim
Then set your function up like this:
fun = @expectedprofit;
% other setup steps here until:
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub);
Obviously, when you get your solution x, you will have k = x(1) and s = x(2).
Alan Weiss
MATLAB mathematical toolbox documentation