Filter löschen
Filter löschen

S-function error

1 Ansicht (letzte 30 Tage)
iman
iman am 4 Jun. 2012
Hi,Help please,when i try to use the follow s-function in a simulink blocket, i obtain this error:
Error in 'NARMA/S-Function' while executing MATLAB S-function 'NARMASYSTEM', flag = 3 (output), at time 0.0.
Too many output arguments.
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 linear 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 = 1;
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=x;
% End of mdlOutputs.

Akzeptierte Antwort

Kaustubha Govind
Kaustubha Govind am 4 Jun. 2012
Iman: I would recommend looking at some of Level-1 S-function demos. Type "sfundemos" at the MATLAB prompt and select MATLAB file S-functions -> Level-1 MATLAB S-functions. Please use these as a starting point and compare your S-function code with these to see if you can figure out what is going on.
You have been asking a slew of questions without any indication of having debugged/understood the issue yourself, so I'm trying to teach you to fish here. :)
Hint: Look at how mdlOutputs is called in the examples.
  3 Kommentare
Kaustubha Govind
Kaustubha Govind am 4 Jun. 2012
Iman: I see. I thought that you might be new to S-functions because the errors you are running into are basic syntax issues. I am surprised that they make a difference between continuous and discrete states systems - I would expect the three questions you have faced so far to cause similar issues if used inside continuous-time Level-1 MATLAB S-functions.
In any case, please feel free to ask in a comment if my answer didn't help you.
iman
iman am 4 Jun. 2012
Dear Kaustubha,
I really appreciate your response to me and I would like to thank you for your time and patience :).
I have received your comment and I thank you for your help so very much :) :).
Finally I found my errors and I corrected my code and I see that does not exist difference between continuous and discrete systems ;) ;) ;) ;).
In future, since in this semester I have a lot of projects related to MATLAB especially to simulink,I will communicate with you.
Best wishes
byeeeeeeeeee

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Block and Blockset Authoring finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by