29
NHP MÔN Simulink INTRODUCTION TO Simulink Prepared by Hai Au Version 1 - July 2007

NHẬP MÔN Simulink

Embed Size (px)

Citation preview

Page 1: NHẬP MÔN Simulink

NHẬP MÔN Simulink

INTRODUCTION TO Simulink

Prepared by Hai Au

Version 1 - July 2007

Page 2: NHẬP MÔN Simulink

Copyright © 2007 Hai Au - CA Group Email: [email protected] Website: http://www.dieukhien.net

Page 3: NHẬP MÔN Simulink

i

Contents Overview ..........................................................................................................................................1 Learning Outcomes ..........................................................................................................................1 1. Background of Simulink® ...........................................................................................................2

1.1 What Is Simulink?..................................................................................................................2 1.2 Simulink Library Browser......................................................................................................2 1.3 Concept of Signal and Logic Flow.........................................................................................4 1.4 Connecting Blocks .................................................................................................................6

2. Computer Simulation of Dynamic Systems with Simulink® ......................................................7 2.1 Create and Run a Simulink Model .........................................................................................7

3. Commonly Used Simulink Blocks.............................................................................................21 4. Summary ....................................................................................................................................23 References ......................................................................................................................................23 Additional Exercises ......................................................................................................................25

Page 4: NHẬP MÔN Simulink

ii

This page is intentionally left blank.

Page 5: NHẬP MÔN Simulink

1

Introduction to Simulink® Overview Simulink® is a graphical application program accompanied with MATLAB and working in the MATLAB environment. Although the MATLAB package is useful for linear system analysis Simulink is far more useful for control system simulation. Simulink enables the rapid construction and simulation of control block diagrams. Learning Outcomes After reading this chapter, you should be able to

• Get started with Simulink® • Explain how a Simulink model works • Create and run simple Simulink models. • Solve differential equations with Simulink®.

Page 6: NHẬP MÔN Simulink

2

1. Background of Simulink® 1.1 What Is Simulink? Simulink, a companion program to MATLAB, is an interactive system for simulating linear and nonlinear dynamic systems. It is a graphical mouse-driven program that allows you to model a system by drawing a block diagram on the screen and manipulating it dynamically. It can work with linear, nonlinear, continuous-time, discrete-time, multivariable, and multi-rate systems. 1.2 Simulink Library Browser The first step is to start up MATLAB on the computer you are using. Type >>simulink in the Command Window or click the button in the Toolbar menu of the MATLAB Window. The Simulink Library Browser window will appear as that shown in Figure A2.1.

Figure A2.1 Simulink library browser (MATLAB 7.1, Simulink 6.3)

Page 7: NHẬP MÔN Simulink

3

Simulink® 6.3 has a number of additional options. There are several groups of Simulink blocks in the Simulink icon such as Commonly Used Blocks, Continuous, Discontinuities, Math Operations, Sinks and Sources, etc. Selecting Commonly Used Blocks will provide a list of blocks shown in Figure A2.2.

Figure A2.2 A list of blocks in Commonly Used Blocks group Selecting Continuous will provide a list of blocks shown in Figure A2.3. The ones that we often use are Transfer Fcn, State-space and Integrator. Selecting the Sources icon yields the library shown in Figure A2.4. The most commonly used sources are Clock (which is used to generate a time vector), Step (which generates a step input), and Constant (that generate a constant function).

Page 8: NHẬP MÔN Simulink

4

The Sinks icon as shown in Figure A2.5 provides a set of Sinks blocks that are used to display simulated results. The most often used blocks may be To Workspace (to which a variable passed is written to a vector in the MATLAB Workspace), Scope (to represent data graphically).

Figure A2.3 A list of blocks in Continuous group 1.3 Concept of Signal and Logic Flow In Simulink, data/information from various blocks is sent to another block by lines connecting the relevant blocks. Signals can be generated and fed into blocks (dynamic / static). Data can be fed into functions. Data can then be dumped into sinks, which could be scopes, displays or could

