Introduction to Simulink Mikael Manngård Process Control Laboratory, Åbo Akademi University February 27, 2014 Simulink is an extension to MATLAB that is used for modeling and simulation of dynamic systems. In Simulink, systems are drawn as block diagrams that can be simulated without prior knowledge in programming mathematical solvers. Simulink offers a variety blocks, such as transfer functions, mathematical operators, plotting tools and logical operators that are ready to use. Simulink is integrated within MATLAB and data can easily be transferred between the programs. Contents 1 Starting Simulink 2 2 Library Browser 3 3 Creating a Model 4 3.1 Example 1. Step response of a second order system...... 4 3.2 Example 2. Feedback control.................. 5 4 Simulateing a Model 7 5 Using the MATLAB plot command 7 1
1 Starting Simulink Simulink can be started in several ways, where one is by simply typing simulink in the MATLAB command prompt, or by selecting File New Model. When creating a new model, a blank model window is opened. Our simulink model will be drawn into this window. In Matlab 2012 or newer versions, the Simulink window will look like the following. The library browser that contains all the blocks that can be used, is accessed by clicking on the highlighted icon, in older versions of MATLAB it will look like. 2
2 Library Browser The library browser contains all the operators, and functions that can be used in Simulink. When it is first opened the library browser will look something like the following. In the left hand column, the blocks are organized into folders or so called libraries. The Commonly used blocks library contains (as it says) a set of commonly used blocks, the Contineous library contains contineous time transfer functions, PID-controllers, integrators and derivatives, sources contains input signals such as sinuoids, constants and steps and the sinks library contains scopes and blocks that saves signals to Matlab workspace. Another way to navigate the library is by typing the block names into the search field. 3
3 Creating a Model Simulink models can be created by simply dragging blocks from the library browser into the empty Simulink window. The blocks can either be connected by dragging arrows between them or by ctrl-clicking on the blocks. Blocks can be modified by double clicking in them, which will open new window of block specific settings. The principle of how to model simple dynamic systems is going to be explained by the following examples 3.1 Example 1. Step response of a second order system Assume that we want to simulate a step response for a second order system with the following transfer function G(p) = 3 3p 2 + 2p + 1 when a step change in the input u(t) from 0 to 1 at time step t = 0. To build this model, three blocks are needed, a Step (source), a Transfer Fcn (continuous) and a Scope (sink). The blocks are dragged from the library browser into the model window, and are liked together, this will look somethink like the following. The step size and step time can be modified by double clicking on the Step block, which will open a settings window. In the step time field, insert the time when the step takes place, and in the Initial value and final value fields, define the size of the step. 4
To modify the transfer function, we will double click on the Transfer Fcn block and input the numerator coefficient (3) and denominator coefficients ([3, 2, 1]). The applied settings and final model are displayed below. 3.2 Example 2. Feedback control The second order system described in Example 1 can be controlled with feedback and a PID-controller with controller described as u(t) = (P + I 1p ) + Dp e(t) where e(t) is the control error given by e(t) = r(t) y(t), where y(t) is the output and r(t) is the set point. The parameter P is the controllers proportional gain, I is the integral gain and D is the derivative gain. The controller parameters are chosen as P = 1 and I = 1/3 and D = 2/3. This sytem can be modelled in Simulink by adding a Sum and a PID Controller block to our existing model from example 1. Our step input 5
will now represent the set point r(t), which is set to change from 0 to 1 at t=0. We want a negative feedback (deviation from set point), so the sign in the Sum block needs to be changed from ++ to +. The controller parameters are inserted by double clicking on the PID Controller block. The applied settings and the final model are presented below. 6
4 Simulateing a Model Models can be simulated in Simulink simply by pressing the Run button and the results can be displayed by double clicking the Scope. If you wish to export signals to MATLAB Workspace, the To Workspace block can be used. By double clicking on the block, the variable name can be changed. Remember also to change the format to Array if you wish to save it as a vector in workspace! If the plots seem to be jagged or cut off, you will need to use a smaller step size. This can be achieved by going to Simulation Model Configuration Parameters Max Step Size, and decrease the max step size. Solver type and settings can be change in the same menu if needed. Accurate simulation time can also be saved to workspace by using the Clock and To Workspace blocks. 5 Using the MATLAB plot command Variables saved in MATLABs workspace can be plotted by using the plot command. plot(x, Y ) plots a vector Y versus vector X. Different line types, markers and colours can be obtained with plot(x, Y, S ), where S is a string from any or all of the following columns As an example, for a red dotted line with x markers, the following string will be used: plot(x, Y, rx : ) 7
To insert labels for the x and y axes, the bommands xlabel and ylabel are used. A figure title can be inserted by using the title command and line descriptions can be inserted by the legend command. The step responses for Example 1 and 2 and the matlab scrips are presented below. 8