fmincon should return a scalar value error

50 次查看(过去 30 天)
Hello,
I am trying to do an optimization for a large function using a "fmincon" function. I have got an error and couldn't find the problem. The main function has so many trigonometric functions (cos, sin. ...). The following is the error obtained :
Error using fmincon (line 609)
Supplied objective function must return a scalar value.
Error in Energy_test2 (line 74)
x = fmincon(objective,x0,A,b,Aeq,beq,lb,ub,'flcon');
The following is the main script using "fmincon". I also have checked the matrices dimensions.
objective = @(x)( cos(x(1)).*sin(x(2))+ ....(cos(t).*cos((Ay)-(x(1)))).*...*Ar*e));
x0 = [2,42];
disp(['initial objective:' num2str(objective(x0))])
A = [];
b=[];
Aeq = [];
beq = [];
x0 = [1,4];
lb = [0,0];
ub = [90,90] ;
nonlinecon = @flcon;
x = fmincon(objective,x0,A,b,Aeq,beq,lb,ub,'flcon');
I appreciate if somebody help with this issue and how can be solved.
Thank you.
Hassan
  4 个评论
AJEET Kumar
AJEET Kumar 2022-8-30
Hello sir, Sir how to decide the initial condition in fmincon problem.because fir different initial condition we get different solution.so please ask me any formula for take initial condition.
Torsten
Torsten 2022-8-30
Take the initial condition in the neighbourhood of the solution you want to get from fmincon :-)

请先登录,再进行评论。

采纳的回答

Nicolas Schmit
Nicolas Schmit 2018-1-26
fmincon() crashed because it expected the objective function to return a scalar value but got something that was not a scalar.
Test your objective function on the initial conditions and check that the output is a scalar.
objective(x0) % Scalar?
If you need further help, post the complete program.
  13 个评论
Sanyam Maheshwari
Sanyam Maheshwari 2020-7-17
Hi Walter
I just posted in order to get my answer as fast as possible.
As suggested by you I tried using syms x.
That again resulted in different errors.
If possible if you could suggest me to use any other solver. I am new to MATLAB So I don't have much experience.
Here x represents demand of 4 different products.
I am solving an optimization problem in which is somewhere linked with newspaper vendor problem of operations management.
Also can you please explain how I have to use double() with an example.
It will be really helpfull
Walter Roberson
Walter Roberson 2020-7-17
Here x represents demand of 4 different products.
The demands of 4 different products can be optimized in more than 16 different ways. If we call them P1, P2, P3, P4, then one way would be to find the mimimum of P1, ignoring P2, P3, P4. Another would be to find the mnimum of P2, ignoring P1, P3, P4. Another would be to find the product of P2 and P3 ignoring P1 and P4. Other way would be to find the sum of P1+P4, ignoring P2 and P3. And so on. You might even ask about min() of the P1, P2, P3, P4 values --- but if you use min() then the function is not differentiable and fmincon() cannot be used.
Each of the products is an "objective", and fmincon() can only optimize one objective. When you have four objectives, you need to either use a different routine such as gamultiobj() or else you need to find a function that takes the four individual objectives and forms a unified score.
For example, suppose you could lower P1, P2, P3 by 0.02 each, but only at the cost of increasing P4 by 0.01: should you do that? Suppose the mininum for P1 is at its lower bound, but that forces P3 to be relatively large: should you do that?

请先登录,再进行评论。

更多回答(1 个)

maged salah
maged salah 2020-7-20
  3 个评论
maged salah
maged salah 2020-7-20
if you can help ,I need a drawing for this table (parallel,step response)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by