Conversion to double from function_handle is not possible.

2 vues (au cours des 30 derniers jours)
hussain askar
hussain askar le 17 Oct 2021
Modifié(e) : Stephen23 le 17 Oct 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)

Réponses (1)

Jan
Jan le 17 Oct 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 commentaire
Stephen23
Stephen23 le 17 Oct 2021
Modifié(e) : Stephen23 le 17 Oct 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with Optimization Toolbox dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by