Simulink 无法确定块 'MATLAB Function' 的输出的大小 and/or 类型

Simulink cannot determine sizes and/or types of the outputs for block 'MATLAB Function'

我有一个小型的 Simulink 模型如下:

以及相关代码:

function [xdot,y] = fcn(x,u)

    % define your constants
    g = 9.81;
    m = 0.05;
    R = 1;
    L = 0.01;
    C = 0.0001; 
    x1 = 0.012;
    x2 = 0;
    x3 = 0.84;

    % nonlinear set of equations
    xdot = [x2; g-((C/m)*(x3/x1)^2); -((R/L) +(((2*C)/L)*(((x2*x3)/((x1)^2)))))] + [0;0;1/L]*u;

    y = x';   

然而,当我尝试 运行 时,Simulink 生成以下错误:

Inferred size ('[1 3]') for data 'y' does not match specified size ('scalar'). Component:MATLAB Function | Category:Coder error Simulink cannot determine sizes and/or types of the outputs for block 'MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.

我在一些文档中搜索了 variable size inputs and outputs,选中了可变大小复选框并输入了上限 [1 3]。

当我再次尝试 运行 时,我得到:

Expression '[1 3]' for maximum of data 'y' must evaluate to a scalar.

我不确定如何解决这个错误。我也查看了 here,但仍然无法正常工作。 任何帮助将不胜感激。

我认为您必须像您尝试的那样,在端口和数据管理器中为功能块的所有输入和输出设置大小。

y 的大小设置为 [1 3],将 x 设置为 [3 1],将 xdot 设置为 [3 1]

此外,我认为您的非线性状态 space 存在错误,因为您的 'A' 矩阵现在是常数。因此,要使它们依赖于当前状态,请将 x1 等的声明替换为:

x1 = x(1);
x2 = x(2);
x3 = x(3); 

我假设您现在拥有的值是微分方程的初始条件,您必须在积分器块中设置这些条件。

您可能会发现您的代码可以通过进行以下更改来工作,

  • 您没有可变大小的数据,可以将所有这些选项设置回默认值。

  • (如其他答案之一所示,)您需要更改输入 x 进入方程式的方式,因为目前它不用于计算 xdot.

  • x1x2x3 移动为 Integrator 块的初始条件的 3×1 向量。 (假设这是他们真正的样子。)

通过这些更改,块应该检测到 x 信号是 3×1(因为 Integrator 块有 3 个初始值),因此你的 xdot输出为 3×1,而您的 y 输出为 1×3。

为了安全起见,您也可以考虑将以下两行放在函数的顶部。

xdot = zeros(3,1);
y = zeros(1,3);

这些行将在块初始化期间用于告诉编译器输出信号的大小。

注意:为什么要使 y 成为 1×3 向量?这是非常不寻常的,我怀疑你真的希望它是一个 3×1 向量(如果你想输出状态) 你应该总结 x 得到 y 在这种情况下它只是一个标量。

与上述内容无关,但您也可以考虑使块的 常量 参数,以便您可以在不编辑函数的情况下更改它们。