Supplied objective function must return a scalar value in fmincon
Show older comments
Hey guys please help me I am getting this error in my optimization problem
Below is the Data File
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,[],[],[],[],[],[]);
Below is the Objective Function File
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

This is the error I am getting. Please help me.
9 Comments
Walter Roberson
on 17 Jul 2020
I already described the exact cause to you in the thread at https://www.mathworks.com/matlabcentral/answers/379051-fmincon-should-return-a-scalar-value-error#comment_938159
Sanyam Maheshwari
on 17 Jul 2020
Edited: Sanyam Maheshwari
on 17 Jul 2020
Walter Roberson
on 17 Jul 2020
You do not ask it to display results. You run the optimization but do not display anything.
"Local minimum possible" is normal. fmincon() is not a global optimizer, and it cannot do symbolic analysis of the formulas it is working with. It cannot prove that the point it found is a global minima so it does not make the claim that the output is definitely the minima.
Sanyam Maheshwari
on 20 Jul 2020
Walter Roberson
on 20 Jul 2020
x = sym('x',[1,4]); %x as symbolic variable
x is length 4
c2 = double(int(normpdf(x, 400, 100),q,Inf));
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 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 by using double() .
function [c,ceq] = Constraints(Q,b)
How are you invoking that as part of your fmincon() call? You are declaring it with two parameters, so normally you would have to use an anonymous function to pass in whatever b is. However your code does not use b, so it does not care that you are not passing anything in for b. The readers trying to make sense of your code care, though.
Q = zeros(4,4);
You are ignoring the current value of Q that is passed in, so you will always return the same value for the constraint function. It is not worth having a constraint function in such a case: either the constraint can never be satisfied and you are wasting time making the fmincon() call, or else the constraint is always satisfied and you wasting your time processing the constraint.
Sanyam Maheshwari
on 20 Jul 2020
Edited: Sanyam Maheshwari
on 20 Jul 2020
Walter Roberson
on 23 Jul 2020
(reminder to myself)
Sanyam Maheshwari
on 23 Jul 2020
Walter Roberson
on 23 Jul 2020
"(reminder to myself)" indicates that I need to return to this question and give you a reply when I have more time. I have been working on so many questions recently that the ones I still need to do more work on, are scrolling off the first three pages of the lists of my activity.
Answers (0)
Categories
Find more on Conversion Between Symbolic and Numeric 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!


