The Simulink operation basis of MATLAB and the establishment of the system simulation model

1. Basic operation of Simulink

  • Simulink is an important part of MATLAB. It is suitable for both linear systems and nonlinear systems, both continuous systems and discrete systems and continuous and discrete mixed systems. It is applicable to both steady system and time-varying system.

1. Starting and exiting Simulink

1.1 Starting Simulink

  • During the installation of MATLAB. If the Simulink component is selected, Simulink will be installed after MATLAB is installed. If necessary, Simulink can be started directly, the steps are as follows.
  • (1) Enter the Simulink command in the command line window of MATLAB, or select the "Home" tab of the MATLAB main window, and click the Simulink command button in the SIMULINK command group, or select the "Home" tab of the MATLAB main window, and click "File ” command button in the command group, and then select the Simulink Model command from the drop-down menu, and then enter the Simulink start page.
  • (2) Click the Blank Model button on the Simulink start page to open a model editing window named untitled, as shown in the figure below. Using the model editing window, a simulation model can be created by dragging and dropping the mouse.

insert image description here

  • In the Simulink model editor window select File ⟶ \longrightarrowNew ⟶ \longrightarrow Blank Model command, or press Ctrl+N keys, to open the Simulink Library Browser window as shown below.
  • The window consists of two panes, the left pane lists all module repositories in a tree-like list. If you click a certain module library, the right pane lists the submodule libraries of the module library; if you click a certain submodule library, the right pane lists all the modules in the submodule library.

insert image description here

  • In the Simulink Library Browser window, click the button in its toolbar or press the Ctrl+N key to open the model editing window.
  • (4) After the model is created, select File ⟶ \longrightarrow in the Simulink model editing window Use the Save command or Save as command, or click the save command button in the tool bar of the model editing window to save the model in the format of a model file.
  • The format of the saved file is related to the character encoding supported by the current system. If Chinese characters are used in the model, it is recommended to use the SLX format for storage.
  • It should be noted here that Simulink cannot run independently, it can only run in the MATLAB environment.

1.2 Open the model file

  • If you want to edit and modify an existing model file, you need to open the model file, and the methods are as follows.
  • (1) Directly input the model file name (without the extension) in the MATLAB command line window, which requires the model file to be in the current folder or in the defined search path.
  • (2) Click the Open file button on the Simulink start page, and then select or enter the name of the model to be edited. Or directly select the recently opened model file to open the model.
  • (3) Select File ⟶ \longrightarrow in the Simulink model editing window Open command, or press Ctrl+O keys, or click the Open command button in the toolbar, and then select or input the name of the model to be edited. Alternatively File⟶ \longrightarrow Open Recent command opens the most recently opened model file.
  • (4) In the Simulink Library Browser window, click the Open command button in its toolbar to open an existing model file.

1.3 Exiting Simulink

  • To exit Simulink, just close all model editing windows and the Simulink Library Browser window.

2. Preliminary Simulink simulation

2.1 Model elements

  • A typical Simulink model includes the following three elements.
  • (1) Signal source (Source). The signal source can be Constant (constant), Clock (clock), Sine Wave (sine wave), Step (unit step function), etc.
  • (2) System modules. For example, the Math Operations block (mathematical operations), the Continuous block (continuous systems), the Discrete block (discrete systems), and so on.
  • (3) Sink. The signal can be displayed on the Scope (oscilloscope), XY Graph (graph recorder), and can also be stored in a file (To File), and exported to the workspace (To Workspace).

2.2 Simulation steps

  • System simulation using Simulink usually includes the following steps.
  • (1) Establish a system simulation model, including adding modules, setting module parameters, and connecting modules.
  • (2) Set the simulation parameters.
  • (3) Start the simulation and analyze the simulation results.