Page 9: NHẬP MÔN Simulink

5

be saved to a file. Data can be connected from one block to another, can be branched, multiplexed etc. In simulation, data is processed and transferred only at Discrete times, since all computers are discrete systems. Thus, a SIMULATION time step (otherwise called an INTEGRATION time step) is essential, and the selection of that step is determined by the fastest dynamics in the simulated system.

Figure A2.4 A list of blocks in the Sources group

Page 10: NHẬP MÔN Simulink

6

Figure A2.5 A list of blocks in Sinks group 1.4 Connecting Blocks A Simulink model is created based on the idea of block diagram and it consists of several blocks. Necessary blocks for a Simulink model can be selected from the Simulink Library Browser and they are connected together. To connect blocks, left-click and drag the mouse from the output of one block to the input of another block. Figure A2.6 shows the steps involved. The Simulink model in Figure A2.6 consists of the following blocks: Clock, To Workspace1, Step Input, Transfer Fcn and To Workspace2.

Page 11: NHẬP MÔN Simulink

7

Figure A2.6 Connecting blocks

2. Computer Simulation of Dynamic Systems with Simulink® 2.1 Create and Run a Simulink Model The following example illustrates how to create and run a Simulink model. Example A2.1 Create a Simulink model to simulate a signal that have the following form: ( )y 2 3sin t / 2= + ω + π (t = 0 to 10, ω = 2 rad/s) SOLUTION Do the following steps:

• Start Simulink • Open a new model • Select a Constant block (Sources) • Select a Sine Wave block (Sources) • Select a Sum block (Math Operations) • Select a Clock block (Sources) • Select a To Workspace block (Sinks) • Copy the To Workspace block and paste it to a new place. The Simulink model should

look like the one in Figure A2.7. • Connect the block as shown in Figure A2.8. • Save the Simulink model as MyFirstModel.mdl. • Double-click the To Workspace block, change Variable name as t and Save format as

Array as shown in Figure A2.9.

Page 12: NHẬP MÔN Simulink

8

Figure A2.7 A simple Simulink model

Figure A2.8 The Simulink model ‘MyFirstModel.mdl’

Page 13: NHẬP MÔN Simulink

9

Figure A2.9 Change parameters of the To Workspace block

• Change the parameters of the To Workspace1 in the same way with the To Workspace block.

• Double-click the Constant block, change the Constant value to 2. • Double-click the SineWave block, do some changes as shown in Figure A2.10. • Click OK button. • Before running the Simulink model, we need to set simulation parameters by selecting the

Simulation menu → Configuration Parameters. The Configuration Parameters: MyFirstModel/Configurations dialog box will appear as shown in Figure A2.11. In this dialog box we can set some parameters such as Start time 0.0, Stop time: 10.0 (seconds), and Solver: ode45. Click OK button. The Simulink model is ready to run.

• Run the Simulink model by selecting Simulation → Start or click the button. The results (t, y) are put in Workspace.

• Type the following command in MATLAB Command Windows: >>plot(t,y) 2.2 Solving Differential Equations with Simulink Simulink provides with different blocks that can be used to solve numerical solution of ordinary differential equation. The following example illustrates how to solve differential equations with Simulink. Example A2.2 Solving 1st order differential equation for a Liquid Tank System Let us consider a liquid tank system as shown in Figure A.8. The relationship between the liquid level (h) and the input flow rate is represented by the following first-order differential equation:

Page 14: NHẬP MÔN Simulink

10

Figure A2.10 Settings of sine wave signal

Figure A2.11 Configuration Parameter dialog box

Page 15: NHẬP MÔN Simulink

11

Figure A2.12 Graph of ( )y 2 3sin t / 2= + ω + π for Example A2.1

Figure A2.13 Liquid level system

idhAR h Rqdt

