How to fix NLMPC validateFcn() error in input arguments
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Lechuan Piao
am 14 Jul. 2025
Beantwortet: Bay Jay
am 24 Jul. 2025
Hello all,
I am using MPC to design Energy Management System (EMS) of an energy storage, charging and discharging (MV) at differnt time of the day, looking for arbitrage based on load and electricity price (MD of my MPC) variation.
The state x and output y are the State of Charge (SOC). (OutputFcn as in y = x)
The objective function is customized cost (forecasted for the next 24 hours).
I ran into validateFcn error.
Error using nlmpc/validateFcns (line 179)
Expecting 6 input arguments but "Model.StateFcn" appears to take 4 inputs.
Error in MPC_Toolbox (line 68)
validateFcns(nlobj, x0, mv, md, params);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Based on what I have read here https://www.mathworks.com/help/mpc/ug/specify-cost-function-for-nonlinear-mpc.html , I assume the inputs of State Fuction are required to have 3 inputs maximum.

Both my nlmpc code and stateFun are attached.
Thank you for your help. It's my 1st question here so please let me know if I need to provide any additional files.
function Xnext = myStateFunction(X, U, params)
% params = [Ts; eta_dis; eta_cha; E_rated]
Ts = params{1};
eta_dis = params{2};
eta_cha = params{3};
E_rated = params{4}; % rated energy of BESS
U_mv = U(1);
if U_mv >= 0 % discharging
deltaE = -U_mv * Ts / (eta_dis * E_rated);
else % charging
deltaE = -U_mv * Ts * eta_cha / E_rated;
end
% next SOC
Xnext = X + deltaE;
end
clear;clc;
%% ————————parameters————————
Ts = 5/60; % 5min
eta_dis = 0.95;
eta_cha = 0.95;
eta_trans = 0.98;
E_rated = 1000/1000; % 1 MWh
u_min = -0.4; % 400 kW (charge)
u_max = 0.4; % 400 kW (discharge)
% du_min = -0.1;
% du_max = + 0.1;
SOC_max = 0.9;
SOC_min = 0.1;
Np = 288;
Nc = 288;
%% ———————— nlmpc object ————————
nx = 1; % state:SOC
ny = 1; % ov:SOC
% creation of nlmpc
nlobj = nlmpc(nx, ny, 'MV', 1, 'MD', 2: 577);
% specify time
nlobj.Ts = Ts;
nlobj.PredictionHorizon = Np;
nlobj.ControlHorizon = Nc;
%% ———————— Specify Model Function ————————
nlobj.Model.StateFcn = "myStateFunction";
nlobj.Model.OutputFcn = "myOutputFunction";
nlobj.Model.NumberOfParameters = 4;
nlobj.Model.IsContinuousTime = false;
%% ———————— Constraints ————————
% MV(P_Bess) limits
nlobj.ManipulatedVariables.Max = u_max;
nlobj.ManipulatedVariables.Min = u_min;
% MV rate limits
% nlobj.ManipulatedVariables.RateMax = u_max;
% nlobj.ManipulatedVariables.RateMin = u_min;
%
nlobj.ManipulatedVariables.Name = "P_Bess";
% SOC limits (states)
nlobj.States.Max = SOC_max;
nlobj.States.Min = SOC_min;
% SOC limits(output variables)
nlobj.OutputVariables.Max = SOC_max;
nlobj.OutputVariables.Min = SOC_min;
nlobj.Optimization.CustomIneqConFcn = "myIneqConFunction";
%% ———————— Customized Cost ————————
nlobj.Optimization.ReplaceStandardCost = true;
nlobj.Optimization.CustomCostFcn = @(x,u, e, data)econCost(X, U, e, data);
%% ———————— validation ————————
x0 = 0.5; % initial SOC
params = {Ts, eta_dis, eta_cha, E_rated};
mv = -u_min +(u_max - u_min) * rand(1);
md_load = getLoad_v2(1, Ts, 1, "weekday") ;
md_price = getPrice(1);
md = [md_load; md_price]';
validateFcns(nlobj, x0, mv, md, params);
1 Kommentar
Bay Jay
am 24 Jul. 2025
You stated 4 parameters.
Matlab expect you to pass the parameters as comma seperated at the last entry to the validatfcn {param1, param2, param3,param4}
In the statefunction, you can then assgn them as below
Ts = param1;
eta_dis = param2;
eta_cha = param3;
E_rated = param4;
Akzeptierte Antwort
Bay Jay
am 24 Jul. 2025
You stated 4 parameters.
Matlab expect you to pass the parameters as comma seperated at the last entry to the validatfcn {param1, param2, param3,param4}
In the statefunction, you can then assgn them as below
Ts = param1;
eta_dis = param2;
eta_cha = param3;
E_rated = param4;
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Statics and Dynamics finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!