using level 1 S-function with simulink
이전 댓글 표시
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
답변 (1개)
Kaustubha Govind
2011년 9월 26일
The inputs to the S-function are not available at the time that mdlInitializeSizes is called (u is an empty variable - you could set a breakpoint and verify that). If you think about it, the stage flag=0 is the setup stage, so no inputs are available at this point. So the line:
QLi = u(1); Qgi = u(2); kv1 = u(3); kv2 = u(4);
errors out.
Move it under the "case 1" section, so it is executed only where available and required.
댓글 수: 4
Hassan
2011년 9월 26일
Kaustubha Govind
2011년 9월 27일
You function mdlInitializeSizes has no input arguments. Change:
[sys,x0,str,ts, simStateCompliance] = mdlInitializeSizes(t,x,u,flag);
to
[sys,x0,str,ts, simStateCompliance] = mdlInitializeSizes;
Hassan
2011년 10월 3일
Kaustubha Govind
2011년 10월 4일
You should perhaps run "dbstop if all error" and then run the Simulink model so that MATLAB automatically stops when an error occurs. You can debug the source of the error.
카테고리
도움말 센터 및 File Exchange에서 Simulink에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!