+ = (where A = 2.5 m2, R = 10 m-2sec and qi = 0.1 m3/s)

Level h

Outlet flow qo

Inlet flow qi Inlet valve

Outlet valve R

Page 16: NHẬP MÔN Simulink

12

SOLUTION The above equation can be rewritten in the following form:

i1 1h h q

AR A= − +

The Integrator block can be used to integrate its input ( h ) and produces h. Other blocks needed in this model include a Gain block and a Sum block. To generate a step signal, use the Step block. View the output using a Slope block. Again, to put data (including time) in Workspace we can use a Clock block and a To Workspace block. The Simulink model for the liquid tank system is shown in Figure A2.14.

Figure A2.14 Simulink model for Example A2.2

Running this Simulink model we obtain the results as shown in Figures A2.15 and A2.16

Figure A2.15 View result by Scope block

Page 17: NHẬP MÔN Simulink

13

0 20 40 60 80 100 120 140 160 180 2000

20

40

60

80

100

120Tank System

Time (seconds)

Liqu

id le

vel (

cm)

Figure A2.16 Result of Example A2.8

From the above examples it can be seen that Simulink is an excellent interactive environment for simulation and analysis of control systems. Simulink enables the rapid creation of block diagrams based on transfer functions, followed by simulation for a given input signal. To facilitate model definitions, Simulink has a block diagram window in which blocks are created from the Simulink library browser and edited primarily by implementing drag-and-drop commands using a mouse. Blocks can be configured as additive transfer functions or as a multiplicative transfer function, simply by connecting the output of one block to the input of another block. The coefficients of descending powers of s of the numerator and denominator polynomials in each block are entered as vectors. Time delays (also called transport delays in Simulink) can be inserted in series with blocks for rational transfer functions. Input signals, called sources, include step, sinusoidal, and random inputs, but not the pulse function. Clicking on the input block allows the user to specify the time when the input changes from an initial value of zero, and, for a step input, its initial and final values. Example A2.3 Use of Transfer Fcn block and Transport Delay block Consider a dynamic system consisting of a single output Y and two input U and D:

