Use of Matlab · Simulink— 【Creation and Application of S Function】

(〇) Foreword

What is the
    S function : The S function is the English abbreviation of the system function, which refers to
how to write a S function using a design language (non-graphical way) to describe the S function :
    you can use Matlab language, you can also use other programming languages ​​into C , C ++, etc.
    I only introduce the commonly used MATLAB comes with "language" and S function template to write S function

(1) Write S function

①Enter the command in the command line window: >> edit sfuntmpl.m
    to open the template file
Insert picture description here
②The template file sfuntmpl.m contains a main function and six sub-functions

[1] Let's look at the main function
  first : the first sentence of the main function-the leading statement is:
function [sys, x0, str, ts, simStateCompliance] = sfuntmpl (t, x, u, flag)
     where fname is the function of the S function Name;
     input parameters t, x, y, flag are simulation time, state vector, input vector and call flag
     output parameter sys represents return parameters; x0 is the initial state value; str is set to empty array; ts is a Column matrix, one column is the sampling period of each state variable, and the other column is the offset of the corresponding sampling time.
     [This is how the S function is set in the M file]

   【2】再看看**子函数**:

  The prefix of the subfunction is mdl, and the flag controls the calling situation
. There are four common situations:
   Flag = 0: call the initialization subfunction mdlInitializeSizes;
   Flag = 1: call the continuous state update subfunction mdlDerivatives (t, x, u);
   Flag = 2: Call discrete state update subfunction mdlUpdate (t, x, u);
   Flag = 3: Call output subfunction mdlOutputs (t, x, u);

(2) Application of S function:

  Take the simplest chestnut: y = kx + b

  (1) Define the S function

        ① Initialize the main function

function [sys,x0,str,ts,simStateCompliance] = move(t,x,u,flag)
switch flag,
    case 0,
        [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;
    case 1,
        sys=mdlDerivatives(t,x,u);
    case 2,
        sys=mdlUpdate(t,x,u);
    case 3,
        sys=mdlOutputs(t,x,u);
    case 4,
        sys=mdlGetTimeOfNextVarHit(t,x,u);
    case 9,
        sys=mdlTerminate(t,x,u);
    otherwise
        DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end

        ② Initialization subfunction

function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates  = 1;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 1;
sizes.NumInputs      = 1;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;   % at least one sample time is needed
sys = simsizes(sizes);
x0  = [0];
str = [];
ts  = [0 0];
simStateCompliance = 'UnknownSimState';
function sys=mdlDerivatives(t,x,u)
sys = u;
function sys=mdlUpdate(t,x,u)
sys=[];
function sys=mdlOutputs(t,x,u)
sys=x;
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1;    %  Example, set the next hit to be one second later.
sys = t + sampleTime;
function sys=mdlTerminate(t,x,u)
sys = [];

  (2) Use S function in Simulink model

    First of all, we need to build this model with a module.
    Novices may not find the device. Here I will briefly say: in the Simulink standard module library, there is Scope under sinks, Sine Wave under Sources, and S-Function. Just go up
    here and talk about how to modify the number of input terminals of the Scope: double-click to open the Scope interface, click on the upper left to set, modify the Input parameter, just remember to modify Apply .
Insert picture description here
The model is built as follows: The
Insert picture description here
results are as follows:
Insert picture description here
    Sampling time: For the Simulink model, a step in the solver determines the minimum sampling time interval of the entire model.

(3) Special attention *-ensure correct operation

    When I applied the S function for the first time, I was able to encounter many problems. I summarized several common problems and then I need to pay attention to them. I hope everyone can write the right one at a time.

    ①Note that the S function name must be the same as the .m file you write, otherwise you will get an error ②NoteInsert picture description here
    that you must put the S function .m file and the model .slx file in a directory to ensure that you can find it when you click Edit And open the corresponding S function M file
Insert picture description here
    ③ Note that you must modify the current working directory of matlab to the directory where you saved these two files
Insert picture description here
    so that you can basically guarantee the operation. If you have other problems, please query or comment yourself .

Published 7 original articles · won 21 · views 195

Guess you like

Origin blog.csdn.net/Nirvana_Tai/article/details/105439610