Errors with s-function level 1 in simulink

In summary, the author is modeling a separator with pressure and water level as two variables. They first created 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, they used an level-1 s-function (gabileh) to apply the m-file (hamda.m) and created a fellow function [sys,x0,str,ts,simStateCompliance] = gabileh(t,x,u,flag,varargin).
  • #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
 
Last edited:
Physics news on Phys.org
  • #2
. </code><code>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.This happens because you are trying to access the first element of the input vector 'u', which is not defined yet. You should define it in your initialization step (flag 0):switch flag, %%%%%%%%%%%%%%%%%% % Initialization % %%%%%%%%%%%%%%%%%% case 0, [sys,x0,str,ts, simStateCompliance] = mdlInitializeSizes(t,x,u,flag); QLi = u(1); Qgi = u(2); kv1 = u(3); kv2 = u(4); %%%%%%%%%%%%%%% % 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));endHope this helps.</code>
 

FAQ: Errors with s-function level 1 in simulink

1. What is an s-function level 1 in Simulink?

An s-function level 1 in Simulink is a type of block used to create custom functions or algorithms in Simulink. It allows users to incorporate their own code written in C or MATLAB into their Simulink models.

2. What are some common errors encountered with s-function level 1 in Simulink?

Some common errors with s-function level 1 in Simulink include syntax errors in the custom code, incorrect data types being passed between the s-function and other blocks, and issues with the s-function's block parameters.

3. How can I troubleshoot errors with s-function level 1 in Simulink?

To troubleshoot errors with s-function level 1 in Simulink, you can start by double-checking your custom code for any syntax errors or incorrect data types. You can also use the Simulink debugger to step through your code and identify any potential issues with block parameters or data passing between blocks.

4. Can I use an s-function level 1 in Simulink with my own custom library?

Yes, you can use an s-function level 1 in Simulink with your own custom library. You can either add your library to the Simulink Library Browser or directly add it to your custom code using the #include directive.

5. Are there any alternatives to using s-function level 1 in Simulink?

Yes, there are alternatives to using s-function level 1 in Simulink. Simulink provides other types of blocks, such as MATLAB Function blocks and Stateflow blocks, which can also be used to incorporate custom code into your model. Additionally, Simulink also has built-in blocks for common functions, so you may not always need to use an s-function.

Back
Top