tutorial

14
Simulink tutorial for SC4031 Zs. Lendek, D. Jeltsema November 2, 2007 1 Introduction Simulink is a software add-on for Matlab, a mathematical tool developed by Mathworks (www.mathworks.com). Simulink can be used to graphically model a dynamical system, sim- ulate it, and look at the simulation results. Dynamical systems can be constructed by using standard building blocks available in the Simulink libraries, thus avoiding the need of coding various mathematical functions and integration algorithms. It is assumed that the reader has elementary knowledge of Matlab and Simulink. In this tutorial, a cascaded tank system will be implemented in Simulink and simulated for different conditions. 1 A Simulation Problem The simulation problem considered in this tutorial is the following. Two tanks are connected in a cascade system as shown in Figure 1. Water is pumped from a reservoir into the upper tank. From the upper tank, the water flows into the lower tank and from the lower tank back into the reservoir. The system has one control input u, which is the current applied to the motor of the pump. Figure 1: Cascaded tank system. 1

Upload: ihsan-hamawand

Post on 29-Nov-2014

81 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Tutorial

Simulink tutorial for SC4031

Zs. Lendek, D. Jeltsema

November 2, 2007

1 Introduction

Simulink is a software add-on for Matlab, a mathematical tool developed by Mathworks(www.mathworks.com). Simulink can be used to graphically model a dynamical system, sim-ulate it, and look at the simulation results. Dynamical systems can be constructed by usingstandard building blocks available in the Simulink libraries, thus avoiding the need of codingvarious mathematical functions and integration algorithms. It is assumed that the reader haselementary knowledge of Matlab and Simulink.

In this tutorial, a cascaded tank system will be implemented in Simulink and simulatedfor different conditions.

1 A Simulation Problem

The simulation problem considered in this tutorial is the following. Two tanks are connectedin a cascade system as shown in Figure 1. Water is pumped from a reservoir into the uppertank. From the upper tank, the water flows into the lower tank and from the lower tank backinto the reservoir. The system has one control input u, which is the current applied to themotor of the pump.

Figure 1: Cascaded tank system.

1

Page 2: Tutorial

Simulink tutorial 2 CASCADED TANKS SYSTEM USING SIMULINK BLOCKS

The differential equations describing the dynamics of this system are the following:

h1(t) =s2

√2gh2(t)S1

− s1

√2gh1(t)S1

h2(t) =Fin,2(t)

S2− s2

√2gh2(t)S2

τFin,2(t) = −Fin,2(t) + Qsu(t),

where

• h1 is the height of the water in the lower tank,

• h2 is the height of the water in the upper tank,

• Fin,2 is the flow-rate provided by the pump.

The parameter meanings and their nominal values are:

Symbol Parameter Value Unitsg acceleration due to gravity 981 cm/s2

S1, S2 cross-sectional area of the tanks 5.3 cm2

s1 outlet area of the lower tank 0.096 cm2

s2 outlet area of the upper tank 0.692 cm2

Qs input to flow gain 33.6 cm3/s/Aτ motor time constant 3 s

2 Simulink basics

Simulink allows building a dynamical system using standard building blocks, organized intolibraries. Data are sent from one block to another by lines connecting the respective blocks.Signals can be generated from sources and fed into blocks. To visualize, display or saveresulting signals, it can be directed into sinks.

Simulink can be started by simply typing simulink in Matlab. An overview of the Simulinklibraries available will appear as shown in Figure 2.

The main libraries used for this course are: Continuous (for continuous-time systems),Discrete (for discrete-time systems), Sources (for signal generation), Sinks (for visualiz-ing results), User Defined Functions and Math Operations. Some blocks from thesedirectories are also available in the directory Commonly Used Blocks.

To open a new Simulink model, select from File→ New→Model or click on the Newicon from the Simulink window.

2 Cascaded Tanks System Using Simulink Blocks

Assume for the moment that s1, s2, S1, S2, g, τ , Qs and the input u are all constants.

1 Standard Simulink Blocks

Using standard Simulink building blocks, the system can be implemented in the following way.

2 of 14

Page 3: Tutorial

Simulink tutorial 2 CASCADED TANKS SYSTEM USING SIMULINK BLOCKS

Figure 2: Simulink libraries

1. Select Integrators from Commonly Used Blocks or Continuous to obtain h1, h2

and Fin,2 from h1, h2 and Fin,2. The initial conditions of the integrators should be setto zero. Since the height of both tanks is positive and finite, saturation limits have tobe introduced (see Figure 3). The blocks can be put in the model using drag-and-drop.

2. Select Constant blocks from Sources for the input u and change its value.

3. Select Add and Gain blocks from Commonly Used Blocks to implement expressionssuch as Fin −Qsu, 2gh, 1/τ .