2.3 Simple example

  • The following is a simple example to illustrate the method of using Simulink to establish a simulation model and carry out system simulation.
  • For example, we use the Simulink simulation curve y ( t ) = sin ⁡ t sin ⁡ ( 4 π t ) ( 0 ≤ t ≤ π ) y(t)=\sin t\sin(4\pi t)(0\le t \le \pi)y(t)=sintsin ( 4 π t ) ( 0tp ) .
  • The sine signal is provided by the Sine Wave module in the Sources module library (signal source), and the quadrature is realized by the Product module in the Math Operations module library (mathematical operation), and then the Scope module (oscilloscope) in the Sinks module library (signal output) Output waveform, the operation process is as follows.
  • (1) Open a model editing window named untitled to create a simulation model.
  • (2) Open the Simulink Library Browser window and add the required modules to the model. Expand the Simulink module library in the Simulink Library Browser window, then click the Sources module library, find the Sine Wave module in the right window, and drag it to the model editing window with the mouse; click the Math Operations module library, and click the Math Operations module library on the right side Find the Product module in the window, drag it to the model window with the mouse; click the Sinks module library, find the Scope module in the right window, and drag it to the model window with the mouse.
  • (3) Connect each module with wires to form a system simulation model, as shown in the figure below. Most modules have the symbol ">" on both sides, the end connected to the tip indicates the signal input terminal, and the end connected to the opening indicates the signal output terminal. When connecting, press the left mouse button from the output terminal of one module, drag it to the signal input terminal of another module, release the left mouse button to complete the connection operation, and the connection arrow indicates the direction of signal flow. You can also click the source module of the signal flow, hold down the Ctrl key, and then click the target module of the signal flow to realize module wiring.

insert image description here

  • (4) Set the module parameters. For the two Sine Wave modules, first double-click them to open their Block Parameters dialog box, as shown in the figure below, set the Frequency (frequency) to 1 and 4*pi respectively, set the Amplitude (amplitude) to 1, and keep the rest of the parameters unchanged . For other modules, their parameters do not change.

insert image description here

  • After the model is built, select File ⟶ \longrightarrow in the model editing window Use the Save command or Save as command, or click the save command button in the tool bar of the model editing window to save the model in the format of a model file.
  • (5) Set the system simulation parameters. In the model editor window select Simulation ⟶ \longrightarrow Model Configuration Parameters command to open the simulation parameter setting dialog box, set the start time to 0 in the Star time text box, and set the end time to pi in the Stop time text box.
  • Set the Type parameter in the Solver options (algorithm options) column to Fixed-step (fixed step size), and select the ode5 (Dormand-Prince) option in the Solver drop-down list box on the right, which is the 5th-order Runge-Kutta algorithm , and then set the value of Fixed-step size to 0.001, as shown in the figure below.

insert image description here

  • (6) Simulation operation. In the model editor window select Simulation ⟶ \longrightarrow Run command, or click the Run command button in the toolbar, and then double-click the oscilloscope module, you can see the simulation results in the oscilloscope window, the curve is shown in the figure below.

insert image description here

Second, the establishment of the system simulation model

  • Simulink provides a graphical user interface, users can use the mouse to call standard modules from the module library, connect them appropriately to form a dynamic system model, and use the parameter dialog box of each module to set parameters for each module in the system.
  • After the parameter setting of each module is completed, the model of the system is established. If no parameter is set for a certain module, it means that the default parameter value pre-set by Simulink for the module is used as the parameter of the module.

1. Basic modules of Simulink

  • The module is the basic unit of the system simulation model, and the simulation model of the dynamic system can be established by connecting various modules together in an appropriate way, so the construction of the system simulation model mainly involves the operation of the Simulink module.
  • Simulink's block library provides a large number of blocks, which can be roughly divided into two categories: basic block library and professional block library. Click the symbol in front of Simulink in the Simulink Library Browser window, you will see the basic sub-module library contained in the Simulink module library, click the required sub-module library, you will see the corresponding module in the right window, select the required The module can be dragged to the model editing window with the mouse.
  • Similarly, right-click on the Simulink option on the left side of the Simulink Library Browser window, and select the Open Simulink Libray command in the pop-up shortcut menu, and the Simulink basic module library window will open, as shown in the figure below. Double-click the sub-module library icon to open the sub-module library and find the modules required for simulation.

