Simulation and Analysis of Simulink System in MATLAB

1. Simulation and analysis of Simulink system

  • After the system model is established, the simulation parameters and numerical algorithm are selected, and the simulation program can be started to simulate the system.

1. Set simulation parameters

  • In the process of system simulation, various simulation parameters such as simulation algorithm and output mode must be set in advance. There are the following methods to open the simulation parameter setting dialog box in the model editing window.
  • (1) Click the Model Configuration Parameters button in the toolbar.
  • (2) Select Simulation ⟶ \longrightarrow Model Configuration Parameters command.
  • The opened simulation parameter setting window is shown in the figure below.

insert image description here

  • In the simulation parameter setting window, the simulation parameters are divided into the following 7 categories.
  • (1) Solver parameters: used to set the simulation start and end time, select the differential equation solving algorithm and specify parameters for it, and select some output options.
  • (2) Data Import/Export parameters: used to manage the import and export of workspace data.
  • (3) Optimization parameter: used to set the simulation optimization mode.
  • (4) Diagnostics parameter: It is used to set the warning level when various errors occur during the simulation.
  • (5) Hardware Implementation parameter: It is used to set the hardware to realize the simulation.
  • (6) Model Referencing parameter: used to set the reference model.
  • (7) Simulation Target parameter: used to set the simulation model target.

1.1 Solver parameter settings

  • Solver is a numerical integration algorithm that uses the information contained in the model to calculate the dynamic behavior of the system.
  • Simulink provides solution algorithms that support the simulation of a wide variety of systems, including continuous-time (analog), discrete-time (digital), hybrid (mixed-signal), and multiple-sample-rate systems of any size.
  • These solution algorithms can simulate rigid systems as well as systems with discontinuous processes. You can specify the parameters of the simulation process, including the type and properties of the solution algorithm, the start and end time of the simulation, and whether to load or save simulation data.
  • Additionally, optimization and diagnostic information can be set. Select the Solver option in the left pane of the simulation parameter setting window, and all Solver parameters will be listed in the right pane, as shown in Figure 12-8.

