fmincon should return a scalar value error

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 commentaires

Difficult to say without the actual objective function and knowing the sizes of the variables involved.
If that is intended to be the actual objective then you need to split the lines properly for us.
Hassan garni
Hassan garni le 26 Jan 2018
Hello Walter,
Thank you for your response. I will try to post more details below with the whole function.
AJEET Kumar
AJEET Kumar le 30 Août 2022
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 le 30 Août 2022
Take the initial condition in the neighbourhood of the solution you want to get from fmincon :-)

Connectez-vous pour commenter.

 Réponse acceptée

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 commentaires

Hello Nicolas, thank you for your response. It shows a vector using the test function as you suggested. Moreover, I used "isscalar(objective)" and then isvector(objective)" and both gave logical 1.
the function is following :
objective = @(x)(-((((1-0.05.*(secd((acosd((cos(R).*cos(deg2rad((Azd)-(x(1)))).*sin(deg2rad(x(2)))+ sin(R).*cos(deg2rad(x(2)))))))-1)).*(((DNI.*(cos(R).*cos(deg2rad((Azd)-(x(1)))).*sin(deg2rad(x(2)))+ sin(R).*cos(deg2rad(x(2)))))+((DNI.*(cos(R).*cos(deg2rad((Azd)-(x(1)))).*sin(deg2rad(x(2)))+sin(R).*cos(deg2rad(x(2)))))))))+(DHI.*((1+cos(deg2rad(x(2))))/2)*df)+(alb.*GHI.*(1-cos(deg2rad(x(2))))/2))*Ar*eff));
You need to look at objective() evaluated at a point, not at the function handle.
Hassan garni
Hassan garni le 26 Jan 2018
I am not sure what you mean by look at objective() evaluated at a point.
x = [1 2];
a = objective(x)
Now is "a" a scalar or a vector ? If a is a vector, you will have to change something in your definition of "objective".
Best wishes
Torsten.
What is the size() of these variables:
alb, Ar, Azd, df, DNI, eff, GHI, R
Hassan garni
Hassan garni le 26 Jan 2018
Modifié(e) : Hassan garni le 26 Jan 2018
Hello Torsten, the test using
x = [1 2];
a = objective(x)
shows a vecotr !
Hassan garni
Hassan garni le 26 Jan 2018
Hi Walter, the size of each element as follow:
  • alb = 1x1;
  • Ar = 1x1;
  • Azd = 8760x1;
  • df = 1x1;
  • DNI = 8760x1;
  • eff = 1x1;
  • GHI = 8760x1;
  • R = 8760x1
Your objective has a number of multiplications by expressions that are 8760x1. It has no sum() or prod() or mean() or other function that would aggregate these 8760 elements into a single value. You do have a small number of / operators, but they are /2 rather than dividing by a vector so the / operator is not being used to aggregate the 8760 elements into a single value either.
We must conclude from this that objective() returns an 8760 x 1 vector. fmincon() cannot optimize an 8760 x 1 vector.
If each of the 8760 elements is to be considered a separate objective, you need something like gamultiobj or fgoalattain .
Hassan garni
Hassan garni le 28 Jan 2018
Thank you very much, Walter. I will consider reforming the objective function in which should give a scalar output.
I am also getting the same error. Please help.
Below is the Data File and Below that is the objective function file. Please help.
global SL V p r k m n C Z s
SL = 0.75;
V = 94100;
p = [0.07,0.18,0.2,0.3];
r = [55,47,45,49];
k = [33,28,29,30];
m = 4;
n= 4;
C = [78,69,70,73;
64,68,56,59;
34,39,42,41;
52,47,48,45];
Z =[250 250 250 250;
320 320 320 320;
440 440 440 440;
350 350 350 350];
s = [110,95,99,100;
110,95,99,100;
110,95,99,100;
110,95,99,100];
bguess = randi(4,4);
bguess = (bguess<2);
Q0 = [zeros(4,4) bguess];
% call the solver
xopt = fmincon(@EP, Q0,[],[],[],[],[],[]);
function G = EP(Q,b)
global s r k C m n
rng;
y = rand(4);
b = randi(4,4);
b = (b<2);
q = 0;
for i = 1:m
for j = 1:n
q = q + y(i,j)*Q(i,j);
end
end
x = sym('x',[1,4]); %x as symbolic variable
f1 = 0;
for i = 1:m
for j = i:n
f1 = f1 + s(i,j)*b(i,j)*x(j);
end
end
f2 = 0;
for i = 1:m
for j = 1:n
f2 = f2+C(i,j)*b(i,j)*y(i,j)*Q(i,j);
end
end
f3 = 0;
f3a = 0;
for i = 1:m
for j = 1:n
f3a = f3a + y(i,j)*Q(i,j)-x(j);
end
end
for i = 1:m
for j = 1:n
f3 = f3 + b(i,j)*r(j)*f3a;
end
end
f4 = 0;
for i = 1:m
for j = 1:n
f4 = f4+ s(j)*b(i,j)*y(i,j)*Q(i,j);
end
end
f5 = 0;
f5a = 0;
for i = 1:m
for j = 1:n
f5a = f5a + x(j) -y(i,j)*Q(i,j);
end
end
for i = 1:m
for j = 1:n
f5 = f5 + b(i,j)*k(j)*f5a;
end
end
F1 = int(((f1 - f2 + f3).*normpdf(x, 400, 100)),0,q);
F2 = int(((f4 - f2 - f5).*normpdf(x, 400, 100)),q,Inf);
G = -(F1 +F2);
end
x = sym('x',[1,4]); %x as symbolic variable
x is length 4
F1 = int(((f1 - f2 + f3).*normpdf(x, 400, 100)),0,q);
x is length 4, so normpdf(x, 400, 100) is a vector of length 4. You integrate an expression involving the vector of length 4, from 0 to q, where q is 0, and integral of anything from 0 to 0 is 0, so you get back a vector of 4 zeros.
F2 = int(((f4 - f2 - f5).*normpdf(x, 400, 100)),q,Inf);
Likewise, the normpdf is a vector of length 4. You integrate an expression involving the vector of length 4, from q to inf, where q is 0. You do not specify a variable of integration, so it does symvar() and picks the first one, which will be x1. So you integrate the expression of length 4 with respect to x1. As the expression involves x1, x2, x3, x4, the result is going to be of length 4 involving x2, x3, x4 (the x1 vanishes because it is a definite integration with respect to x1.) Because the result involves unresolved variables x2, x3, x4, there is no way to convert the result into a numeric scalar such as needed by fmincon -- for example -sum(F1+F2) will not work because although you would have reduced the length 4 to a scalar, it would still involve the unresolved variables x2, x3, x4.
Now, I could suggest that perhaps you wanted something like
syms X
F2 = int(((f4 - f2 - f5).*normpdf(X, 400, 100)),X,q,Inf);
which would give you a scalar result. But the f4-f2-f5 expression involves the unresolved variables x1, x2, x3, x4, so again the result would not be something you could convert to numeric.
Note: if you do manage to get down to a scalar without any unresolved variables, then beware that fmincon will not accept symbolic numbers: you must convert the symbolic expression into double()
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
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?

Connectez-vous pour commenter.

Plus de réponses (1)

maged salah
maged salah le 20 Juil 2020

0 votes

3 commentaires

Walter Roberson
Walter Roberson le 20 Juil 2020
This does not appear to be an Answer to the original Question, and does not appear to be an Answer to anything, and also does not appear to be related to any of the earlier discussion ?
maged salah
maged salah le 20 Juil 2020
if you can help ,I need a drawing for this table (parallel,step response)

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by