insert image description here

  • Taking the Continuous block library (continuous system) as an example, double-click the icon of the Continuous block library in the Simulink basic block library window to open the block library window, or select the Continuous option under the Simulink menu in the Simulink Library Browser window, and then click The right side of the Library Browser window opens to the Continuous Systems Block Library.
  • In the continuous system module library, there are many modules such as Integrator (integral link), Derivative (differential link), State-Space (state equation), Transfer Fcn (transfer function), etc., which can be used for continuous system modeling.
  • The Simulink block library is very rich in content, and the operation method of other block libraries is the same as that of the continuous system block library. In addition, we can customize and create modules ourselves.

2. Module operation

2.1 Add and delete modules

  • To add a module to the model, first find the module in the Simulink block library, and then drag the module to the model editing window.
  • To delete a module, select the module and press the Delete key, or select Edit ⟶ \longrightarrow Cut command or Delete command. You can also right-click the module to be deleted, and select the Cut command or the Delete command in the pop-up shortcut menu. The Cut command will send the deleted module to the clipboard, and the Delete command will completely delete the module.

2.2 Select modules

  • To select a single module in the model editing window, just click on the module with the mouse, and a dark border will appear around the module.
  • To select multiple modules, you can press and hold the left mouse button at one corner of the area occupied by all modules, and drag to the opposite corner of the area. During this process, a dark frame will appear. When the dark frame surrounds the selected After selecting all the modules, let go of the left mouse button, and a dark border will appear around all the selected modules, indicating that the modules are all selected.

2.3 Copy Module

  • When establishing a system simulation model, multiple identical modules may be required, and the method of module duplication can be used at this time.
  • The method to copy a module in the same model editing window is to click the module to be copied, hold down the left mouse button and hold down the Ctrl key at the same time, move the mouse to an appropriate position and release the left mouse button, after the module is copied, you will find the The module name of the model is numbered on the basis of the original name, which is a Simulink convention. There is a one-to-one correspondence between modules and names in each model, and each module has a different name.
  • The way to copy modules between different model editing windows is to first open the windows where the source module and the target module are located, then click the module to be copied, hold down the left mouse button and move the mouse to the corresponding window (do not hold down the Ctrl key) , and then release the left mouse button, the module will be copied, but the source module will not be deleted.
  • The copy operation can also be done by selecting Edit ⟶ \longrightarrow in the model editing window Copy command and Edit+Paste command to complete.

2.4 Adjustment of module shape

  • To change the size of a single module, first point the mouse pointer to the module, then a small white square appears at the four corners of the module, click any one of the four white squares around it with the left mouse button and drag it to the desired position Then release the mouse.
  • To change the size of all modules in the entire model, select View ⟶ \longrightarrow in the model editing window⟶Zoom menu item, the Zoom in and Zoom out commands are used to zoom in and out of the whole model respectively, the Normal View (100%) command is used to restore the whole model to the original normal size, and the Fit to View command is used to The module or current model can be zoomed in to the full window size for viewing.
  • To adjust the orientation of a block, first select the block, then select Diagram ⟶ \longrightarrow in the model editor window Rotate & Flip menu item, the Clockwise command makes the block rotate 90° clockwise, the Counterclockwise command makes the block rotate 90° counterclockwise, and the Flip Block command makes the block rotate 180°.
  • To change the color of a block, first select the block, then select Diagram ⟶ \longrightarrow in the model editor window In the Format menu item, the Foreground Color command is used to set the foreground color of the module, that is, the color of the module icon, frame and module name.
  • Select the Background Color command to set the background color of the module, that is, the background fill color of the module.
  • Select the Canvas Color command to change the background color of the model editing window.
  • Select the Shadow command to make the module produce a shadow effect.

