Matlab and Simulink: A brief tutorial

Matlab and Simulink: A brief tutorial

APPENDIX Matlab and Simulink: A brief tutorial G G.1 Introduction Simulink is a companion program to MATLAB, and is a software system for simulatin...

247KB Sizes 1 Downloads 226 Views

APPENDIX

Matlab and Simulink: A brief tutorial

G

G.1 Introduction Simulink is a companion program to MATLAB, and is a software system for simulating dynamic systems, both linear and nonlinear. “It is a graphical menu-driven program that allows the user to model a system by drawing a block diagram on the computer screen and manipulating it to simulate various system responses”. It can be used for linear, nonlinear, continuous-time, discrete-time, single-input single-output (SISO) and multiple-input multiple-output (MIMO) systems. Linear time-invariant system models can be easily constructed using transfer functions [1]. State-space and transfer function models and their solutions are discussed in this tutorial. System response plots can be made during simulation and can be integrated with MATLAB commands. This tutorial discusses the following: • • • •

Features of Simulink. Example of using Simulink for a first-order system. Designing a proportional-integral (P-I) controller using Simulink. Solving state-space equations

G.2 Getting started with simulink Start the MATLAB program by double-clicking on the MATLAB icon. At the MATLAB prompt ▶ type simulink. • •

In the Simulink Library Browser window click on create a new model icon. This creates a new window that is untitled. Open the Simulink directory by clicking on the +Simulink icon. Several items are displayed, including the following: ▪ Continuous ▪ Discontinuous ▪ Discrete ▪ Look-up Tables ▪ Math Operations

319

320

APPENDIX G Matlab and Simulink: A brief tutorial

▪ ▪ ▪ ▪ ▪ ▪ ▪

Model Verification Ports & Subsystems Signal Attributes Signal Routing Sinks Sources User-defined Functions.

We will use some of these features to build our models and display the results. Explore the details of these features by clicking on each. Use the help feature in each to learn more about Simulink functions.

G.3 Simulation of a single-input single-output (SISO) system Consider the following example to illustrate some of the features of Simulink. •

Click on +Continuous. Drag the Transfer Fcn icon into the work space. A default transfer function is shown. Double click on this block. Change the second coefficient in the denominator to 0.02. Now the transfer function block has the form Gp ðsÞ ¼











12 s + 0:02

(G.1)

Click on the +Sources button and drag the function Step to the work area. Connect the step block to the Gp(s) block. In the step block, use step time equal to zero, so that there is no delay in the step input. Click on the +Sinks button and drag Scope and To Workspace buttons to the work area. Connect the output of the main block to both Scope and To Workspace blocks. If necessary, adjust the parameters of the Scope and To Workspace blocks. For example, change the name of the workspace block to level. Changes to any of the blocks may be made by double-clicking on the block and adjusting the parameters. Use ‘array’ for structure of data. Fig. G.1 shows the model diagram and the above blocks (this is not an executable block). After the model is completed you may save this in a file. Hint: Connecting one block to another: Select the source block; then hold down the Ctrl key while left clicking on the destination block. Run the simulation by clicking on simulation and then click start. Set the stop time equal to 300. By clicking on the Scope you may view the simulation. The output may also be plotted using the MATLAB plot command plot (tout, level) as shown in Fig. G.2. Note that the time points are stored in the array ‘tout’.

APPENDIX G Matlab and Simulink: A brief tutorial

12 s+0.02 Step

Transfer Fcn

Scope

level

To Workspace

FIG. G.1 Example Simulink model block diagram.

Example simulation for TF = 12/(s+0.02) 600

500

Level

400

300

200

100

0

0

50

100

150

200

250 300 Time (s)

350

400

450

FIG. G.2 Plot of the variable, level, as obtained from the Simulink simulation block.

500

321

322

APPENDIX G Matlab and Simulink: A brief tutorial

G.4 Simulation of a closed-loop system with P-I controller The Simulink model for controlling the variable, level, using a proportional-integral (P-I) controller is shown in Fig. G.3. The transfer function of the P-I controller is given by Gc ðsÞ ¼ Kp +

