how to have function with scalar value

13 views (last 30 days)
Ali Esmaeilpour
Ali Esmaeilpour on 6 Sep 2019
Answered: Catalytic on 6 Sep 2019
Greetings people! I have a code and 2 objective functions when I want to use fmincon it says function has to be scalar
here is my code:
function z = MyCost(x)
z1 = Obj1(x);
z2 = Obj2(x);
z = [z1;z2];
end
I know that my problem is because of above code z=[z1;z2] but I dont know how to have z which consist of z1 and z2 in a way to have scalar value of z
clc;
clear;
close all;
%% Problem Definition
nVar=3;
VarSize=[1 nVar];
VarMin=10^(-3);
VarMax=10^2;
%% Weighted-Sum Approach
N=60;
w1=linspace(0,1,N);
w2=1-w1;
for i=1:N
FWS=@(x) w1(i)*Obj1(x)+w2(i)*Obj2(x);
x0=unifrnd(VarMin,VarMax,VarSize);
A=[];
b=[];
LB=10^(-3);
UB=10^2;
options=optimoptions('fmincon','TolFun',10^(-12),'TolCon',10^(-10),'MaxFunEvals',4*10^3);
sol(i).Position=fmincon(FWS,x0,A,b,[],[],LB,UB,@NLC,options);
sol(i).Cost=MyCost(sol(i).Position);
end
Costs=[sol.Cost];
figure;
plot(Costs(1,:),Costs(2,:),'.');
function [C, Ceq]=NLC(x)
n=2;
C=[((30767*((1000*x(1:n))/29011 - 50500/29011)^2)/100000 - (37571*x(1:n))/2901100 + (33539*((1000*x(1:n))/29011 - 50500/29011)^3)/100000 - (8057*((1000*x(1:n))/29011 - 50500/29011)^4)/12500 + (7961*((1000*x(1:n))/29011 - 50500/29011)^5)/25000 + (1278950236579183*((1000*x(1:n))/29011 - 50500/29011)^6)/18014398509481984 - (1433*((1000*x(1:n))/29011 - 50500/29011)^7)/10000 + (360206905396347*((1000*x(1:n))/29011 - 50500/29011)^8)/9007199254740992 + (6604510839140323*((1000*x(1:n))/29011 - 50500/29011)^9)/576460752303423488 - (3107872853893447*((1000*x(1:n))/29011 - 50500/29011)^10)/576460752303423488 + 37931111499167682390657/52261571515858183782400)^2 + ((787049070879267875*x(1:n))/1045231430317163675648 + (27059*((1000*x(1:n))/29011 - 50500/29011)^2)/50000 - (75971*((1000*x(1:n))/29011 - 50500/29011)^3)/100000 + (19587*((1000*x(1:n))/29011 - 50500/29011)^4)/100000 + (3329*((1000*x(1:n))/29011 - 50500/29011)^5)/10000 - (14281*((1000*x(1:n))/29011 - 50500/29011)^6)/50000 + (634413072308427*((1000*x(1:n))/29011 - 50500/29011)^7)/18014398509481984 + (2918764904100309*((1000*x(1:n))/29011 - 50500/29011)^8)/72057594037927936 - (3668884458035139*((1000*x(1:n))/29011 - 50500/29011)^9)/288230376151711744 + (1831068088942187*((1000*x(1:n))/29011 - 50500/29011)^10)/295147905179352825856 - 284748233055908747705/2090462860634327351296)^2-x(n+1:end)
-x(n+1:end)];
Ceq=[];
end
function z = Obj2(x)
yref = 0;
p1 = -0.0053913;
p2 = 0.011457;
p3 = 0.039991;
p4 = -0.1433;
p5 = 0.070996;
p6 = 0.31844;
p7 = -0.64456;
p8 = 0.33539;
p9 = 0.30767;
p10 = -0.37571;
p11 = 0.071788;
mu = 50.5;
sigma = 29.011;
s = [10^6 10^4 10^2];
S = diag(s);
n=2;
g = (x(1:n)-mu)/sigma;
z = sum((yref-(p1*g.^10+p2*g.^9+p3*g.^8+p4*g.^7+p5*g.^6+p6*g.^5+p7*g.^4+p8*g.^3+p9*g.^2+p10*g+p11)).^2)+x(n+1:end)'*S*x(n+1:end);
end
function z = Obj1(x)
yref = 0;
p1 = -6.2039e-06;
p2 = 0.012729;
p3 = -0.040506;
p4 = -0.035217;
p5 = 0.28562;
p6 = -0.3329;
p7 = -0.19587;
p8 = 0.75971;
p9 = -0.54118;
p10 = -0.021845;
p11 = 0.098187;
mu = 50.5;
sigma = 29.011;
s = [10^6 10^4 10^2];
S = diag(s);
n=2;
g = (x(1:n)-mu)/sigma;
z = sum((yref-(p1*g.^10+p2*g.^9+p3*g.^8+p4*g.^7+p5*g.^6+p6*g.^5+p7*g.^4+p8*g.^3+p9*g.^2+p10*g+p11)).^2)+x(n+1:end)'*S*x(n+1:end);
end
  2 Comments
Ali Esmaeilpour
Ali Esmaeilpour on 6 Sep 2019
Edited: Ali Esmaeilpour on 6 Sep 2019
consider weighted summation of those Obj1 and Obj2 and make a function named FWS and give it to fmincon.

Sign in to comment.

Answers (1)

Catalytic
Catalytic on 6 Sep 2019
"I have a code and 2 objective functions"
If you really do want to treat this as a multi-objective problem, you should probably use gamultiobj or paretosearch instead of fmincon.

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!