D)s(G)s(U)s(G)s(Y dp += (A2.1) where

1s15s50e2)s(G 2

s5

p ++=

(process transfer function) (A2.2)

Page 18: NHẬP MÔN Simulink

14

1s15e3.0)s(G

s5

d +=

(disturbance transfer function) (A2.3)

In order to build a Simulink model for simulation of the dynamic system described by (A2.1), we need to use a number of blocks from the Simulink library such as Sources, Sinks and other appropriate blocks. Figure A2.17 shows the Simulink model for equation (A2.1) (transport delay 1 = 5 for both models). To generate a transient response, the simulation menu is selected to allow parameters for the simulation to be specified (start time, stop time, integration routine, maximum integration step size) as shown in Figure A2.18. Numerical values of time t are entered into the input-output data set via a Clock block. After the simulation has been completed, the resulting data can be plotted (see Figure A2.19), manipulated, and analysed from the MATLAB command window.

Figure A2.17 Simulink block diagram for equation (A2.1)

Page 19: NHẬP MÔN Simulink

15

Figure A2.18 Configuration parameters dialogue box

0 10 20 30 40 50 60 70 800

0.5

1

1.5

2

2.5

Figure A2.19 Response for simultaneous unit step changes at t = 0 in U and D from the Simulink diagram in Figure A2.17.

Page 20: NHẬP MÔN Simulink

16

To simulate a closed-loop system, the procedure is somewhat more involved than for an open-loop system. Changing the previous example somewhat, start with Figure A2.17, but let Gd = Gp. Click on the connection between the U block and the Process Transfer Fcn block, and delete it. Rename the U block, Ysp. This block will be used to produce a step change in the set point.

Figure A2.20 Partially completed closed-loop diagram Place a copy of the Sum block to the right of Ysp. Double click above the Sum icon and label it Sum1. Open its dialogue box and change the ++ sequence to +-. The top left input will have a + located to the right of it, while the bottom input of Sum1 and dragging the arrow to the line following the output of Sum. The output of Sum1 is the error between the set point Ysp and the controlled variable Y. This leads to the block diagram in Figure A2.20. To insert the controller, right-click the Simulink Extras block. Click on the Additional Linear block; then select the PID Controller and the output of PID controller to the input of the Process Transfer Fn. Double click on PID controller settings are entered in the expanded form as P, I and D where P = KP, I = KP/TI and D = KPTD. Thus, the numerical values of P, I and D should reflect these definitions. The model developed above presents the closed-loop system, as shown in Figure A2.21. Text can be added to the block diagram simply by double-clicking on a point in the diagram and typing the desired words (see Figure A2.21).

Page 21: NHẬP MÔN Simulink

17

Figure A2.21 Closed-loop diagram

0 5 10 15 20 25 30 35 40 45 500

0.5

1

1.5

t

y

Figure A2.22 Unit step response for the closed-loop system in Figure A2.21 with P = 1.65, I =

0.23 and D = 2.97

Page 22: NHẬP MÔN Simulink

18

0 5 10 15 20 25 30 35 40 45 50-0.1

0

0.1

0.2

0.3

0.4

0.5

0.6

t

y

Figure A2.23 Closed-loop response for a unit step disturbance

Now simulate the unit response to a unit step disturbance. Double-click on Ysp and set Final value to 0. Double-click on D and set Final value to 1. Again, select Start from the Simulation menu to begin the simulation. Type plot(t,Y) to view the response. Figure A2.23 shows the resulting disturbance response plot after modifying some of the labels. Simulink can be used to simulate the effects of different control strategies with realistic multivariable process models such as a distillation column or a furnace. Example A2.4: Damper-Spring-Mass System Let’s consider the above mass-spring-damper system as shown in Figure A2.24. A mechanical shown in the following figure is at rest before excitation force Psinωt is given, derive the complete solution y(t). The displacement y is measured from the equilibrium position. Assume that the system is under-damped. Use values of P = 20 N, 0.1f = Hz ( f2π=ω ), m = 200 kg, b=100 Ns/m, k = 600 N/m, y(0) = 0 and )0(y = 0. SOLUTION The differential equation of motion for the system is as follows: ukyybym =++ (A2.4)

Page 23: NHẬP MÔN Simulink

19

The transfer function is

kbsms1

)s(U)s(Y)s(G 2 ++== (A2.5)

Figure A2.24 Spring-mass-damper system

P = 20.0; % N f = 1.0; % Hz m = 200.0; % kg b = 100.0; % Ns/m k = 600.0; % N/m ome = 2*pi*f; % rad/s u(t) is P*sin(ome*t)

From equations (A2.4) and (A2.5), we can make simulation programs in different ways with Simulink. We try to use the Integrator block and Transfer Function block as follows. Integrator block: Equation (2.4) can be rewritten as

um1y

mky

mby +−−= (A2.6)

m

u

y b

k

Page 24: NHẬP MÔN Simulink

20

Figure A2.25 Simulink model using Integrator blocks Results:

0 1 2 3 4 5 6 7 8 9 10-10

0

10

20

Time (seconds)

Dis

plac

emen

t (m

m)

0 1 2 3 4 5 6 7 8 9 10-40

-20

0

20

40

Time (seconds)

Vib

ratio

n ve

loci

ty (m

m/s

)

Figure A2.26 Simulated results from the above Simulink model

Page 25: NHẬP MÔN Simulink

21

Transfer Function block:

Figure A2.27 Simulink model using Transfer Function block

Figure A2.28 Simulated result from the Simulink model in Figure A2.27 3. Commonly Used Simulink Blocks Table A2.1 summarise a list of commonly used simulink blocks which we will the most often use in our course.

Page 26: NHẬP MÔN Simulink

22

Table A2.1 Summary of Commonly Used Simulink Blocks

Block icon Name Use

Continuous

State-Space Implement a linear state-space system

Transfer Fcn Implement a linear transfer function

Math Operations

Derivative Merge scalar, vector or matrix signals

Divide Multiply or divide inputs

Function Apply a specified expression to the input

Gain Multiplies the input by a constant

value (gain)

Integrator Integrate the input signal

Math Function Perform a mathematical function

Product Multiply inputs

Sum Add or subtract inputs

Transport Delay Delay the input by a given amount of time

Signal Routing

Demux Split vector signals into scalars or

smaller vectors

Page 27: NHẬP MÔN Simulink

23

Mux Extract and output the elements of a

bus or vector signal

Sinks

Scope Display signals generated during a

simulation

To Workspace Write data to the workspace

XY Graph Display an X-Y plot of signals using a

MATLAB figure window

Sources

Clock Generate a time vector

Constant Generate a constant

Ramp Output a ramp signal

Sine Wave Generate a sine wave signal

Step General a step signal

4. Summary Simulink is a very powerful block diagram simulation language. Simple simulations can be set up rapidly. The aim of this tutorial was to provide enough of an introduction to get you started on the development of simulations for dynamic systems. With experience, the development of these simulations will become second nature. It is recommended that you perform the simulations shown in this tutorial as well as the additional exercises, to rapidly acquire these simulation skills. References Bequette, B.W. (1998), Process Control – Modeling, Design and Simulation, Prentice Hall Inc.,

Upper Saddle River, NJ. Chin, C. (2007). Engineering Mathematics II Lecture Notes, AMC. Nguyen, H.D. (2007). Marine and Offshore Systems Simulation and Diagnostics Lecture Notes,

AMC. Nguyen, H.D. (2007). Instrumentation and Process Control Lecture Notes, AMC.

Page 28: NHẬP MÔN Simulink

24

Seborg, D.E., Edgar, F.T., and Mellichamp, D.A. (2005), Process Dynamics and Control, John Wiley and Sons Inc., Hobokwn, NJ, USA.

MathWorks websites: MathWorks: http://www.mathworks.com/access/helpdesk/help/techdoc/index.html MathWorks: http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/ MathWorks: http://www.mathworks.com/access/helpdesk/help/pdf_doc/simulink/sl_gs.pdf MathWorks: http://www.mathworks.com/access/helpdesk/help/pdf_doc/simulink/sl_using.pdf

Page 29: NHẬP MÔN Simulink

25

Additional Exercises 1. A dynamic systems are expressed by the following transfer functions:

(a) ( ) 1H ss 1

=+

(b) ( ) 2

1.117s 3.1472G ss 4.6429s 5.3821− +

=+ +

Create a Simulink model to simulate the systems when a unit step input applies. 2. The following figure shows an RLC network:

Figure A2.29 RLC network where R = resistance (Ω), L = inductance (H) and C = capacitance (F), ei(t) and eo(t) are input voltage (V) and output voltage (V), respectively. (a) Write a differential equation relating ei(t) and eo(t). Assuming that all initial conditions are zero, write down the transfer function for the network and find its poles and zeros if R = 50 Ω, L = 0.1 H and C = 1000 µF. Make a Simulink model to simulate the network. (b) Assuming that in order to control the output voltage as desired the above RLC network is connected to an amplifier with gain of Ka and a PID controller with control gains Kp, Ki and Kd, and eo(t) is fed back to the PID controller through a voltmeter with a sensitivity of Km (V/V) as illustrated in the following figure:

draw a block diagram for the closed-loop control system and write down the total feedback transfer function. Modify the Simulink model you have made in (a) to simulate the whole system and use the simulation program to select values of control gains such the step response has mall overshoot.

ei(t) eo(t) i

R

C

L

ei(t) eo(t) i

R

C

L

Amplifier

Volmeter

RLC Network

Set-point

Feedback

PID Controller