- #1

HFAI

- 1

- 0

Hi all,

I am modeling a separator model with pressure and water level as 2 variables.

I first made an m-file(hamda.m) with ode23tb, which is working( 4 plots: pressure, water level, flow of liq output and flow of gas output.

then I used an level 1 s-function( gabileh) applying the m-file(hamda.m) I created as fellow

function [sys,x0,str,ts,simStateCompliance] = gabileh(t,x,u,flag,varargin)

% M-File S-Function implementing

QLi = u(1); Qgi = u(2); kv1 = u(3); kv2 = u(4);

switch flag,

%%%%%%%%%%%%%%%%%%

% Initialization %

%%%%%%%%%%%%%%%%%%

case 0,

[sys,x0,str,ts, simStateCompliance] = mdlInitializeSizes(t,x,u,flag);

%%%%%%%%%%%%%%%

% Derivatives %

%%%%%%%%%%%%%%%

case 1,

sys = mdlDerivatives(t,x,u,QLi,Qgi,kv1,kv2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Update, Output, and Terminate %

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

case {2, 4, 9},

sys = []; % do nothing

%%%%%%%%%%

% Output %

%%%%%%%%%%

case 3

sys = mdlOutputs(t,x,u);

otherwise

DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));

end

%

%===================================================================================

% mdlInitializeSizes

% Return the sizes, initial conditions, and sample times for the S-function.

%===================================================================================

%

function [sys, x0,str,ts] = mdlInitializeSizes

sizes = simsizes;

sizes.NumContStates = 2;

sizes.NumDiscStates = 0;

sizes.NumOutputs = 4;

sizes.NumInputs = 4;

sizes.DirFeedthrough = 0;

sizes.NumSampleTimes = 1;

sys = simsizes(sizes);

x0 = [];

str = [];

ts = [0 0]; % continuous sample time: [period, offset]

% Specify the block simStateCompliance. The allowed values are:

% 'UnknownSimState', < The default setting; warn and assume DefaultSimState

simStateCompliance = 'UnknownSimState';

% end mdlInitializeSizes

%

%===================================================================================

% mdlDerivatives

%

%===================================================================================

%

function sys = mdlDerivatives(t,x,u,varargin)

QLi = u(1); Qgi = u(2); kv1 = u(3); kv2 = u(4);

sys = hamda(t,x,u);

% end mdlInitializeSizes

%=============================================================================

% mdlOutputs

% Return the output vector for the S-function

%=============================================================================

%

function sys = mdlOutputs(t,x,u)

sys =Qgt;

sys =QLt;

sys = x;

% end mdlOutputs

when I used the level-1 s-function in a simulink with 4 inputs and 4 ouputs,

I keep having the same error:

Error in 'seprig/M-file S-Function1' while executing M-File S-function 'gabileh', flag = 0 (initialize), at start of simulation. MATLAB error message:

Attempted to access u(1); index out of bounds because numel(u)=0.

please help, thanks in adavance

I am modeling a separator model with pressure and water level as 2 variables.

I first made an m-file(hamda.m) with ode23tb, which is working( 4 plots: pressure, water level, flow of liq output and flow of gas output.

then I used an level 1 s-function( gabileh) applying the m-file(hamda.m) I created as fellow

function [sys,x0,str,ts,simStateCompliance] = gabileh(t,x,u,flag,varargin)

% M-File S-Function implementing

QLi = u(1); Qgi = u(2); kv1 = u(3); kv2 = u(4);

switch flag,

%%%%%%%%%%%%%%%%%%

% Initialization %

%%%%%%%%%%%%%%%%%%

case 0,

[sys,x0,str,ts, simStateCompliance] = mdlInitializeSizes(t,x,u,flag);

%%%%%%%%%%%%%%%

% Derivatives %

%%%%%%%%%%%%%%%

case 1,

sys = mdlDerivatives(t,x,u,QLi,Qgi,kv1,kv2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Update, Output, and Terminate %

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

case {2, 4, 9},

sys = []; % do nothing

%%%%%%%%%%

% Output %

%%%%%%%%%%

case 3

sys = mdlOutputs(t,x,u);

otherwise

DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));

end

%

%===================================================================================

% mdlInitializeSizes

% Return the sizes, initial conditions, and sample times for the S-function.

%===================================================================================

%

function [sys, x0,str,ts] = mdlInitializeSizes

sizes = simsizes;

sizes.NumContStates = 2;

sizes.NumDiscStates = 0;

sizes.NumOutputs = 4;

sizes.NumInputs = 4;

sizes.DirFeedthrough = 0;

sizes.NumSampleTimes = 1;

sys = simsizes(sizes);

x0 = [];

str = [];

ts = [0 0]; % continuous sample time: [period, offset]

% Specify the block simStateCompliance. The allowed values are:

% 'UnknownSimState', < The default setting; warn and assume DefaultSimState

simStateCompliance = 'UnknownSimState';

% end mdlInitializeSizes

%

%===================================================================================

% mdlDerivatives

%

%===================================================================================

%

function sys = mdlDerivatives(t,x,u,varargin)

QLi = u(1); Qgi = u(2); kv1 = u(3); kv2 = u(4);

sys = hamda(t,x,u);

% end mdlInitializeSizes

%=============================================================================

% mdlOutputs

% Return the output vector for the S-function

%=============================================================================

%

function sys = mdlOutputs(t,x,u)

sys =Qgt;

sys =QLt;

sys = x;

% end mdlOutputs

when I used the level-1 s-function in a simulink with 4 inputs and 4 ouputs,

I keep having the same error:

Error in 'seprig/M-file S-Function1' while executing M-File S-function 'gabileh', flag = 0 (initialize), at start of simulation. MATLAB error message:

Attempted to access u(1); index out of bounds because numel(u)=0.

please help, thanks in adavance

Last edited: