Fmincon giving error, citing initial objective function (which does satisfy constraints) and not working

I have been trying to optimise a function func using fmincon.
params = fmincon(func,x0,[],[],Aeq,beq)
I have already verified that Aeq*x0 = beq, so the initial objective function is right. Yet, i'm still getting this error:
Not enough input arguments.
Error in symengine>@(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12)c1.*3.805674668010712e-2+c2.*1.452300963913537e-1+c3.*2.085921430624678e-1+c4.*2.085921430624678e-1+c5.*1.452300963913537e-1+c6.*3.805674668010712e-2-(c7.*1.702917447916667e-2+c8.*4.586072682291666e-1+c9.*4.991979401041667e-1+c10.*2.51656171875e-2).^2.*6.066605301267732e-2-(c9.*2.51656171875e-2+c10.*4.991979401041667e-1+c11.*4.586072682291666e-1+c12.*1.702917447916667e-2).^2.*6.066605301267732e-2-(c8.*9.790462239583334e-2+c9.*6.424059244791667e-1+c10.*2.589742838541667e-1+c11.*7.151692708333333e-4).^2.*7.305773474604981e-2-(c8.*7.151692708333333e-4+c9.*2.589742838541667e-1+c10.*6.424059244791667e-1+c11.*9.790462239583334e-2).^2.*7.305773474604981e-2-(c7.*5.818634088541667e-1+c8.*3.598167994791667e-1+c9.*5.334330729166666e-3).^2.*3.782756996279376e-3-(c10.*5.334330729166666e-3+c11.*3.598167994791667e-1+c12.*5.818634088541667e-1).^2.*3.782756996279376e-3-(c7.*4.521909869791667e-1+c8.*5.052392213541667e-1+c9.*2.661019010416667e-2).^2.*1.874353837169595e-2-(c10.*2.661019010416667e-2+c11.*5.052392213541667e-1+c12.*4.521909869791667e-1).^2.*1.874353837169595e-2-(c7.*1.821031666666667e-1+c8.*6.657801666666666e-1+c9.*1.521121666666667e-1).^2.*4.018916131883391e-2-(c10.*1.521121666666667e-1+c11.*6.657801666666666e-1+c12.*1.821031666666667e-1).^2.*4.018916131883391e-2
Error in fmincon (line 568)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in prob1 (line 89)
params = fmincon(func,x0.',[],[],Aeq,beq)
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
I have tried to change the dimensions of Aeq (12*12 in the given example), and the others, change x0 = x0.', but nothing happens. What am I doing wrong?
Thanks

 Accepted Answer

You created func by using matlabFunction() on a symbolic expression that refered to variables c1 through c12.
You should have also used the 'vars' option of matlabFunction
%assuming that c contains the list of c* variables, then
func = matlabFunction(TheExpression, 'vars', {c})
This will pack all of the variables into a single parameter vector and generate code to extract the appropriate elements from the vector. It might look like @(in) in(:,1).*3.805674668010712e-2+in(:,2).*1.4523009639135 and so on.

2 Comments

Thank you, this worked.
I had actually already put
func = matlabFunction(TheExpression);
But adding those two paramters completely solved it.
The default for matlabFunction() is to use one parameter for each different unbound symbolic variable.
(matlabFunction() is a bit weak on bound symbolic variables, such as the variable of integration for int() )

Sign in to comment.

More Answers (1)

I have already verified that Aeq*x0 = beq, so the initial objective function is right.
The error message has nothing to do with Aeq and beq. The error is complaining that func(x0) does not return a numeric value. You should test that, before invoking fmincon.
Since a reference to symengine appears in the error message, I suspect that you are using syms in your objective function, which you probably shouldn't be doing, since fmincon is not part of the Symbolic Math Toolbox.

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!