2.5 Processing of module names

  • To hide or show block names, first select the block, then select Diagram ⟶ \longrightarrow in the model editor windowFormat ⟶ \longrightarrow Show Block Name command, to display the hidden name of the block or hide the block name.
  • To modify the module name, click the area of ​​the module name, and a cursor in the editing state will appear here, and the module name can be modified at will in this state.
  • The font in the block name and block icon can also be changed by selecting the block and selecting Diagram ⟶ \longrightarrow in the model editor windowFormat ⟶ \longrightarrow Font Style command, the Select Font dialog box will pop up, select the desired font in the dialog box.
  • The position of the module name has certain rules. When the interface of the module is on the left and right sides, the module name can only be located on the upper and lower sides of the module, and the default is on the lower side; when the interface of the module is on the upper and lower sides, the module name can only be located on the module The left and right sides, the default is on the left.
  • Therefore, the module name can only be moved from the original position to the relative position. You can use the mouse to drag the module name to its relative position; you can also select the module and select Diagram ⟶ \longrightarrow in the model editing windowRotate & Flip ⟶ \longrightarrow The Flip Block Name command realizes the relative movement of the block name.

3. Module connection

After setting up each module, they need to be connected in a certain order to form a complete system model.

3.1 Connecting two modules

  • From the output of one module to the input of another module, this is the most basic connection situation of Simulink simulation model.
  • The method is to move the mouse pointer to the output terminal first, press and hold the left mouse button when the mouse pointer turns into a cross-shaped cursor, move the mouse pointer to the input terminal of another module, and release the left mouse button when the connection line changes from a dotted line to a solid line. key to complete the connection of the two modules.
  • If two modules are not on the same horizontal line, the connecting line is a polyline. If you want to use a slash, you need to select the connection after the connection, and then hold down the Shift key to drag.

3.2 Adjustment of wiring between modules

  • Adjusting the connection position between modules can be realized by dragging and dropping the mouse.
  • First move the mouse pointer to the position of the line segment to be moved, press and hold the left mouse button, move the mouse to the target position, and release the left mouse button.
  • The method of deleting a connection is the same as that of deleting a module, that is, first select the connection and then delete it.

3.3. The branch of the connection

  • In the simulation process, it is often necessary to transmit a signal to different modules, and at this time, it is necessary to separate a connection from a connection.
  • The operation method is, after connecting a line first, move the mouse pointer to the position of the branch point, first press and hold the Ctrl key, then press and hold the mouse and drag to the input terminal of the target module, release the mouse and the Ctrl key.

3.4. Mark the connection

  • In order to make the model more intuitive and readable, the transmitted signals can be marked.
  • The operation method is to double-click the connection to be marked, and a small text edit box will appear, in which to enter the marked text, thus creating a signal mark.

4. Module parameter and attribute setting

  • Module parameters define the dynamic behavior and state of the module, and properties define the appearance of the module.

4.1 Module parameter setting

  • There are the following methods to open the block parameter setting dialog box in the model editing window.
  • (1) Double-click the module to be set.
  • (2) Select the module to be set, and then select Diagram ⟶ \longrightarrow Block Parameters command.
  • (3) Right-click the module to be set, and select the Block Parameters command from the shortcut menu.
  • The module parameter setting dialog box is divided into two parts, the upper part is the module function description, and the lower part is used for module parameter setting.

4.2 Module property setting

  • There are the following methods to open the block property setting dialog box in the model editing window.
  • (1) Select the module to set properties, select Diagram ⟶ \longrightarrow⟶Properties command.
  • (2) Right-click the module whose properties are to be set, and select the Properties command from the shortcut menu.
  • The block property dialog box includes 3 tabs: General, Block Annotation and Callbacks.
  • There are 3 basic attributes that can be set in the General tab: the Description attribute describes the usage of the module in the model; the Priority attribute specifies the execution priority of the module in the model relative to other modules, and the value of the priority must be an integer (you can is a negative integer), the smaller the value, the higher the priority, or you can not input the priority value, then the system will automatically select the appropriate priority; the Tag attribute is a tag in text format added by the user for the module.
  • The Block Annotation tab specifies which parameter of the block is displayed under the block's icon.
  • The Callbacks tab specifies the MATLAB commands or programs that need to be executed when certain operations are performed on the module.

Guess you like

Origin blog.csdn.net/weixin_45891612/article/details/131410665