Non Linear Constraint Optimization Problem Error

2 views (last 30 days)
Hello,
I am getting the same error as posted in the thread http://in.mathworks.com/matlabcentral/newsreader/view_thread/326736, i.e., " User supplied objective function must return a scalar value". However unlike the case stated in the thread, objective function i use returns a scalar value. I am pasting my objective function and the constraint function for your reference.
  • Objective Function *
function f = objfun(x)
global It
global Li
f=It*sqrt(Li/x(2))*(exp(-(sqrt(Li/((4*x(1)*x(1)*x(2))-Li)))*atan(sqrt(((4*x(1)*x(1)*x(2))-Li)/Li))));
  • Non Linear Constraint Function *
function [c,ceq] = confun(x)
global Li
global tmax
c=[1-(4*x(1)*x(1)*x(2)/Li);((pi - atan(sqrt(((4*x(1)*x(1)*x(2))-Li)/Li)))*2*x(1)*x(2))/(sqrt(((4*x(1)*x(1)*x(2))-Li)/Li)) + 2*x(1)*x(2) - tmax ];
ceq=[];
  • Minimization *
x0=[0.1;1e-6];
Li=1e-6;
tmax=50e-6;
It=1000;
options=optimset('Algorithm','interior-point');
[x,fval] = fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options)
The objective function is returning a scalar value. But why such an error message comes is perplexing. Any suggestion is highly appreciated.
Thanks and Regards, Lekshman

Accepted Answer

Torsten
Torsten on 24 Jul 2015
4*x(1)*x(1)*x(2))-Li < 0 at the start, thus taking the "sqrt" will result in a complex number.
Best wishes
Torsten.
  3 Comments
Matt J
Matt J on 24 Jul 2015
This ensures that sqrt((4*x(1)*x(1)*x(2) - Li)/Li) to be real.
At the solution, but not at the iterations. Here, it didn't seem to matter. The algorithm located a feasible point early on, but I think that might just have been lucky. It is good practice in your code to check for complex values and return Inf at those points.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 24 Jul 2015
Edited: Matt J on 24 Jul 2015
My suggestion would be to accept that there is an unintended event somewhere that is causing f to end up non-scalar. You should get familiar with MATLAB debugging tools for trapping this event. DBSTOP, for example, will stop the code when the error triggers, allowing you to inspect f. You can also use Conditional Breakpoints to force the code to stop specifically when f is non-scalar.
In any case, I cannot reproduce the error when I run your code. For me, fmincon completes with no errors, though of course I had to run without the "options" argument, which you did not provide. My guess would be that the global variables It and Li are not scalars as you suppose, or get changed by other code behind your back. That is why global variables are discouraged in favor of other ways to pass extra parameters, see
  4 Comments
Walter Roberson
Walter Roberson on 27 Jul 2015
fmincon() is not a global optimizer. You need a tool from the Global Optimization Toolbox to search for global minima.
JC
JC on 27 Jul 2015
Edited: JC on 27 Jul 2015
Hi Torsten,
I had updated It value but forgot to invoke f1=@(x)parameterobjfun1(x,It,Li) after that. So was getting a different fval corresponding to x = [0.2266;0.00002]. The minimum corresponding to x = [0.2266;0.00002] is in fact 863.745. My mistake, sorry. Thanks for all the suggestions.
Hi Walter,
Yes the minima value is seen to be varying based on the initial guess.
Thanks and Regards,
Lekshman

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!