⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cnf_sfun_nl.m

📁 hard disk drive servo control tools, it is very helpful
💻 M
字号:
function [sys,x0,str,ts] = cnf_sfun_nl(t,x,u,flag,para)
% This S function produce a vector output based on the expression of para. 
% It is called from the Simulink model cnf_sim.mdl
% The general structure of an S-function is used

switch flag,
case 0, % Initialization %
    [sys,x0,str,ts]=mdlInitializeSizes(t,x,u,para);
case 1,% Derivatives %
    % Do nothing not used    
    % sys=mdlDerivatives(t,x,u);
case 2,  % Update %
     % sys = mdlUpdate(t,x,u,para);
      sys=[];   
case 3,  % Outputs %
     sys=mdlOutputs(t,x,u,para);
case 4,  % GetTimeOfNextVarHit %
    % Do nothing not used
    % sys=mdlGetTimeOfNextVarHit(t,x,u);
case 9,  % Terminate %
    sys=mdlTerminate(t,x,u);
otherwise  % Unexpected flags %
    error(['Unhandled flag = ',num2str(flag)]);
end
% end sfuntmpl

%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
function [sys,x0,str,ts]=mdlInitializeSizes(t,x,u,para)
% call simsizes for a sizes structure, fill it in and convert it to a
% sizes array.
% Note that in this example, the values are hard coded.  This is not a
% recommended practice as the characteristics of the block are typically
% defined by the S-function parameters.

ud = get_param('cnf_sim/Get Data','UserData');
cnfdb = guidata(ud.Figure);
if cnfdb.System.QD_in==1   % NL-function dot(q)=f(y,q)
    nn=length(cnfdb.System.Q0);
else
    nn=1;
end    
[nh,nx]=size(cnfdb.System.C2);

sizes = simsizes;
sizes.NumContStates  = 0;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = nn;  % the dimension of q
sizes.NumInputs      = nh + nx + nn;  % the dimension of measurement output y + nn
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;   % at least one sample time is needed
sys = simsizes(sizes);
% initialize the initial conditions
x0  = [];
% str is always an empty matrix
str = [];
% initialize the array of sample times
ts  = [0 0];
% end mdlInitializeSizes

%=============================================================================
% mdlUpdate     flag = 2
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=============================================================================
function [sys] = mdlUpdate(t,x,u,para)		%flag = 2
% sys = eval(para);
sys = [];

%=============================================================================
% mdlOutputs     flag = 3
%=============================================================================
function sys=mdlOutputs(t,x,u,para)

ud = get_param('cnf_sim/Get Data','UserData');
cnfdb = guidata(ud.Figure);
% nh = size(cnfdb.System.C2, 1);
% h = u(1:nh);
[nh,nx]=size(cnfdb.System.C2);
x = u(1:nx);
h = u(nx+1:nx+nh);
q = u(nx+nh+1:end);
sys = eval(para);
% end mdlOutputs


%=============================================================================
% mdlTerminate    flag = 9
% Perform any end of simulation tasks.
%=============================================================================
function sys=mdlTerminate(t,x,u)
sys = [];
% end mdlTerminate

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -