Error "Supplied objective function must return a scalar value."
8 views (last 30 days)
Show older comments
Hi,I am trying to minimize a function,but shows an error,
clc;
clear;
gb=3;gs=8;
G=[10;10;10;20;30;30;30];
Gh=[30;30;30;20;10;10;10];
H=[200;200;200;200;100;100;100];
k=1;
as(k)=3;ab(k)=8;
for i=1:1:length(G)
Egm=G(i)-Gh(i);
if Egm>0
x = [x i];
elseif Egm<0
y = [y i];
else
z = [z i];
end
end
x0=zeros(7,1);
for j=y(1):1:y(length(y))
F2=@(A)(-(H(j,:).*log(1+A(j,:))+ab(k).*(G(j,:)-A(j,:))));
[A(j,:),fval1(j,:)]=fmincon(F2,x0,[],[],[],[],-100,100)
end
for j=z(1):1:z(length(z))
A(j,:)=G(j)
end
if ES>EB
f=@(a)((a(1).*(G(x,:)+m)+a(2).*(G(y,:)+p)+gb*(H(x,:)./(a(1)))+gb*(H(y,:)./(a(2)))));
[a,fv] = fmincon(f,X0,[],[],[],[],[3 3],[8 8])
as(k+1)=a(1)
ab(k+1)=a(2)
else
end
if (abs(as(k+1)-as(k))>=0.01) && abs(ab(k+1)-ab(k))>=0.01 && rms(abs(Aopt(:,k+1)-Aopt(:,k)))>=0.01
t=1;
else
k=k+1;
end
Error using fmincon (line 641)
Supplied objective function must return a scalar value.
Error in program23 (line 56)
[a,fv] = fmincon(f,X0,[],[],[],[],[3 3],[8 8])
Can someone explain me why? Please help me to correct this code.
Thanks in advance
0 Comments
Accepted Answer
Torsten
on 3 Jun 2022
The function you pass to fmincon returns a vector of values, not a scalar.
Maybe you mean
f=@(a) norm(sum(((a(1).*(G(x,:)+m)+a(2).*(G(y,:)+p)+gb*(H(x,:)./(a(1)))+gb*(H(y,:)./(a(2)))))));
or something similar.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!