insert image description here

  • (1) Set the simulation start and end time (Simulink time). In the two edit boxes of Start time and Stop time, set the simulation start time and end time by directly inputting values, and the time unit is second (s).
  • (2) Selection of simulation algorithm (Solver options). Set the algorithm category in the Type drop-down list box: Fixed-step (fixed step size) and Variable-step (variable step size) algorithms, and select a specific algorithm in the Solver drop-down list box.
  • Simulation algorithms are divided into fixed-step-size algorithms and variable-step-size algorithms according to the change of step size. The fixed step size means that the calculation step size remains unchanged during the simulation process, while the variable step size means that the step size needs to be changed according to the calculation requirements during the simulation process. For these two types of algorithms, their corresponding options and specific algorithms are different.
  • When using variable step size algorithms, you should first specify the allowable error limit, including relative error limit (Relative Tolerance) and absolute error limit (Absolute Tolerance). When the error in the calculation process exceeds the error limit, the system will automatically adjust the step. The size of the step size will determine the accuracy of the simulation.
  • When using the variable step size algorithm, the maximum allowed step size (Max Step Size) should also be set. In the case of the default value (Auto), the maximum step size given by the system is (end time start time) / 50.
  • Under normal circumstances, the maximum step size given by the system is sufficient, but if the user's simulation time is too long, the default step size value is very large, and distortion may occur. In this case, a smaller value should be set as needed the step size.
  • When using the fixed step size algorithm, the fixed step size must be set first. Since the step size of the fixed-step algorithm remains unchanged, the error limit is not set at this time, and there is an additional model type (Tasking Mode for Periodic Sample Times) option, which includes Auto (default value), SingleTasking (single-task And MultiTasking (multitasking).
  • Single task means that the sampling rate of each module is the same, and the transfer of sampling rate is not detected; multitasking means that the modules in the model have different sampling rates, and the transfer of sampling rate between modules is detected at the same time; the default value is based on the sampling rate of the module Whether it is the same to decide whether to use single task or multi task.
  • Variable step size and fixed step size include many different specific algorithms. In general, the continuous system simulation should choose the ode45 variable step size algorithm. For rigid problems, the ode15s algorithm with variable step size can be selected. The discrete system generally chooses the discrete (no continuous states) algorithm with a fixed step size by default. Note that in the simulation model This simulation algorithm cannot be used when there are continuous links, but an algorithm such as the 4th order Runge-Kutta method can be used to solve the problem.

1.2 Data lmport/Export parameter settings

  • The imported data includes input signal and initial state, and input signal can be generated by standard signal or custom function.
  • The exported data includes output signals and state data of the simulation process, which can be used to generate graphics or perform other processing. The Data ImportExport (data import/export) parameter options are shown in the figure below, including Load from workspace, Save to workspace or file and Simulation Data Inspector.

insert image description here

  • (1) Load from workspace (load data from workspace). During the simulation process, if there is an input port (In module) in the model, the data can be directly loaded into the input port from the workspace, that is, first check the Input check box in the Data Import/Export parameter option, and then Enter the variable name of the data in the edit box. Variable names can take different input forms.
  • ① Matrix form. If the variable name is input in the form of a matrix, the number of columns of the matrix must be one more than the number of input ports of the model. MATLAB defaults the first column of the matrix to a time vector, and each subsequent column corresponds to each input port. The first row of the matrix Indicates the input state of each input port at a certain moment.
  • In addition, the matrix can also be represented separately, that is, the default representation method of MATLAB [ t , u ] [t,u][t,u ] , of whichttt is a one-dimensional time column vector, representing the simulation time,uuu is andttt is an n-dimensional column vector of equal length (n indicates the number of input ports), representing the state value.
  • For example, we define tt in the command line windowtwauu __u
>> t=(0:0.1:10)';
>> u=[sin(t),cos(t).*sin(t),exp(-2*t).*sin(t)];
  • Then the relationship between the data input by the three input ports and time is sin ⁡ t \sin tsint cos ⁡ t sin ⁡ t \cos t\sin t costsint e − 2 ∗ t sin ⁡ t e^{-2*t}\sin t e2tsint
  • ② Contains the structural form of time data. For structures containing time data, there are very strict regulations in MATLAB, that is, there must be two top-level members in the structure whose names cannot be changed: time and signals.
  • The time member contains a column vector, representing the simulation time; the signals member is a vector, each element in the vector corresponds to an input port, and each element must contain a values ​​member whose name cannot be changed, and the values ​​member also contains a Column vector corresponding to the input data for the input ports.
  • For example, for the above example, if we change to a structure input containing time data, the command format is as follows:
>> t=(0:0.1:10)';
>> A.time=t;
>> A.signals(1).values=sin(t);
>> A.signals(2).values=cos(t).*sin(t);
>> A.signals(3).values=exp(-2*t).*sin(t);
  • Enter A in the text box on the right side of the Input check box, and the generated simulation curve is exactly the same as the output curve after the above matrix data input.
  • Below the Input check box, there is an Initial state check box, which represents the initialization state of the module. The way to initialize the module is to check the Initial state check box first, and then enter the variable name of the initialization data in the text box on the right. The form required by the variable is basically the same as the variable form of the input port data above, but the number of data in the variable must be the same as the number of state modules.
  • (2) Save to workspace or file (save to workspace or file). In the Save to workspace or file area, the options that can be selected include Time (clock), States (state), Output (output port), Final states (final state), Signal logging (signal), etc.
  • Same as the form of loading data, there are three forms of saving data: matrix, structure and structure including time data, which can be selected according to the needs in the Format drop-down list box. For different saving forms, the format of Time is unchanged, and always corresponds to the sampling time of the simulation.

2. Run the simulation and analyze the simulation results

2.1 Running the simulation

  • In MATLAB, you can run simulations interactively in the Simulink model editing window. Simulink simulation has 3 modes, you can select Simulation ⟶ \longrightarrow in the model editing window Mode command to set.
  • (1) Normal: Standard mode (default setting), run in an interpreted mode, and can flexibly change model parameters and display results during the simulation process, but the simulation runs slowly.
  • (2) Accelerator: Accelerator mode, which improves simulation performance by creating and executing compiled object code, and can change model parameters more flexibly during simulation. In accelerated mode, the s-function generated by model compilation is run, and model coverage information cannot be provided.
  • (3) Rapid Accelerator: Fast accelerator mode, which can perform model simulation faster than Accelerator mode. This mode does not support debugger and performance evaluator.
  • After setting the simulation parameters, click the Run button in the toolbar of the model editing window, or select Simulation ⟶ \longrightarrow Run command to start the simulation of the current model.
  • Simulink supports debugging using the Simulation Stepper, which is convenient for stepping through simulation data on an oscilloscope, or checking how and when the system changes state. Click the Step Forward button in the toolbar of the model editing window to start a single-step simulation. Click the Stop button in the toolbar of the model editing window to terminate the single-step simulation.
  • Before running the simulation, click the Stepping Options button in the toolbar of the model editing window, check the Enable stepping back check box in the opened dialog box, and click the Step Back button in the toolbar of the model editing window during the simulation to retrace the simulation process.

2.2 Simulation result analysis

  • Simulink provides several debugging tools that are useful for understanding simulation behavior. Use the viewers and oscilloscopes available in Simulink to view signals and visualize simulation behavior. Simulation results can also be exported to the MATLAB workspace to view and analyze the data using MATLAB algorithms and visualization tools.
  • During the simulation process, the user can set different output modes to observe the simulation results. In order to observe the change trajectory of the simulation results, three methods can be used.
  • (1) Send the simulation result to the Scope module or XYGraph module. The Scope block shows the system output versus simulation time, and the XYGraph block shows one of the two signals fed to the block versus the other.
  • (2) Send the simulation results to the output port, export the results to the workspace, and then use MATLAB commands to draw the change curve of the variable. Before running a simulation of this model, in the Data Impot/Export tab of the Configuration Parameters dialog box, specify the names of the time variable and the output variable (assumed to be ttt y y y ), then, when the simulation ends, the time value is saved in the time variable t, and the signal value of the corresponding output port is kept in the output variableyyy , at this time, you can use the whos command in the command line window to view the memory variables, or you can use the drawing command to draw the change curve of the system output.
  • (3) Send the output result to the To Workspace module, fill in the output variable name in the parameter dialog box of the To Workspace module, and select the Array option in the Save format drop-down list, so as to save the result directly into the workspace, and then use the MATLAB command Draw the change curve of this variable.
  • There are other output methods for the simulation output results, for example, the output value can be displayed by using the Display module.
  • For example, we use Simulink to construct the function curve y = 5 r + 16 y=5r+16y=5r+16
  • The Sources block library of Simulink provides common signal sources such as clock, pulse, and sine wave. These common signal sources and other blocks such as summation, product, gain, and MATLAB functions provided in the Simulink block library can be used for appropriate connection combinations. You can build all kinds of signal sources you need.
  • The following two methods are used to construct the function curve, one method is to use the MATLAB Function module, and the other method is to use the combined connection of the basic modules.
  • Method 1: Use MATLAB Function module to construct the simulation model.
  • (1) Start Simulink and open the model editing window, and add the required modules to the model. Click the Sources block library in the Simulink Library Browser window, find the Clock (clock) block in the right window, and drag it to the model editing window with the mouse.
  • Similarly, drag out the MATLAB Function (MATLAB function) module in the User-Defined Functions (user-defined function) module library, and drag out the To Workspace (output to workspace) module and the Scope (oscilloscope) module in the Sinks module library to connect Combination, its model is shown in the figure below.

insert image description here

  • (2) Set the module parameters. Double-click the MATLAB Function block to create a function of its signal source.
function	y=f(t)
	y=5*t*t+16;
  • Double-click the two output to workspace modules, and enter the variable name tt respectivelyt y y y , the simulation parameter takes the default value.
  • (3) Start the simulation, the function curve is shown in the figure below.

insert image description here

  • Method 2: Build a simulation model by combining basic modules.
  • Another way to construct the signal source is to combine some commonly used mathematical modules into a model, and the model is shown in the figure below.

insert image description here

  • Modules such as Clock (clock), Constant (constant), Gain (gain), Product (product) and Sum (summation) are used in the model. Double-click the gain module to input 5, the constant module to input 16, and finally set the simulation parameters. The output is the same as method 1.
  • Compared with method 1, this method uses more modules. In addition, the XY Graph module is also used in the model to display the trajectory of the output signal.
  • For example, we use Simulink simulation to find I = ∫ 0 1 x ln ⁡ ( 1 + x ) dx I=\int_{0}^{1}x\ln \left ( 1+x\right)\mathrm{d}xI=01xln(1+x)dx
  • First open the model editing window and add the required modules to the model. Click the Sources block library in the Simulink Library Browser window, and drag the Clock block to the model editing window.
  • Drag the function block Fcn in User-Defined Functions to the model editing window, drag the Integrator block in the continuous system block library Continuous to the model editing window, and drag the Display block in the Sinks block library to the model editing window.
  • Then, we set the module parameters and connect each module to compose the simulation model. Double-click the Fcn module to open the Block Parameters dialog box, enter u*log(1+u) in the Expression column, and do not need to set other module parameters.
  • After setting the module parameters, connect each module with a connection line to form a simulation model, as shown in the figure below.

insert image description here

  • Set the system simulation termination time to 1s, run the simulation model, and the Display module shows that the simulation result is 0.25.

Guess you like

Origin blog.csdn.net/weixin_45891612/article/details/131431486
Recommended