Non Linear Constraint Optimization Problem Error
Show older comments
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
More Answers (1)
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
Torsten
on 27 Jul 2015
If you know the minimum, you should choose an initial guess for x(1) and x(2) in its neighbourhood and see what fmincon returns.
Best wishes
Torsten.
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.
Categories
Find more on Linear Least Squares 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!