# Supplied objective function must return a scalar value

8 views (last 30 days)
hussain askar on 18 Oct 2021
Answered: Alan Weiss on 19 Oct 2021
I have two questions here for this economic dispatch problem using fmincon:
1) i'm trying to set Aeq = [ p(1) p(2) p(3)] because my equality constraint is the power balance which means that p1 + p2 + p3 = pd. However, when i use this Aeq = [ p(1) p(2) p(3)], i get an error that p is unrecognized. The point is that the function should start with the initial value p0 and then do its thing and find the next p that follow the constraints. What am I doing wrong here?
2) to make the code run I used Aeq = [ 3 5 4], the next issue i'm having is ( Supplied objective function must return a scalar value ). We have 3 different generators, so shouldn't fmincon return the minimization values for each different generator which means 3 answers? why am i getting this issue and what should i do to fix it?
clc; clear;
Nc = 3;
a = [ 0.06 0.03 0.04];
bb = [ 0.5 0.25 0.3];
c = [ 5 4 2 ];
p0 = [2 4 7 ];
%objective = cell(1,3);
objective = @(p) a.^2 .* p.^2 + bb .* p + c;
disp([' Initial Objective: ' num2str(objective(p0))])
pd = 12;
A = [];
b = [];
% Aeq = [ p(1) p(2) p(3)];
% Aeq = [ sum(p(i))];
Aeq = [ 3 5 4];
beq = [ pd ];
lb = [ 0 0 0 ];
ub = [ 4 6 9 ];
p = fmincon(objective, p0, A , b , Aeq, beq, lb, ub);
disp(p)
disp([' Final Objective: ' num2str(objective(p))])

Alan Weiss on 19 Oct 2021
You have a misconception about what linear constraints mean and what a scalar objective function is. Linear constraints do NOT take variable values as matrix entries. I mean, the A and Aeq matrices must be constant values. If you want to keep the sum of your p variables equal to a constant value pd, well, you have a nonlinear equality constraint.
As for your objective function, the value must be a scalar. I think that you mean for your objective function to be
objective = @(p) sum(a.^2 .* p.^2 + bb .* p + c); % Add to get a scalar
I suggest that the easiest way for you to formulate and solve a problem is to use the Problem-Based Optimization Workflow. It is a bit different, but is much easier to use overall.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

R2021b

### Community Treasure Hunt

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

Start Hunting!