MATLAB: [9] Simulink system simulation

9.1 Basics of Simulink Simulation

project creation

  • At the position of the new button, you can directly create a new Simulink file

  • In the opened window you can create a new Simulink project

  • You can click the colored Simulink Library Brower button to select the corresponding module. Modules are roughly divided into two categories: professional module library and Simulink module library
  • Drag different modules to the window and connect them to form a Simulink simulation system

Operation of Simulink blocks

  • Adding a module: first find the module in the Simulink module library, and then drag the module to the model editing window with the mouse
  • Module deletion and copying: the same as general editing operations
  • Connection of two modules: use the mouse to connect the output terminal of one module with the input terminal of another module, when the connection line is a solid line, release the mouse to indicate that the connection is successful

  •  Connection branch: After connecting a line, move the mouse pointer to the branch point, press the Ctrl key, hold down the mouse and drag it to the input terminal of the target module, release the mouse and Ctrl

Model save

  • File-Save in the upper left corner

Setting of module parameters

  • Module parameter setting: double-click the module to be set
  • Different modules have different parameters and need to be designed according to actual requirements

Simulation parameter settings

  • Click the gear button to enter the setting of simulation parameters

  •  Under the Solver menu, you can set the simulation algorithm to be a variable step size or a fixed step size

9.2 Establishment and packaging of subsystems

Subsystem Creation

  • Create a subsystem through the Subsystem module, drag it into the model editing window, and double-click to edit the subsystem

  • Convert an existing module into a subsystem: first select all the modules that make up the subsystem, and then execute the command to create a subsystem, so that all the original modules become a subsystem

 Encapsulation of Subsystems

  • Subsystem encapsulation refers to customizing dialog boxes and icons for the subsystem so that it has an independent operation interface, and combining its parameter settings in a dialog box to make the use of the subsystem more convenient

  •  Set up the four packaging tabs to complete the packaging of the subsystem

Conditional Execution of Subsystems

  • A subsystem that is executed by a control signal is called a conditionally executed subsystem

Enabling subsystem: Enabling the subsystem means that the system starts to execute when the control signal changes from negative to positive, and ends when the control signal becomes negative again. There is an "enable" control signal input port on its appearance.

Triggered Subsystem: A triggered subsystem is one that starts executing a subsystem when a triggering event occurs. Similar to the enable subsystem, the establishment of the trigger subsystem can directly select the Triggered Subsystem module or expand the existing system, add the Trigger module in the Ports & Subsystems library, and convert the subsystem into a starting subsystem. The value at the start of each departure of the trigger subsystem is the value at the end of the last trigger.

Enable trigger subsystem: It is to execute the subsystem when the enable control signal and the trigger signal work together

  • Four trigger modes of the trigger subsystem

 

9.3 Design and application of S function

S function

  • The full name of the S function is System Function, which refers to a functional module described in Chinese programming language
  • Users can use MATLAB language, or use C, C++, FORTRAN and other languages ​​to write S functions (need to be compiled into executable files)
  • The S function can receive relevant information from the Simulink solution algorithm and respond appropriately to the commands issued by the solution algorithm. This interaction is similar to the interaction between the Simulink system module and the solution algorithm

Write the S-function

  • Enter the command edit sfuntmpl.m in the MATLAB command line window to open the template file

function [sys, x0, str, ts ] = fname(t, x, u, flag )

  1. fname is the function name of the S function
  2. The input formal parameters t, x, u, flag are simulation time, state vector, input vector and sub-function call flag respectively
  3. The output parameter sys represents a return parameter; x0 is the initial state value; for the M file S function, str will be made into an empty array; ts is a two-column matrix
%采用S函数实现y=kx+b。

%主函数
function [sys,x0,str,ts]=timekb(t,x,u,flag,k,b)
switch flag
    case 0
        [sys,x0,str,ts]=mdlInitializeSizes; %初始化
    case 3
        sys=mdlOutputs(t,x,u,k,b);          %计算输出量
    case {1,2,4,9}
        sys=[];
    otherwise                               %出错处理
        error(num2str(flag))
end

%②初始化子函数
function [sys,x0,str,ts]=mdlInitializeSizes()
sizes=simsizes; %返回一个结构
sizes.NumContStates=0;   %无连续状态
sizes.NumDiscStates=0;    %无离散状态
sizes.NumOutputs=1;            %有一个输出量
sizes.NumInputs=1;         %有一个输入信号
sizes.DirFeedthrough=1;   %输出量中含有输入量
sizes.NumSampleTimes=1;   %单个采样周期
sys=simsizes(sizes);
%给其他返回参数赋值

x0=[];                   %设置初始状态为零状态
str=[];                    %将str变量设置为空字符串
ts=[-1,0];                 %假定继承输入信号的采样周期

%输出子函数
function sys=mdlOutputs(t,x,u,k,b)
sys=k*u+b;

Application of S function

  • S function module must be added
  • Add the S-function file name in the module of the S-function (double-click to open the parameter dialog box)

  •  In the model editing window, select the S module and set the encapsulation

  •  to complete the setup

9.4 Simulink simulation application example

Leave a hole here, and then make something fun

 

Guess you like

Origin blog.csdn.net/Alex497259/article/details/104598741
Recommended