4. A square root can be implemented by selecting a Math Function from Math Oper-ations and changing the function to square root.

5. Select Scopes from Sinks to visualize h1, h2 and Fin,2.

6. Connect the blocks.

The resulting model should look like the one in Figure 4. The model consists of three parts,corresponding to: the pump, the upper tank and the lower tank. The simulation results wereobtained for a unit input u and a simulation time of 10 seconds.

3 of 14

Page 4: Tutorial

Simulink tutorial 2 CASCADED TANKS SYSTEM USING SIMULINK BLOCKS

Figure 3: Setting the parameters of the integrator.

Figure 4: Simulink model of the cascaded tanks system.

4 of 14

Page 5: Tutorial

Simulink tutorial 2 CASCADED TANKS SYSTEM USING SIMULINK BLOCKS

The main problem with this kind of implementation, especially when large and complicatedsystems are involved, is that, once implemented, the Simulink model is hard to understandand debug. To avoid such problems, it is often desirable to simplify the model by using FcnBlocks.

2 Fcn Blocks

Fcn Blocks can be found in the User Defined Functions. A Fcn Block allows the use ofstandard Matlab functions and accepts as input a vector u, in which the input parameters aregathered. To gather parameters in a single vector, use Mux (muliplexer) from CommonlyUsed Blocks. The number of inputs can be changed by double-clicking on the block andchanging the value in the parameters (see Figure 5). The inputs will appear in the outputvector from top to bottom.

Figure 5: Changing number of inputs in a mux-block.

Select a Fcn Block for each differential equation. Remember that the input variableof the function block is u, which contains the parameters. Remember the order of theparameters in the vector! Now, define the equations. For example, the derivative of theflow-rate, with u[1] being the input and u[2] the flow-rate, is defined as shown in Figure 6.

Figure 6: The equation of the flow-rate using an fcn block.

The full Simulink model together with the obtained simulation results is presented inFigure 7. This model is already significantly less complex than the one shown in Figure 4.

5 of 14

Page 6: Tutorial

Simulink tutorial 2 CASCADED TANKS SYSTEM USING SIMULINK BLOCKS

Figure 7: Simulink model using fcn blocks.

It is also possible to use a single integrator (see Figure 8). In this case, care must be taken,since, if the values are not given as a vector, the same saturation limits (initial conditions) areapplied to all the states.

Figure 8: Using a single integrator.

Note: Fcn blocks always return scalars. If the result is a vector, use Matlab Function

6 of 14

Page 7: Tutorial

Simulink tutorial 3 COMBINING MATLAB AND SIMULINK

instead. With this block, a general, continuous-time nonlinear system, expressed as:

x = f(x, u)y = h(x, u),

can be implemented in Simulink as shown in Figure 9.

Figure 9: A general nonlinear system.

3 Combining Matlab and Simulink

1 Workspaces

Simulink models have access to variables defined in the current workspace. This makes itpossible to define g, S1, S2, s1, s2, Qs and τ in Matlab’s workspace (see Figure 10) and referto them in the Simulink model. This is particularly useful when a parameter appears in severalplaces and/or the model has to be simulated for several values of a parameter.

Figure 10: Values are defined in Matlab.

Since the function blocks have access to the parameters defined in the workspace, in thefunction block the exact values are not required. Instead, the values are modified to the

7 of 14

Page 8: Tutorial

Simulink tutorial 3 COMBINING MATLAB AND SIMULINK

corresponding symbols (for the expression of Fin,2, see Figure 11). The resulting data canalso be redirected to workspace. To Workspace blocks can be found in the Sinks library.The output variable’s name and format can be chosen after double-clicking on the block (seeFigure 12). For this case, Array is a convenient choice.

Figure 11: Values are modified to symbols and parameters are read from the current workspace.

Figure 12: Choosing output variable parameters.

Note: Data can also be saved to the workspace from the scope’s parameters.Sending the data to workspace is especially useful if the data has to be compared to

some other data or when printing a plot. Simulink also automatically sends the time vectorto Matlab, giving it the name tout. The resulting model is presented in Figure 13. Aftersimulation, the variables appear in the Matlab workspace (see Figure 14).

8 of 14

Page 9: Tutorial

Simulink tutorial 3 COMBINING MATLAB AND SIMULINK

Figure 13: Output variables redirected to workspace.

Figure 14: Output variables are available in the workspace.

2 M-files and Models

It is possible to simulate a model from the Matlab prompt or an m-file (a script or a func-tion). Care must be taken that the variables needed for simulation are defined in the currentworkspace. The simplest case is when the model is simulated from the Matlab prompt usingthe sim command (see Figure 15). The sim function returns t and x where t is a time vectorand x is a matrix containing the data redirected to the workspace. For the cascaded tankmodel, x = [Fin,2 h2 h1].

