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

2 views (last 30 days)
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

Accepted Answer

Matt J
Matt J on 8 Jan 2023
Edited: Matt J on 8 Jan 2023
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 Comments
aditya deepak
aditya deepak on 10 Jan 2023
I am getting the following error
Undefined function 'OutPort' for input arguments of type 'Simulink.MSFcnRunTimeBlock'

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!