Non Linear Constraint Optimization Problem Error

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

 采纳的回答

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 个评论

Hi Torsten,
Thanks for the reply. The first constraint is
1- (4*x(1)*x(1)*x(2)/Li) < 0 This ensures that sqrt((4*x(1)*x(1)*x(2) - Li)/Li) to be real.
Thanks and Regards,
Lekshman
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.
Thanks Matt for clarifying that.

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2015-7-24
编辑:Matt J 2015-7-24
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 个评论

Thanks Matt for the suggestions. I followed the link you had shared and rewrote the function to take in parameter. I am posting the altered objective function and constraint functions below.
Objective Function
function f = parameterobjfun1(x,It,Li)
if ((4*x(1)*x(1)*x(2))-Li) < 0
f=1e6;
else
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))));
end
end
When (4*x(1)*x(1)*x(2))-Li) is < 0, I have set the function to default high value because if i return Inf for the complex value , the objective function reduces to zero
Constraint Function
function [c,ceq] = parameterconfun1(x,Li,tmax)
if ((4*x(1)*x(1)*x(2))-Li) < 0
c=[1-(4*x(1)*x(1)*x(2)/Li); 2*x(1)*x(2) - tmax ];
else
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 ];
end
ceq=[];
end
With
options=optimset('Algorithm','sqp');
f1=@(x)parameterobjfun1(x,It,Li);
f2=@(x)parameterconfun1(x,Li,tmax);
[x,fval] = fmincon(f1,x0,[],[],[],[],[0;0],[10;30e-6],f2,options)
I get the following message on the command window
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the function tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
x =
0.2266
0.0000
fval =
431.8723
This however is not the minimum of the function. What can be done to fix this?
And Is it not possible to accept more than one answer for a thread? - Thanks, Lekshman
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.
fmincon() is not a global optimizer. You need a tool from the Global Optimization Toolbox to search for global minima.
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

请先登录,再进行评论。

类别

Community Treasure Hunt

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

Start Hunting!

Translated by