필터 지우기
필터 지우기

S-function error

조회 수: 6 (최근 30일)
iman
iman 2012년 6월 4일
hi guys when i try to use the follow s-function in a simulink blocket, i obtain this error:
Sizes vector returned by MATLAB S-function 'NARMASYSTEM' in 'NARMA/S-Function' must be a real vector consisting of integer value of length 7
This is the function code:
function [sys,x0,str,ts] = NARMASYSTEM(t,x,u,flag)
%M-file S-function for defining a discrete system.
% This S-function implements discrete equations
% Generate a discrete nonlinear system:
a1=0.2;
a2=0.1;
g1=1;
g2=0.2;%=-0.2
b1=0.3;
b2=-0.6;
switch flag,
case 0
[sys,x0,str,ts] = mdlInitializeSizes(a1,a2,g1,g2,b1,b2); % Initialization
case 2
[sys,x0,str,ts] = mdlUpdates(t,x,u,a1,a2,g1,g2,b1,b2); % Update discrete states
case 3
[sys,x0,str,ts] = mdlOutputs(t,x,u,a1,a2,g1,g2,b1,b2); % Calculate outputs
case {1, 4, 9} % Unused flags
sys = [];
otherwise
error(['unhandled flag = ',num2str(flag)]); % Error handling
end
% End of dsfunc.
%==============================================================
% Initialization
%==============================================================
function [sys,x0,str,ts] = mdlInitializeSizes(a1,a2,g1,g2,b1,b2)
% Call simsizes for a sizes structure, fill it in, and convert it
% to a sizes array.
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 2;
sizes.NumOutputs = 2;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 0; % Matrix D is non-empty.
sizes.NumSampleTimes = 0.05;
sys = simsizes(sizes);
x0 = [0 1]; % Initialize the discrete states.
str = []; % Set str to an empty matrix.
ts = [1 0]; % sample time: [period, offset]
% End of mdlInitializeSizes.
%==============================================================
% Update the discrete states
%==============================================================
function sys = mdlUpdates(t,x,u,a1,a2,g1,g2,b1,b2)
sys(1) =a1*x(1)*cos(x(1))+a2*(x(1)^2)/(1+x(1)^2)+g1*x(2) ;
sys(2)=b1*sin(x(1))+b2*(x(1)^3)/(2+x(1)^2)+g2*u(1)+u(2);
% End of mdlUpdate.
%==============================================================
% Calculate outputs
%==============================================================
function sys = mdlOutputs(t,x,u,a1,a2,g1,g2,b1,b2)
sys(1) =x(1);
sys(2)=x(2);
% End of mdlOutputs.

채택된 답변

Kaustubha Govind
Kaustubha Govind 2012년 6월 4일
NumSampleTimes cannot be a fractional value. You need to change:
sizes.NumSampleTimes = 0.05;
to:
sizes.NumSampleTimes = 1;
Btw, if you are writing a new S-function, I would highly recommend that you consider switching to Level-2 MATLAB S-functions instead (you are writing a Level-1 MATLAB S-function). Level-1 S-functions have been deprecated for a long time and do not support a wide range of Simulink features. Support for these S-functions may also be discontinued in a future release.
  댓글 수: 1
iman
iman 2012년 6월 4일
Thanks so much...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Block and Blockset Authoring에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by