You are on page 1of 14

Simulink tutorial for SC4031

Zs. Lendek, D. Jeltsema November 2, 2007

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, simulate 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 dierent conditions.

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 ows 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.

Simulink tutorial

2 CASCADED TANKS SYSTEM USING SIMULINK BLOCKS

The dierential equations describing the dynamics of this system are the following: 2gh2 (t) s1 2gh1 (t) S1 S1 Fin,2 (t) s2 2gh2 (t) h2 (t) = S2 S2 in,2 (t) = Fin,2 (t) + Qs u(t), F h1 (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 ow-rate provided by the pump. The parameter meanings and their nominal values are: Symbol g S1 , S2 s1 s2 Qs Parameter acceleration due to gravity cross-sectional area of the tanks outlet area of the lower tank outlet area of the upper tank input to ow gain motor time constant Value 981 5.3 0.096 0.692 33.6 3 Units cm/s2 cm2 cm2 cm2 cm3 /s/A s s2

Simulink basics

Simulink allows building a dynamical system using standard building blocks, organized into libraries. 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 save resulting signals, it can be directed into sinks. Simulink can be started by simply typing simulink in Matlab. An overview of the Simulink libraries 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 visualizing results), User Dened Functions and Math Operations. Some blocks from these directories are also available in the directory Commonly Used Blocks. To open a new Simulink model, select from File NewModel or click on the New icon from the Simulink window.

Cascaded Tanks System Using Simulink Blocks

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

Standard Simulink Blocks

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

2 of 14

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 set to zero. Since the height of both tanks is positive and nite, saturation limits have to be 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 expressions such as Fin Qs u, 2gh, 1/ . 4. A square root can be implemented by selecting a Math Function from Math Operations 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 were obtained for a unit input u and a simulation time of 10 seconds.

3 of 14

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

Simulink tutorial

2 CASCADED TANKS SYSTEM USING SIMULINK BLOCKS

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

Fcn Blocks

Fcn Blocks can be found in the User Dened Functions. A Fcn Block allows the use of standard Matlab functions and accepts as input a vector u, in which the input parameters are gathered. To gather parameters in a single vector, use Mux (muliplexer) from Commonly Used Blocks. The number of inputs can be changed by double-clicking on the block and changing the value in the parameters (see Figure 5). The inputs will appear in the output vector from top to bottom.

Figure 5: Changing number of inputs in a mux-block. Select a Fcn Block for each dierential equation. Remember that the input variable of the function block is u, which contains the parameters. Remember the order of the parameters in the vector! Now, dene the equations. For example, the derivative of the ow-rate, with u[1] being the input and u[2] the ow-rate, is dened as shown in Figure 6.

Figure 6: The equation of the ow-rate using an fcn block. The full Simulink model together with the obtained simulation results is presented in Figure 7. This model is already signicantly less complex than the one shown in Figure 4.

5 of 14

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) are applied 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

Simulink tutorial

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
1

Combining Matlab and Simulink


Workspaces

Simulink models have access to variables dened in the current workspace. This makes it possible to dene g, S1 , S2 , s1 , s2 , Qs and in Matlabs workspace (see Figure 10) and refer to them in the Simulink model. This is particularly useful when a parameter appears in several places and/or the model has to be simulated for several values of a parameter.

Figure 10: Values are dened in Matlab. Since the function blocks have access to the parameters dened in the workspace, in the function block the exact values are not required. Instead, the values are modied to the 7 of 14

Simulink tutorial

COMBINING MATLAB AND SIMULINK

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

Figure 11: Values are modied 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 scopes 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 vector to Matlab, giving it the name tout. The resulting model is presented in Figure 13. After simulation, the variables appear in the Matlab workspace (see Figure 14).

8 of 14

Simulink tutorial

COMBINING MATLAB AND SIMULINK

Figure 13: Output variables redirected to workspace.

Figure 14: Output variables are available in the workspace.

M-les and Models

It is possible to simulate a model from the Matlab prompt or an m-le (a script or a function). Care must be taken that the variables needed for simulation are dened in the current workspace. The simplest case is when the model is simulated from the Matlab prompt using the sim command (see Figure 15). The sim function returns t and x where t is a time vector and x is a matrix containing the data redirected to the workspace. For the cascaded tank model, x = [Fin,2 h2 h1 ]. The parameters can also be dened in an m-le and the model simulated from this le. For instance, in order to compare the results of Fin,2 for dierent values of Qs , we can write an m-le which simulates the model repeatedly, one iteration for each value of Qs . A script for simulating and plotting the results in one gure is presented in Figure 16. The simulation results are presented in Figure 17. The same simulation can also be done from a function. Simulink models can also call Matlab functions. For this, rst write the needed Matlab function, then select Matlab Function 9 of 14

Simulink tutorial

4 CONFIGURING THE SIMULATION

Figure 15: Model is simulated from Matlab.

Figure 16: Model is simulated from a script. from User-Dened Functions, and change the function name in the block parameters to the correct name (Figure 18). For instance, assume that a predened time-varying input is required, which can be easily be generated by a Matlab function. Such a model is presented in Figure 19.

Conguring the Simulation

To set the simulation parameters, including numerical integration methods for solving dierential equations, choose Simulation Conguration Parameters or press Ctrl-E. This opens the dialog box presented in Figure 20. The Simulation time panel contains elds to enter the start and stop times. The default value for the Start time is zero, and for the 10 of 14

Simulink tutorial

4 CONFIGURING THE SIMULATION

0.7 18 20 22 24 26

0.6

0.5

0.4

0.3

0.2

0.1

10

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: xed step (Figure 21) and variable step (Figure 22). Fixed step-size solvers allow the changing of the step size (sampling period). For variable step-size solvers the maximum, minimum and initial step-sizes can be modied. These values should be chosen according to the system dynamics. In general, variable-step solvers are more ecient, but xed-step solvers allow the control of length of the data vector. For a detailed description of the variable step numerical integration methods, see the Matlab help (e.g., help ode45). Of the xed step-size solvers, ode1 is Eulers method, ode2 and ode4 are the second, respectively fourth order Runge-Kutta methods, while ode3 and 11 of 14

Simulink tutorial

4 CONFIGURING THE SIMULATION

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

Figure 20: Conguration parameters dialog box. ode5 are the xed step versions of ode23 and ode45, respectively. In general, ode45 is a good rst choice. Note, however, that for sti systems ode15s or ode23s have to be used. If a 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 Rajagopalan http://rclsgi.eng.ohio-state.edu/courses/labs/simulink tut rev.pdf Basic Simulink tutorial from Carnegie Mellon University http://www.library.cmu.edu/ctms/ctms/simulink/basic/basic.htm 12 of 14

Simulink tutorial

4 CONFIGURING THE SIMULATION

Figure 21: Solvers with xed step-size.

Figure 22: Solvers using variable step-size. Simulink Tutorials, from the University of Michigan http://www.engin.umich.edu/group/ctm/working/mac/ Simulink Tutorial, Tom Nguyen http://edu.levitas.net/Tutorials/Matlab/Simulink/

13 of 14

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 a nonlinear element for which the associated force can be written as: Fs (x) = k1 x + k2 x3 , with x being the displacement of the spring. The damping coecient d is constant. The dynamics of 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 of k2 : 0, 0.1, 0.5, 1, 5. 3. k1 = 1, k2 = 0, Fin is a step. Compare the velocities obtained for the following values of 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

You might also like