Ki Kp s + Ki ¼ s s

(G.2)

Kp ¼ proportional constant, Ki ¼ integral constant. Gc ðsÞ ¼

The summing block

0:01s + 0:0025 s

(G.3)

is available in the +Math library as Sum. Click on

this, and drag it to the work space and make connections as shown in Fig. G.3. If the summing block has a positive feedback, convert the feedback signal to a negative value by inserting the gain block with Gain 5 2 1. Fig. G.4 shows the response of the system to a unit step change in the input (or set point).

+ + Step

1

num(s) s

s+0.02

Transfer fcn

Transfer fcn1

–1 Gain

Scope

level To Workspace

FIG. G.3 Simulink model with level control using a P-I controller. Enter the values of Kp and Ki in the first transfer function block [num(s)]. Use Kp ¼ 0.01, Ki ¼ 0.0025.

APPENDIX G Matlab and Simulink: A brief tutorial

Level response with a P-I controller 1.4

1.2

level (ft)

1

0.8

0.6

0.4

0.2

0

0

50

100

150

200

250

300

350

Time (s)

FIG. G.4 Response of the level dynamics for a set point change of unity. Note that the the settling time (for the level to be within 5% of steady-state value) is about 200 s.

G.5 Solving linear differential equations using state-space models The state-space model of a linear dynamic system is written as. dx ¼ Ax + Bf dt y ¼ Cx + Df

(G.4)

x ¼ (n x 1) vector of state variables, y ¼ (r x 1) vector of outputs, f ¼ (m x 1) vector of inputs. A(n x n), B(n x m), C(r x n) and D(r x m) are matrices. In most applications matrix D is zero. Also, matrix B may be just a column vector of dimension (n x 1), with a single input variable. In the Simulink library browser, click on continuous and select the state-space model. Click and drag the state-space icon to the work space. If you double-click on this box, a state-space block parameter window appears. You may define the

323

324

APPENDIX G Matlab and Simulink: A brief tutorial

. x = Ax + Bu y = Cx + Du Step

State-Space

Scope

simout To Workspace

FIG. G.5 Block diagram of the state-space model.

matrices in this block, or you may simply define the names of the matrices (such as A, B, C, D) and specify them in the MATLAB workspace. If these matrices are saved in a file, load this file on to MATLAB. Define the initial condition vector as [0 0 0 0 0]. The length of the vector is equal to the number of state variables in the vector x. See remarks at the end for additional information on saving and plotting arrays. Connect step (from the sources), scope and To Workspace (from sinks) blocks to the state-space block as shown in Fig. G.5. Connecting one block to another: Select the source block, then hold down the Ctrl key while left clicking on the destination block. Verify each of the blocks for appropriate values of its parameters. Double-click on the To Workspace block. The output variables are defined in the vector simout. You may change the variable name. The decimation value allows you to write data at every n-th sample. Use default values for both decimation (1) and sample time (1). Use array for structure of data. After setting and applying these parameters, go back to the main work space. Click on Simulation on the control bar. Set start time (0.0) and stop time (10.0). If you are satisfied with all the settings click on Simulation and then click on Start. This completes the simulation. You may plot the vector simout using MATLAB.

Example G.1

A ¼ [0 1; -1 –1] B ¼ [0 1]’; the magnitude of the step may be changed by changing the entry in (B) C ¼ [1 0; 0 1] D ¼ [0 0]’ Initial Condition ¼ [0 0]’ Save your model (use a filename) for future applications.

APPENDIX G Matlab and Simulink: A brief tutorial

Simulation of a state-space model 1.2

1

X1 and X2

0.8

0.6

0.4

0.2

0.0

–0.2

0

2

6

4

8

10

Time (s)

FIG. G.6 Response of the state variables x1 () and x2 () for a step input.

Fig. G.6 shows the plots of the two state variables. The graph is created by using MATLAB commands for plotting. You may also save this plot in a file.

