fmincon should return a scalar value error
조회 수: 51 (최근 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
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
2022년 8월 30일
Take the initial condition in the neighbourhood of the solution you want to get from fmincon :-)
채택된 답변
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
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
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
2020년 7월 20일
댓글 수: 3
Walter Roberson
2020년 7월 20일
You should open a new Question for this. When you do, then http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!