I think that you mainly need to realize that optimization toolbox generally asks that you put all of your control variables into one input argument, typically called x. See Writing Scalar Objective Functions. Also, since you have other data or variables that the solver is not supposed to change, include these extra parameters using the techniques of Passing Extra Parameters. I suggest that you break up your variables into the x = control variables (the five-dimensional x variable that you want fmincon to change) and the extra parameters, and rewrite your functions in terms of x and the others explicitly. Something like
function f = myfun(x,extra)
a = x(1);
b = x(2);
c = x(3);
d = x(4);
e = x(5);
Your subsidiary functions, the ones that help compute the objective, can have any inputs and outputs that you like, but for consistency they should use the same x as well as any other variables as inputs.
f is necessarily a scalar output of myfun.
function [c,ceq] = nonlcon(x,extra)
c(1) = fun2(x,extra);
c(2) = fun3(x,extra);
ceq = [];
Call the functions properly, such as
fun = @(x)myfun(x,extra)
nlc = @(x)nonlcon(x,extra);
[solution,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nlc,options)
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
Best Answer