Objective function is undefined at initial point. Fmincon cannot continue.

8 次查看(过去 30 天)
I am running an optimization using fmincon and I recieved the following error '' Objective function is undefined at initial point. Fmincon cannot continue.''
when the time step DT (which is in bold) is increased from 1 to 2 the code runs without any error but when set to 1, I am getting the error
please find part of the code below
data = xlsread('Power_curve.xlsx');
t = data(:,1);
Pl = data(:,2);
% Define time step for simulation.
DT = 1; % This can be increased to decrease computation time
% Define lower and upper bounds for Fuel Cell
PFC_lb = 0; % Lower bound for PFC
PFC_ub = 10000; % Upper bound for PFC
% Define lower and upper bounds for State of Charge
SOCmin = 0.2;
SOCmax = 0.7;
% Battery and Fuel Cell parameters
VFCoc = 62; % Fuel cell open circuit voltage in Volts
RFC = 0.06; % Fuel cell resistance in Ohms
RFCup = 10; % Fuel cell ramp up power in kW (value can be changed as required)
RFCdown = -10; % Fuel cell ramp down power in kW (value can be changed as required)
VBToc = 72; % Battery open circuit voltage in Volts
RBT = 0.06; % Battery resistance in Ohms
CBT = 45; % Capacity of the battery in Ah
PBT_max = 15000;
% Initial SOC of the battery
SOC0 = 0.5; % Initial chosen status of battery
mu = 0.6;
%% We will have three variables:
% Fuel Power: PFC
% Battery Power: PBT
% Battery SOC: SOC
% Construct vectors of lower and upper bounds for fmincon
lb = [PFC_lb;-PBT_max;SOCmin];
ub = [PFC_ub;PBT_max;SOCmax];
Aeq = [1, 1, 0]; % Equality matrix for fmincon. This ensures that PFC + PBT = Pload - refer report
options = optimset('display', 'off'); % Display of the system for each time step is off
n = 1;
for i = 1:DT:length(t)
ti = i % Display current time
Pload(n) = Pl(i);
if i == 1
SOCold = SOC0;
PFCold = 0;
PBTold = 0;
else
SOCold = SOC(n-1);
PFCold = PFC(n-1);
PBTold = PBT(n-1);
end
x0 = [3000,5000,SOCold]; % Initial values
beq = Pload(n);
[x,fval,exitflag] = fmincon('Objectivefunction', x0, [], [], Aeq, beq, lb, ub, 'Nonlinearequations', options, VFCoc, RFC, VBToc, RBT, CBT, SOCold, ti, DT, PFCold, RFCup, RFCdown);
% Saving the results
PFC(n) = x(1);
PBT(n) = x(2);
SOC(n) = x(3);
% H(n) = fval; % Hydrogen consumption
n = n +1;
end
%Objective function is
function f = Objectivefunction(x, VFCoc, RFC, VBToc, RBT, CBT, SOCold,SOC_min, SOC_max,mu, ti, DT, PFCold, PBTold, RFCup, RFCdown)
% Minimize Hydrogen consumption
PFC = x(1);
PBT = x(2);
lambda = (1-2*mu*((SOCold -0.5*(SOC_max-SOC_min))/(SOC_max-SOC_min)));
f = PFC + PBT*lambda;
end

回答(1 个)

Walter Roberson
Walter Roberson 2022-11-5
ti becomes SCO_min and DT becomes SCO_MAX in the function. The function computes max minus min and divides by that. Which is a problem if the min and max are the same.
The first ti is 1. When DT is 1 then those are the same and lead to the division by 0
  7 个评论
Torsten
Torsten 2022-11-5
In the line
[x,fval,exitflag] = fmincon('Objectivefunction', x0, [], [], Aeq, beq, lb, ub, 'Nonlinearequations', options, VFCoc, RFC, VBToc, RBT, CBT, SOCold, ti, DT, PFCold, RFCup, RFCdown);
you tell fmincon to call your objective function as
f = Objectivefunction(x,VFCoc, RFC, VBToc, RBT, CBT, SOCold, ti, DT, PFCold, RFCup, RFCdown)
but your objective function in the code you posted is
function f = Objectivefunction(x, VFCoc, RFC, VBToc, RBT, CBT, SOCold,SOC_min, SOC_max,mu, ti, DT, PFCold, PBTold, RFCup, RFCdown)
so should be called as
f = Objectivefunction(x, VFCoc, RFC, VBToc, RBT, CBT, SOCold,SOC_min, SOC_max,mu, ti, DT, PFCold, PBTold, RFCup, RFCdown)
I don't know which of the two versions is the correct one.
Further, the additional arguments should be directly included as
[x,fval,exitflag] = fmincon(@(x)Objectivefunction(x,VFCoc, RFC, VBToc, RBT, CBT, SOCold,SOC_min, SOC_max,mu, ti, DT, PFCold, PBTold, RFCup, RFCdown), x0, [], [], Aeq, beq, lb, ub, @(x)Nonlinearequations(x,VFCoc, RFC, VBToc, RBT, CBT, SOCold,SOC_min, SOC_max,mu, ti, DT, PFCold, PBTold, RFCup, RFCdown), options);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by