错误:未定义与 'struct' 类型的输入参数相对应的函数 'minus'。
Error in nmpcblock_interface.m (line 165)
throw(ME)
'test100load/Nonlinear MPC Controller/MPC/NLMPC' (line 24) 中出现错误
#代码
nx = 2;%状态
nu = 2;%输入
ny = 2;%输出
nlobj = nlmpc(nx, ny, nu);
%定义输入输出名称
nlobj.States(1).Name = 'speed';
nlobj.States(2).Name = 'temp';
nlobj.MV(1).Name = 'fuel';
nlobj.MV(1).Name = 'IGV';
% Set sample time, prediction horizon, and control horizon
Ts = 0.05;
p = 4;
nlobj.Ts = Ts;
nlobj.PredictionHorizon = p;
nlobj.ControlHorizon = 2;
%约束
nlobj.MV(1).Min = 2;
nlobj.MV(1).Max = 10;
nlobj.MV(2).Min = 0;
nlobj.MV(2).Max = 1;
% Set the weights and other parameters...权重
nlobj.Weights.ManipulatedVariables = [1 0;0 1]; % 输入权重
nlobj.Weights.ManipulatedVariablesRate = [0.1 0;0 0.1]; % 输入变化率权重
nlobj.Weights.OutputVariables = [10 0;0 10]; % 输出跟踪权重
%Specify the state function for the controller_模型状态方程
nlobj.Model.StateFcn = "gasmodel";
nlobj.Model.IsContinuousTime = true;
% Specify the output function for the controller._模型输出
nlobj.Model.OutputFcn = "gasmodel";
%Speficy optimization paremeters.
weights = [1; 1]; % 根据需要调整
nlobj.Optimization.CustomCostFcn = "cost";
nlobj.Optimization.ReplaceStandardCost = false;
%nlobj.Optimization.CustomIneqConFcn = "IneqConFunction";
nlobj.Optimization.SolverOptions.MaxIter = 1000;
nlobj.Optimization.UseSuboptimalSolution = true;
x0 = [0;0];
u0 = [0;0];
validateFcns(nlobj,x0,u0);
% Open the Simulink model
mdl = 'test100load'; % Ensure this is the correct model name
open_system(mdl);
% Check that the nlobj exists in the base workspace
assignin('base', 'nlobj', nlobj);
function cost = cost(N, x, u, ref)
% N: 预测步数
% xopt: 预测的状态(状态包含 x1 和 x2)
% uopt: 预测的控制输入(控制输出 u1 和 u2)
% ref: 参考轨迹
% weights: 权重向量或矩阵
% 实现自定义成本的计算
cost = 0;
weights=[1; 0.1];
% 逐步计算与参考信号的偏差的加权平方和
for k = 1:N
stateError = x(:, k) - ref(:, k); % 状态偏差
inputError = u(:, k); % 输入偏差,假设期望输入为0
cost = cost + weights(1) * (stateError' * stateError) + weights(2) * (inputError' * inputError);
end
end