Failure in initial objective function evaluation. FMINCON cannot continue in s function

I am using a level 2 s function to perform fmincon and I need to limit the rate of change of output. I have stored the output using Dwork vectors. I want to create a constraint where the difference between the current output value and previous output value should be less than 20.
function ECMSsf2(block)
setup(block);
%endfunction
function setup(block)
%% Register number of input and output ports
block.NumInputPorts = 2;
block.NumOutputPorts = 3;
%% Setup functional port properties to dynamically
%% inherited.
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
block.InputPort(1).DirectFeedthrough = false;
%% Set block sample time to inherited
block.SampleTimes = [-1 0];
%% Set the block simStateCompliance to default (i.e., same as a built-in block)
block.SimStateCompliance = 'DefaultSimState';
%% Register methods
block.RegBlockMethod('PostPropagationSetup',@DoPostPropSetup);
block.RegBlockMethod('InitializeConditions',@InitConditions);
block.RegBlockMethod('SetInputPortSamplingMode', @SetInpPortFrameData);
block.RegBlockMethod('Outputs', @Output);
block.RegBlockMethod('Update', @Update);
function DoPostPropSetup(block)
%% Setup Dwork
block.NumDworks = 1;
block.Dwork(1).Name = 'x0';
block.Dwork(1).Dimensions = 1;
block.Dwork(1).DatatypeID = 0;
block.Dwork(1).Complexity = 'Real';
block.Dwork(1).UsedAsDiscState = true;
end
function InitConditions(block)
% Initialize Dwork
block.Dwork(1).Data = 0;
end
function SetInpPortFrameData(block, idx, fd)
block.InputPort(idx).SamplingMode = fd;
block.InputPort(idx).SamplingMode = fd;
block.OutputPort(1).SamplingMode = fd;
block.OutputPort(2).SamplingMode = fd;
block.OutputPort(3).SamplingMode = fd;
end
function Output(block)
%constants initialization
Pbatt_char=5000;
SOC_min=0.2;
SOC_max=0.90;
Pfc_min=1000;
Pfc_max=10000;
Pbatt_max=17000;
%define Matrix Aeq
Aeq=[0 1 0;1 0 1];
%define Matrix beq
mu = 0.6;
beq=[(1-2*mu*((block.InputPort(2).Data-0.5*(SOC_max+SOC_min))/(SOC_max+SOC_min))); block.InputPort(1).Data];
%define boundary conditions
lb=[Pfc_min, 0, -Pbatt_char];
ub=[Pfc_max, 100, Pbatt_max];
%defining initial conditions
x0 = [0 0 0];
options = optimoptions('fmincon','Algorithm','active-set','Display','off','MaxFunctionEvaluations',1000,'MaxIterations',100);
[y,fval] = fmincon('OF_ECMS',x0,[],[],Aeq,beq,lb,ub,'Nonlinearequations',options); %#ok<*ASGLU>
Pfc=y(1); Pbatt=y(3); alpha=y(2);
block.OutPort(1).Data = Pfc;
block.OutPort(2).Data = Pbatt;
block.OutPort(3).Data = alpha;
end
function Update(block)
block.Dwork(1).Data = block.Outport(1).Data;
end
end
end
and the objective function is
function f = OF_ECMS(y(1),y(2),y(3));
f = (y(1)+y(2)*y(3));
end
and the constraints are
function [c, ceq] = Nonlinearequations(y,PFCold)
c = [];
ceq = [];
PFCold = block.Dwork(1).Data;
% Ramp constraints which are inequality constraints
c(1) = PFCold - y(1) + 20;
c(2) = y(1) - PFCold - 20;
end

 Réponse acceptée

Both your objective function and constraints are linear, so you should be using linprog. In fact, you nonlinear constraints are simple bounds on y(1). So, do instead,
lb=[Pfc_min, 0, -Pbatt_char];
ub=[Pfc_max, 100, Pbatt_max];
PFCold = block.Dwork(1).Data;
lb(1)=max(lb(1), PFCold-20);
ub(1)=min(ub(1), PFCold+20);
[y,fval] = linprog([1,1,1], [],[],Aeq,beq,lb,ub);

5 commentaires

As a side note, however, if you were to use your original fmincon attempt, it should look like this:
nonlcon=@(y) Nonlinearequations(y,block);
[y,fval] = fmincon('OF_ECMS',x0,[],[],Aeq,beq,lb,ub,nonlcon,options);
function f = OF_ECMS(y) %<----fix
f = (y(1)+y(2)*y(3));
end
function [c, ceq] = Nonlinearequations(y,block)%<----fix
c = [];
ceq = [];
PFCold = block.Dwork(1).Data;
c(1) = PFCold - y(1) - 20; %<----change to -20
c(2) = y(1) - PFCold - 20;
end
I really recommend using
[y,fval] = fmincon(@OF_ECMS,x0,[],[],Aeq,beq,lb,ub,nonlcon,options);
instead of the quoted name 'OF_ECMS' . Using the quoted name is supported by fmincon, but it has the disadvantage that the function must exist on the MATLAB path -- so in the case of providing your own function, that there has to be a separate .m function by that name. When you use a quoted name, you cannot refer to a function defined inside the same script or function as your other code.
Thank you for your reply. There is a requirement that I have to use fmincon. I have seperate m files for constraint and the objective function. I would like to define both functions in the same script. Are there any examples. Thank you in advance
I am getting the following error
Undefined function 'OutPort' for input arguments of type 'Simulink.MSFcnRunTimeBlock'
I would like to define both functions in the same script. Are there any examples.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by