Conversion to double from function_handle is not possible.

2 次查看(过去 30 天)
I'm trying to solve an economic dispatch problem using the fmincon function, but i'm getting many different errors in the function inside the loop. Can you please help me run this code without errors? This is my first time using fmincon.
Nc = 3;
a = [ 0.06 0.03 0.04];
b = [ 0.5 0.25 0.3];
c = [ 5 4 2 ];
p0 = [2 4 7 ];
objective = zeros(1,3);
for i = 1:Nc
objective(i) = @(p) a(i)^2 * p(i)^2 + b(i) * p(i) + c(i);
end
pd = 550;
A = [];
b = [];
Aeq = [ p(1) p(2) p(3)];
beq = [ pd ];
lb = [ 0 0 0 ];
ub = [ 4 6 9 ];
% disp([' Initial Objectives: ' num2str(objective(p0))])
cc = fmincon(objective(i), p0, A , b , Aeq, beq, lb, ub);
disp(cc)

回答(1 个)

Jan
Jan 2021-10-17
objective = zeros(1,3); % Here objective is a double
for i = 1:Nc
% And here you want to store a function handle in the double elements:
objective(i) = @(p) a(i)^2 * p(i)^2 + b(i) * p(i) + c(i);
end
This cannot work.
Try this instead:
objective = @(p) a.^2 .* p.^2 + b .* p + c;
  1 个评论
Stephen23
Stephen23 2021-10-17
编辑:Stephen23 2021-10-17
The objective function for FMINCON needs to return a scalar, so the anonymous function will need to use SUM, MEAN, or some other way to return a single value as the output.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by