How to fix NLMPC validateFcn() error in input arguments

조회 수: 3 (최근 30일)
Lechuan Piao
Lechuan Piao 2025년 7월 14일
답변: Bay Jay 2025년 7월 24일
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
Bay Jay
Bay Jay 2025년 7월 24일
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;

댓글을 달려면 로그인하십시오.

채택된 답변

Bay Jay
Bay Jay 2025년 7월 24일
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개)

카테고리

Help CenterFile Exchange에서 Statics and Dynamics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by