The parameters can also be defined in an m-file and the model simulated from this file.For instance, in order to compare the results of Fin,2 for different values of Qs, we can writean m-file which simulates the model repeatedly, one iteration for each value of Qs. A scriptfor simulating and plotting the results in one figure is presented in Figure 16. The simulationresults are presented in Figure 17.

The same simulation can also be done from a function. Simulink models can also call Mat-lab functions. For this, first write the needed Matlab function, then select Matlab Function

9 of 14

Page 10: Tutorial

Simulink tutorial 4 CONFIGURING THE SIMULATION

Figure 15: Model is simulated from Matlab.

Figure 16: Model is simulated from a script.

from User-Defined Functions, and change the function name in the block parameters tothe correct name (Figure 18).

For instance, assume that a predefined time-varying input is required, which can be easilybe generated by a Matlab function. Such a model is presented in Figure 19.

4 Configuring the Simulation

To set the simulation parameters, including numerical integration methods for solving differ-ential equations, choose Simulation → Configuration Parameters or press Ctrl-E. Thisopens the dialog box presented in Figure 20. The Simulation time panel contains fieldsto enter the start and stop times. The default value for the Start time is zero, and for the

10 of 14

Page 11: Tutorial

Simulink tutorial 4 CONFIGURING THE SIMULATION

0 2 4 6 8 100

0.1

0.2

0.3

0.4

0.5

0.6

0.7

1820222426

Figure 17: Simulation results.

Figure 18: Selecting a Matlab function.

Stop time is 10.0.The Solver options section allows selection of the numerical integration algorithm to use

and to set parameters for the integration step size. Solvers are grouped in two categories: fixedstep (Figure 21) and variable step (Figure 22). Fixed step-size solvers allow the changing ofthe step size (sampling period). For variable step-size solvers the maximum, minimum andinitial step-sizes can be modified. These values should be chosen according to the systemdynamics. In general, variable-step solvers are more efficient, but fixed-step solvers allow thecontrol of length of the data vector.

For a detailed description of the variable step numerical integration methods, see theMatlab help (e.g., help ode45). Of the fixed step-size solvers, ode1 is Euler’s method, ode2and ode4 are the second, respectively fourth order Runge-Kutta methods, while ode3 and

11 of 14

Page 12: Tutorial

Simulink tutorial 4 CONFIGURING THE SIMULATION

Figure 19: Using a Matlab function to generate the required signal.

Figure 20: Configuration parameters dialog box.

ode5 are the fixed step versions of ode23 and ode45, respectively. In general, ode45 is agood first choice. Note, however, that for stiff systems ode15s or ode23s have to be used. Ifa simulation runs extremely slowly with ode45, try ode15s.

Resources

The Mathworks website (for online documentation and tutorials)http://www.mathworks.com

Simulink tutorial, Dr. Gregory Washington and Arun Rajagopalanhttp://rclsgi.eng.ohio-state.edu/courses/labs/simulink tut rev.pdf

Basic Simulink tutorial from Carnegie Mellon Universityhttp://www.library.cmu.edu/ctms/ctms/simulink/basic/basic.htm

12 of 14

Page 13: Tutorial

Simulink tutorial 4 CONFIGURING THE SIMULATION

Figure 21: Solvers with fixed step-size.

Figure 22: Solvers using variable step-size.

Simulink Tutorials, from the University of Michiganhttp://www.engin.umich.edu/group/ctm/working/mac/

Simulink Tutorial, Tom Nguyenhttp://edu.levitas.net/Tutorials/Matlab/Simulink/

13 of 14

Page 14: Tutorial

Simulink tutorial 4 CONFIGURING THE SIMULATION

Exercise

Consider the nonlinear spring-mass-damper system presented in Figure 23. The unit mass

Figure 23: A nonlinear spring-mass damper system.

(m = 1) can move in a vertical direction and a force Fin is exerted on it. The spring is anonlinear element for which the associated force can be written as: Fs(x) = k1x + k2x

3, withx being the displacement of the spring. The damping coefficient d is constant. The dynamicsof the system can be expressed as:

x = v

mv = −Fs(x)− dv + Fin.

Construct the Simulink model and simulate the system for the following cases:

1. k1 = 1, k2 = 0, d = 2, Fin is a step.

2. k1 = 1, d = 2, Fin is a step. Compare the velocities obtained for the following values ofk2: 0, 0.1, 0.5, 1, 5.

3. k1 = 1, k2 = 0, Fin is a step. Compare the velocities obtained for the following valuesof d: 0, 0.1, 0.5, 1, 5.

4. k1 = 1, k2 = 0.2, d = 2. Compare the system responses for the following inputs: impulse,ramp, sin(3t + 5), tanh(t− 10).

14 of 14