Conversion to double from function_handle is not possible.

조회 수: 1 (최근 30일)
hussain askar
hussain askar 2021년 10월 17일
편집: Stephen23 2021년 10월 17일
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