Conversion to double from function_handle is not possible.

1 visualización (últimos 30 días)
hussain askar
hussain askar el 17 de Oct. de 2021
Editada: Stephen23 el 17 de Oct. de 2021
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)

Respuestas (1)

Jan
Jan el 17 de Oct. de 2021
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 comentario
Stephen23
Stephen23 el 17 de Oct. de 2021
Editada: Stephen23 el 17 de Oct. de 2021
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with Optimization Toolbox en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by