G.6 Computing step response using a transfer function The transfer function of a linear system is defined as the ratio between the Laplace transform of its output {Y(s)}and the Laplace transform of its input {X(s)}. Consider a transfer function of the form. GðsÞ ¼

Y ðs Þ b1 s + b0 ¼ XðsÞ a2 s2 + a1 s + a0

(G.5)

The numerator is a first order polynomial and the denominator is a second order polynomial. The following are the MATLAB commands for computing the unit step response of the above system; x(t) is a unit step input. num ¼[b1 b0]; den ¼[a2 a1 a0]; sys ¼ tf(num,den); step(sys, tfinal)

325

APPENDIX G Matlab and Simulink: A brief tutorial

Unit step response of a second order system: G(s) = 2/(s2 + s + 2) 1.4 1.2 1 Step Response

326

0.8 0.6 0.4 0.2 0 0

2

4

6 Time (s) (seconds)

8

10

12

FIG. G.7 Step response of a second order linear system with transfer functionG ðs Þ ¼ YX ððssÞÞ ¼ s 2 +2s + 2.

Remarks ‘num’ and ‘den’ are numerator and denominator polynomials defined by the coefficients. ‘tf’ is the transfer function that defines the system ‘sys’. After invoking the command ‘step(sys)’, the plot may be better presented by labeling the x- and y-axes, a title for the plot, and grids. ‘tfinal’ is the total time for computation.

Example G.2 Compute and plot the unit step response of the system defined by the transfer function GðsÞ ¼

Y ðsÞ 2 : ¼ XðsÞ s2 + s + 2

MATLAB Commands: num¼[2]; den¼[1 1 2]; sys ¼tf(num,den); step(sys, 100) The step response function is shown in Fig. G.7.

G.7 Computing eigenvalues and eigenvectors To determine the eigenvalues of a square matrix A, use the command eig(A). To find the eigenvalues and unit eigenvectors, use the command: [U, D] ¼ eig(A). The columns of U are unit eigenvectors and the diagonal elements of D are the eigenvalues of A. Try the matrix A ¼ [2 1;1 2] to compute its eigenvalues and eigenvectors.

APPENDIX G Matlab and Simulink: A brief tutorial

Remarks •

Plotting the simout variable #n, where n is the integer defining the variable number.

plot(tout,simout(:,n)) Two plots on the same graph: plot(tout, simout(:,1),’-‘, tout, simout(:,2),’–‘). (solid ‘-‘, and dashed ‘–‘lines). Same approach holds for multiple plots on the same graph. • A plot may be edited by using the Tools option or by clicking on the edit icon. You may want to change the line style (dotted, dashed, etc.), add symbols, or add color to the graph. • Saving the system matrices and other parameters. Define all the matrices, for example, A, B, C, and D. Then save as follows in MATLAB: Save filename A B C D Note that there are no commas separating the variable names. If you do not specify variable names, then save filename would save all the variables that are in the MATLAB workspace. Example: save F:\Example\SecondOrderSystem A B C D • Before starting the simulation load the stored file. You may want to check the parameter values in this file before running the simulation. Example: load F:\Example\SecondOrderSystem A B C D.

Useful Hint: You need to ensure that the length of the variable ‘tout’ (number of time points) matches the length of the system state variables. Do the following before you run the simulation. • • • • •

In your Simulink graphical model, click on ‘Simulation’. Then click on ‘Configuration Parameters’. On the left column of this window, select ‘Data Import/Export’ and click on it. Then go down to the ‘save options’. Disable the block marked ‘list data points to last’. This may not be seen in the newer version of MATLAB-Simulink.

This would ensure that you are not limiting the number of simulation points that are generated. For further reading refer to Ref. [2].

References [1] MATLAB and Simulink User’s Guide, MathWorks, Inc, Natick, MA, 2018. [2] H. Klee, Simulation of Dynamic Systems with MATLAB and Simulink, CRC Press, Boca Raton, FL, 2007.

327