S函数程序为
function calculate_gain_sfunc(block)
setup(block);
end
function setup(block)
% Register the number of ports
block.NumInputPorts = 3; % Q1, Q2, and F2(t)
block.NumOutputPorts = 2; % y1 and y2
% Set up the input and output ports
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
block.InputPort(1).Dimensions = 1; % Q1
block.InputPort(2).Dimensions = 1; % Q2
block.InputPort(3).Dimensions = 1; % F2
block.OutputPort(1).Dimensions = 1; % y1
block.OutputPort(2).Dimensions = 1; % y2
% Register the parameters
block.NumDialogPrms = 9; % J1, L1, R1, AE, F1, J2, L2, R2
% Set the sample time
block.SampleTimes = [0 0];
% Specify the block simStateCompliance
block.SimStateCompliance = 'DefaultSimState';
% Register the methods
block.RegBlockMethod('Outputs', @Output); % Register the output method
end
function Output(block)
% Get the inputs
Q1 = block.InputPort(1).Data;
Q2 = block.InputPort(2).Data;
F2 = block.InputPort(3).Data;
% Get the parameters
J1 = block.DialogPrm(1).Data;
L1 = block.DialogPrm(2).Data;
R1 = block.DialogPrm(3).Data;
AE = block.DialogPrm(4).Data;
F1 = block.DialogPrm(5).Data;
J2 = block.DialogPrm(6).Data;
L2 = block.DialogPrm(7).Data;
R2 = block.DialogPrm(8).Data;
% Compute the gains
gain1 = J1 * L1 / (R1 * (AE - F1));
gain2 = J2 * L2 / (R2 * (F2 - AE));
% Compute the outputs
y1 = gain1 * Q1;
y2 = gain2 * Q2;
% Set the outputs
block.OutputPort(1).Data = y1;
block.OutputPort(2).Data = y2;
end 模型如上图所示
simulink运行结果为:
The specified MATLAB File 'calculate_gain_sfunc' in 'sys/S-Function1' is not a valid Level 1 S-Function. The number of input and/or output arguments is not valid. A likely cause for this error is that the name of a level-2 MATLAB S-function has been specified in this block (which supports only level-1 MATLAB S-functions). Use the 'Level-2 MATLAB S-Function' block from the Simulink library