fmincon, How to fix error "inputs of data type double" ?

11 views (last 30 days)
Hi,
I am doing a minimization with fmincon
Attached files are my constraint function “con”, objective function “obj”, and solution script “F_main”.
Although my objective function returns a scalar (you can try it), however when trying to solve I’m getting an error:
??? Error using ==> fmincon at 234
FMINCON only accepts inputs of data type double.
Error in ==> F_main at 14
[x, fval] = fmincon(F,x0,A,b,Aeq,beq,lb,ub)
The only nonstandard thing that I have is the two equality constraints (two equations instead of one). But even when I used one equation for equality constraint just to check if that what was causing the error, the error still occur.
Please advise what is the source of this error in my script or function, and how can I fix it?
I appreciate considering my humble MATLAB experience.
Yaser

Accepted Answer

Walter Roberson
Walter Roberson on 29 Jan 2018
Edited: Walter Roberson on 30 Jan 2018
Aeq cannot be a function. Aeq is for static equality constraints. You need to move those equality constraints over to the second output of a nonlinear constraint function, which you would pass after the options.
However, taking another look at your cons.m those are really static equality constraints that you can write in matrix form. You have
Ceq1 = 176 - 197*x(2) - 186*x(1)
Ceq2 = 906*x(2) - (687*x(1)) + 9.2485e+003
Aeq = @con
beq = [10000, 0];
that should be replaced with
Aeq = [-186, -197;
-687, 906];
Beq = [10000-176;
0-9.2485e+003];
Also, note that you used @con but supplied cons.m that has function con inside. However, MATLAB identifies functions by their file name when there is a conflict between the file name and the function name, so if you are going to use cons.m then you need to use @cons rather than @con . With the change I indicate here you should not need the function at all (or of a nonlinear constraint function), but you should take note of this for the future
  1 Comment
Yaser
Yaser on 30 Jan 2018
Thank you Walter, thank you very much
I fixed it according to your remarks and it works
You made it very clear and easy to follow

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!