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

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.

 Accepted 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

2 Comments

Hi alan it works! thank you very much.
but there is still some error I cannot figure out after changing the code. I thought the error is caused by the symsum I defined (sym t) but i am not sure and if it is, how can i change it?
Error using fmincon (line 683)
FMINCON requires all values returned by functions to be of data type double.
Error in main (line 9)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub);
my function code is as follows:
function [ y ] = expectedprofit(x)
k = x(1);
s = x(2);
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
could you give me any idea on where goes wrong?
thanks a lot!!!
By the use of
syms t
y becomes a symbolic variable.
But the return argument to fmincon has to be of type double.
Best wishes
Torsten.

Sign in to comment.

More Answers (0)

Asked:

on 9 Mar 2017

Commented:

on 9 Mar 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!