Simulink error "variable-size matrix but its size is specified as inherited or fixed"

20 ビュー (過去 30 日間)
I'm building a MPC controller for a state space plant (, , with , , ).
I implemented the MPC as Matlab function, whose inputs are
  • enable (a flag that enables the controller. If it is equal to false, then the outputs are set equal to zero);
  • X_ref (a vector of size , with the reference state value for each step in the prediction horizon N);
  • U_ref (a vector of size , with the reference input value for each step in the prediction horizon N);
  • x_k ( a vector of the current state );
  • mpc (a structure with the data for the MPC implementation and all the preprocessed matrices; this is the only function input of type "parameter data"; it's value is read from matlab workspace).
The output is the vector , that is the control action for the plant. It is constructed in the following way
function u_k = mpc_controller(enable, X_ref, U_ref, x_k, mpc)
if enable
...
[z_opt, ~, flag] = quadprog((H+H')./2, f, Ain, bin, Aeq, beq, lb, ub, [], options);
else
idx = 1;
z_opt = zeros(mpc.nu, 1);
end
u_k = z_opt(idx:(idx+mpc.nu-1));
where mpc.nu is , while idx is a index appositely constructed in the code.
The same function works perfectly when running in Matlab, but once exported into simulink in the Matlab function, the following error rises:
Error: 'u_k' is inferred as a variable-size matrix, but its size is specified as inherited or fixed. Verify 'u_k' is defined in terms of non-tunable parameters, or select the 'Variable Size' check box and specify the upper bounds in the Size box.
I fixed the u_k value in the property inspector (inside the matlab function block) equal to mpc.nu, that is the actual length of the output, since it cannot have the feature "variable size" because one of the block in series with the matlab function block is a demux.
Do you have any idea on how to manage this problem? Thanks in advance

採用された回答

Shivam Gothi
Shivam Gothi 2025 年 1 月 28 日
Hello @Corby,
I was able to reproduce the issue faced by you, by creating a simple MATLAB function block inside simulink window:
The function code is:
function u_k = mpc_controller(enable, nu)
if (enable==1)
idx = 2;
z_opt = ones(nu+2, 1);
else
idx = 1;
z_opt = zeros(nu, 1);
end
u_k = z_opt(idx:(idx+nu-1));
When I simulated the model, it threw the following error:
This error is similiar to that reported by you. Also, you want to cascade a "demux" block at the output of the function block, due to which you cannot check the check-box "variable size". I found a work-around to resolve this error. It also allows you to add a "demux" block in cascade with the output.
ASSUMPTION: As stated by you, the size of "u_k" vector is known and is equal to "". I have assumed "" to be 9.
Define the size of output signal "u_k" by using "Function argument validation". Refer to the documentation:
You can define output argument validation by adding some lines of code inside your MATLAB function block as demonstrated below. Basically, this defines the size of output signal. (here it is defined as 9, because as I said, I have assumed nu=9).
function u_k = mpc_controller(enable, Nu)
arguments (Output)
u_k (9,1) double
end
if (enable==1)
idx = 2;
z_opt = ones(Nu+2, 1);
else
idx = 1;
z_opt = zeros(Nu, 1);
end
u_k = z_opt(idx:(idx+Nu-1));
After typing the above attached MATLAB code in "MATLAB function" block, I modified the simulink model to include a "demux" block cascaded to its output as shown in below figure:
After simulating the above model, no errors are thrown. Therefore, I found it to be working. I am attaching the model for your reference
I hope you find this information useful !

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeController Creation についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by