SIMULINK Prepared by : Ehab Naser Abd_Elmonem Sowaidan Introduction to Simulink • Simulink is a graphical, “drag and drop” environment for building simple and complex signal and system dynamic simulations • It allows users to concentrate on the structure of the problem, rather than having to worry (too much) about a programming language • The parameters of each signal and system block is configured by the user Advantages of Simulink Model and Simulate Your System : explore a wide design space by modeling the system under test and the physical plant Test Early and Often : reduce expensive prototypes by testing your system under conditions that are otherwise too risky or time-consuming to consider Automatically Generate Code : instead of writing thousands of lines of code by hand, automatically generate production-quality C and HDL code that behaves the same way as the model you created in Simulink. Then deploy it directly onto your embedded processor or FPGA/ASIC Advantages of Simulink One of the main advantages of Simulink is the ability to model a nonlinear system, which a transfer function is unable to do Another advantage of Simulink is the ability to take on initial conditions where in transfer function the initial conditions are assumed to be zero SIMULINK BASICS A systems is represented by a block diagram You select the block from a library of blocks You need to know the library that contains the block that you need click on the library name and you will see a list of blocks in that library click on the block that you want and drag it to the work file click on the block to select its parameters Connect it to other blocks Launch Simulink First launch MATLAB. To open Simulink, type simulink at the MATLAB command window or click on the Simulink icon on the MATLAB toolbar Simulink Block Libraries Simulink block libraries Simulink provides a library browser that allows you to select blocks from libraries of standard blocks : Continuous - blocks that describe linear functions Discontinuities - blocks that describe nonlinear functions Discrete - blocks that describe discrete-time components logic and bit operations - blocks that describe logic operation Math operations - blocks that describe general mathematics functions Sinks - blocks that display or write block output Look up Tables Model Verification Model Wide Utilities Ports & Subsystems Signal Attributes Signal Routing User Defined Functions Additional Math & Discrete Create a new model • Click the new-model icon in the upper left corner to start a new Simulink file Your workspace Library of elements Model is created in this window Example 1 • Build a Simulink model that solves the differential equation x 3 sin 2t • Initial condition x(0) 1 Simulation diagram • Input is the function 3sin(2t) • Output is the solution of the differential equation x(t) x(0) 1 3sin(2t) (input) x 1 s x x(t) (output) integrator • Now build this model in Simulink Select an input block Drag a Sine Wave block from the Sources library to the model window Select an operator block Drag an Integrator block from the Continuous library to the model window Select an output block Drag a Scope block from the Sinks library to the model window Connect blocks with signals • Place your cursor on the output port (>) of the Sine Wave block • Drag from the Sine Wave output to the Integrator input • Drag from the Integrator output to the Scope input Arrows indicate the direction of the signal flow Select simulation parameters Double-click on the Sine Wave block to set amplitude = 3 and freq = 2 This produces the desired input of 3sin(2t) Select simulation parameters Double-click on the Integrator block to set initial condition = -1 This sets our IC x(0) = -1 Run the simulation In the model window, from the Simulation pulldown menu, select run View the output x(t) in the Scope window. Simulation results To verify that this plot represents the solution to the problem, solve the equation analytically The analytical result, x(t ) 12 32 cos2t matches the plot (the simulation result) exactly Example 2 Build a Simulink model to get the response of the system • • • • 2nd-order mass-spring-damper system zero initial conditions put f(t) is a step with magnitude 3 parameters: m = 0.25, c = 0.5, k = 1 mx cx kx f (t ) From equation the system model f(t) input + - mx 1 m x cx kx 1 s x 1 s x x c k x x(t) output Drag a Sum block from the Math library Double-click to change the block parameters to rectangular and + - - Drag a Gain block from the Math library Double-click to change the block parameters The gain is 4 since 1/m=4 add a title Drag Integrator blocks from the Continuous library ICs on the integrators are zero. Add a scope from the Sinks library Connect output ports to input ports Label the signals by double-clicking on the leader line Drag new Gain blocks from the Math library To flip the gain block, select it and choose Flip Block in the Format pull-down menu. Double-click on gain blocks to set parameters c=0.5 , k=1.0 Connect from the gain block input backwards up to the branch point Re-title the gain blocks Double-click on Step block to set parameters. For a step input of magnitude 3, set Final value to 3 Final Simulink model Results Underdamped response. Overshoot of 0.5. Final value of 3 Is this expected? Analysis based on the equations of motion • Standard form • Natural freq • Damping ratio • Static gain x k m c 1 x x f (t ) k k k n 2.0 m 2 c 0.5 n k 1 K 1 k Check simulation results • Damping ratio of 0.5 is less than 1 – Expect the system to be underdamped – Expect to see overshoot • Static gain is 1 – Expect output magnitude to equal input magnitude – Input has magnitude 3, so does output • Simulation results conform to expectations