Reputation: 118
I'm new to MATLAB and I need help to solve this optimization problem. Here is the objective function m file:
function f = objfun(x,w1,w2)
w = 6:1:125;
z1 = x(4).*((x(1).*(1i.^2).*w.^2)+(x(5).*1i.*w)+x(3));
z2 = x(1).*x(2).*(1i.^4).*w.^4;
z3 = ((x(1)+x(2)).*x(5).*(1i.^3).*w.^3);
z4 = (1i.^2).*w.^2.*((x(1).*x(3))+(x(2).*x(3))+(x(1).*x(4)));
z5 = 1i.*w.*(x(5).*x(4));
z6 = x(3).*x(4);
z7 = x(4).*(-x(5).*1i.*w-x(3));
z8 = (z7./(z2+z3+z4+z5+z6));
trfs = 0.1.*(w.^2).*(z7./(z2+z3+z4+z5+z6));
trfs2 = 0.1.*(z7./(z2+z3+z4+z5+z6));
trfu = 0.1.*(z1./(z2+z3+z4+z5+z6));
abstrfs = abs(trfs);
abstrfs2 = abs(trfs2);
abstrfu = abs(trfu);
y1 = rms(abstrfs);
y2 = rms(abstrfs2);
y3 = abs(rms(abstrfu)-y2);
f = w1.*(y1.^2)+w2.*(y3.^2);
end
These are the constraints:
function [c,ceq] = confun(x,z8,y1,trfu)
c(1) = y1 - 0.315;
c(2) = abs(z8-trfu) - 0.217;
c(3) = abs(trfu) - 0.07;
c(4) = sqrt(x(3)./x(1)) - 9.425;
ceq = [];
end
Main file
x0 = [510 85 81000 650000 3000];
UB = [764 124 120720 839170 3840];
LB = [509 83 80480 559440 2560];
j = 1;
for i = 0:0.05:1
w1 = i;
w2 = 1-i;
[x,fval] = fmincon(@objfun,x0,[],[],[],[],LB,UB,@confun,[],w1,w2);
w = 6:1:125;
z1 = x(4).*((x(1).*(1i.^2).*w.^2)+(x(5).*1i.*w)+x(3));
z2 = x(1).*x(2).*(1i.^4).*w.^4;
z3 = ((x(1)+x(2)).*x(5).*(1i.^3).*w.^3);
z4 = (1i.^2).*w.^2.*((x(1).*x(3))+(x(2).*x(3))+(x(1).*x(4)));
z5 = 1i.*w.*(x(5).*x(4));
z6 = x(3).*x(4);
z7 = x(4).*(-x(5).*1i.*w-x(3));
z8 = (z7./(z2+z3+z4+z5+z6));
trfs =(w.^2).*(z7./(z2+z3+z4+z5+z6));
trfs2 = (z7./(z2+z3+z4+z5+z6));
trfu = (z1./(z2+z3+z4+z5+z6));
abstrfs = abs(trfs);
abstrfs2 = abs(trfs2);
abstrfu = abs(trfu);
y1(j) = rms(abstrfs);
y2(j) = rms(abstrfs2);
y3(j) = abs(rms(abstrfu)-y2(j));
j = j+1;
end
plot (y1,y3,'r.','MarkerSize',10)
I'm getting the error message;
Not enough input arguments. Error in confun (line 5) c(2) = abs(z8-trfu) - 0.217; Error in fmincon (line 633) [ctmp,ceqtmp] = feval(confcn{3},X,varargin{:}); Error in main (line 13) [x,fval] = fmincon(@objfun,x0,[],[],[],[],LB,UB,@confun,[],w1,w2); Caused by: Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.
I know fmincon
accepts constraint function with input in form of one vector with number of elements corresponding to number of constrained variables. But what I don't know is how to set all the input arguments as one vector.
Because the objective function is a bit bulky I separated it into different variables in objfun
. Do I have to expand the function when setting constraints or is there another way? I have done a lot of research and still not sure how this works.
Upvotes: 1
Views: 856
Reputation: 6015
Both functions that their handles are passed as fun
and nonlcon
arguments should take only one argument, which is the design vector (not the constant values of the problem). So you should construct new anonymous functions and pass them to fmincon
like this:
[x,fval] = fmincon(@(x)objfun(x, w1, w2),...
x0,[],[],[],[],LB,UB,@(x)confun(x,z8,y1,trfu));
But to do so, z8
, y1
, and trfu
should be assigned before the call to fmincon
. Since these values are actually calculated for each x
, I'm afraid you need to calculate them again in confun
. If this is not a very time consuming optimization, only move them to a third function and call it from both objfun
and confun
. Otherwise follow the method described here, to use values calculated in objective function, in constraint functions.
Upvotes: 1