errors with s-function level 1 in simulink


by HFAI
Tags: errors, sfunction, simulink
HFAI
HFAI is offline
#1
Sep25-11, 09:34 PM
P: 1
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
Phys.Org News Partner Science news on Phys.org
Cougars' diverse diet helped them survive the Pleistocene mass extinction
Cyber risks can cause disruption on scale of 2008 crisis, study says
Mantis shrimp stronger than airplanes

Register to reply

Related Discussions
S-function! in Simulink/Matlab Differential Equations 0
Need help on Simulink Programming & Computer Science 1
Simulink help Mechanical Engineering 6
SIMULINK - need help Electrical Engineering 0