Simulink-02-01.ppt

  • Upload
    jeco009

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

  • 8/10/2019 Simulink-02-01.ppt

    1/53

    Department of Mechatronics Engineering

    Wahyudi Martono, Dr. Eng.Department of Mechatronics EngineeringInternational Islamic University Malaysia

    Module IV:SIMULINK and Neuro-Fuzzy Toolboxes

    MATLAB & SIMULINKwith Applications

  • 8/10/2019 Simulink-02-01.ppt

    2/53

    Department of Mechatronics Engineering

    Session 2:Getting Started with Simulink

    SIMULINK

  • 8/10/2019 Simulink-02-01.ppt

    3/53

    Department of Mechatronics Engineering

    Session Outlines

    Subsytems

    Masking

    Discrete System

  • 8/10/2019 Simulink-02-01.ppt

    4/53

    Department of Mechatronics Engineering

    Subsystems

    What is Subsystems?We may make a very complex system resulting in a verycomplex model.

    As subprogram (subroutine or function) of otherprogramming language, Simulink provides an analogouscapability called subsystems.

    Using subsystems, we can make the models easier to

    develop, to understand and to maintain.

  • 8/10/2019 Simulink-02-01.ppt

    5/53

    Department of Mechatronics Engineering

    Subsystems

    Method of Creating Subsystems

    There are two method for creating subsystems:

    Encapsulating a SubsystemA portion of existing model is encapsulated to bea subsystem.

    Use Subsystem blocks from Signals & System

    Library.

  • 8/10/2019 Simulink-02-01.ppt

    6/53

    Department of Mechatronics Engineering

    Subsystems

    Encapsulating SubsystemsSuppose we want to make our primitive model of mass-spring-damper as a subsystem.

  • 8/10/2019 Simulink-02-01.ppt

    7/53Department of Mechatronics Engineering

    Subsystems

    Encapsulating SubsystemsTo encapsulate a portion of a Simulink model, select allthe blocks and signal line to be included in thesubsystem using a bounding box using our mouse.

    bounding box

  • 8/10/2019 Simulink-02-01.ppt

    8/53Department of Mechatronics Engineering

    Subsystems

    Encapsulating SubsystemsChoose Create Subsystemunder Editmenu.

    New subsystem

  • 8/10/2019 Simulink-02-01.ppt

    9/53Department of Mechatronics Engineering

    Subsystems

    Encapsulating SubsystemsTo view or edit a subsystem, double-click the block. Anew window will appear.

  • 8/10/2019 Simulink-02-01.ppt

    10/53Department of Mechatronics Engineering

    Subsystems

    Subsystems BlocksIf, when building a model, we know that we will need asubsystem, it will more convenient to build a subsystemin a subsystem directly.

    Drag a Subsystem Blockfrom Ports & Subsystem

    Library to the model window.

  • 8/10/2019 Simulink-02-01.ppt

    11/53Department of Mechatronics Engineering

    Subsystems

    Subsystems BlocksDouble click the subsystem to open subsystem window.

    Build subsystem using the standard procedure.

  • 8/10/2019 Simulink-02-01.ppt

    12/53Department of Mechatronics Engineering

    Subsystems

    Subsystems Blocks

    Use Inport Blocks for all signals entering subsystem andOutport Blocks for all signals leaving the subsystem.(Both blocks are from Port & Subsytem Library)

    You may change the label of Inport and Outpor. Closethe subsystem window if we finished building subsystem.

  • 8/10/2019 Simulink-02-01.ppt

    13/53

    Department of Mechatronics Engineering

    Masking

    Concept

    Masking permits us to treat a subsystem as if it was asimple block.

    A mask is a custom user interface for a subsystemthat hides the subsystems contents, making it appear

    to the user as an atomic block with its own icon andparameter dialog box.

  • 8/10/2019 Simulink-02-01.ppt

    14/53

    Department of Mechatronics Engineering

    Masking

    Concept

    We make a masked subsystem for the following reason:There is an algorithm which we want to hide from

    other user. To prevent unintended modification of subsystems byhiding their contents behind a mask

    Lets build a spring-mass masked block.

  • 8/10/2019 Simulink-02-01.ppt

    15/53

    Department of Mechatronics Engineering

    Masking

    Open a new model workspace, and drag the subsystemof the primitive model into it.

  • 8/10/2019 Simulink-02-01.ppt

    16/53

    Department of Mechatronics Engineering

    Masking

    Select the subsystem model and then choose Maskunder Editmenu from the new model window menu bar.

  • 8/10/2019 Simulink-02-01.ppt

    17/53

    Department of Mechatronics Engineering

    Masking

    Mask Editor dialog box consists of four tabbed pagesIconInitializationParameters

    Documentation

  • 8/10/2019 Simulink-02-01.ppt

    18/53

    Department of Mechatronics Engineering

    Masking

    Mask Editor Documentation Page

    The page consists of threefield. All of the fields areoptional. You may enter asshown in the figure.

    The Documentation pane enables you to define ormodify the type, description, and help text for a maskedblock.

  • 8/10/2019 Simulink-02-01.ppt

    19/53

    Department of Mechatronics Engineering

    Masking

    Mask Editor Parameters PageThe page is used to set parameters of block insubsystem underlying the masked block.

    For our mass-spring-damper, we have the followingparameters:Mass, symbol: m, editable by userDamper, c, editableSpring, k, editable

    To add a variable, click addiconthen enter all thenecessary field.

    add icon

    delete icon

  • 8/10/2019 Simulink-02-01.ppt

    20/53

    Department of Mechatronics Engineering

    Masking

    Mask Editor Parameters Page

  • 8/10/2019 Simulink-02-01.ppt

    21/53

    Department of Mechatronics Engineering

    Masking

    Mask Initiation PageThe Initialization pane allows you to enter MATLABcommands that initialize the masked subsystem.

    Simulink executes the initialization commands when it Loads the model Starts the simulation or updates the block diagram Rotates the masked block Redraws the blocks icon (if the masks icon creation

    code depends on variables defined in theinitialization code)

  • 8/10/2019 Simulink-02-01.ppt

    22/53

    Department of Mechatronics Engineering

    Masking

    Mask Initiation Page

    For our example, leave this page emptysince we want tomake the parameters are editable through blockparameters.

    For example, if we wantour masked system hasthe following value:

    m=1;c=2;k=10Type that parameters in

    initialization commands.

  • 8/10/2019 Simulink-02-01.ppt

    23/53

    Department of Mechatronics Engineering

    Masking

    Mask Editor Icon PageThe Mask Editors Icon pane enables you to create icons thatcan contain descriptive text, state equations, images, andgraphics.

    Commands that draw icon

    Example of drawing Commands

    we can use to draw the block icon.

    Parameters that control icon

    appearance.

  • 8/10/2019 Simulink-02-01.ppt

    24/53

    Department of Mechatronics Engineering

    Masking

    Mask Editor Icon Page

    Drawing commandsThis field allows us to enter commands that draw the

    blocks icon. Simulink provides a set of commands thatcan display text, one or more plots, or show a transferfunction. We may use the following commands: text,plot, displ, dpoly, etc as shown in the example

    command. We must use these commands to draw youricon.

  • 8/10/2019 Simulink-02-01.ppt

    25/53

    Department of Mechatronics Engineering

    Icon optionsThese controls allow you to specify the attributes of theblock icon.

    Frame Transparency

    Rotation Unit

    Masking

    Mask Editor Icon Page

  • 8/10/2019 Simulink-02-01.ppt

    26/53

    Department of Mechatronics Engineering

    MaskingMasking

    Mask Editor Icon PageLet draw the following figure for our masked subsystem.

    Set the following icon

    option:Frame: VisibleTransparency: OpaqueRotation: fixed

    Unit: Normalized

  • 8/10/2019 Simulink-02-01.ppt

    27/53

    Department of Mechatronics Engineering

    plot([0.1 0.1],[0.2 0.8])plot([0.4 0.7 0.7 0.4 0.4],[0.2 0.2 0.8 0.8 0.2])

    plot([0.1 0.2],[0.4 0.4])plot([0.3 0.2 0.2 0.3],[0.3 0.3 0.5 0.5])plot([.1 .2 .2 .3 .3 .4],[.7 .7 .8 .6 .7 .7])plot([0.3 0.4],[0.4 0.4])plot([0.7 0.9],[0.5 0.5])

    text(.5,.5,'m')text(.25,.35,'c')text(.25,.85,'k')text(.8,.6,'f')

    MaskingMasking

    Mask Editor Icon PageType the following commands into commands field

  • 8/10/2019 Simulink-02-01.ppt

    28/53

    Department of Mechatronics Engineering

    MaskingMasking

    Mask Editor Icon PageWe will get the following masked subsystem:

  • 8/10/2019 Simulink-02-01.ppt

    29/53

    Department of Mechatronics Engineering

    MaskingMasking

    Mask Editor Icon PageWe may also displaying a tif image on a block icon usingthe following drawing command:

    image(imread(file_name.tif))

    ki

  • 8/10/2019 Simulink-02-01.ppt

    30/53

    Department of Mechatronics Engineering

    Close the mask editor, now, our subsystem look likeblocks of Simulink library. Double-click to see the effectof the masking.

    Masking

    ki

  • 8/10/2019 Simulink-02-01.ppt

    31/53

    Department of Mechatronics Engineering

    Masking

    Mask Editor Documentation Page

    M ki

  • 8/10/2019 Simulink-02-01.ppt

    32/53

    Department of Mechatronics Engineering

    Masking

    Mask Editor Documentation Page

    Di S

  • 8/10/2019 Simulink-02-01.ppt

    33/53

    Department of Mechatronics Engineering

    Discrete System

    Concept

    Discrete system is dynamical system that can bedescribed using difference equation.

    For example, lets consider the following system:

    The process is assumed to have a sample time of 0.1sec.

    )k(u)1k(x094.0)1k(xsin05.0)k(x

    )1k(x1.0)1k(x)k(x

    212

    211

    Di t S t

  • 8/10/2019 Simulink-02-01.ppt

    34/53

    Department of Mechatronics Engineering

    Discrete System

    Concept

    Simulink has the ability tosimulate discrete system,

    including system whosecomponents operate atdifferent rates (multi rate)and system that mix discrete

    and continuous components(hybrid system).

    Di t S t

  • 8/10/2019 Simulink-02-01.ppt

    35/53

    Department of Mechatronics Engineering

    Discrete System

    Lets build a model and simulate the following system:

    )k(u)1k(x094.0)1k(xsin05.0)k(x

    )1k(x1.0)1k(x)k(x

    212

    211

    We may construct a Simulink model by usingprimitive blockstransfer function blockstate variable model.

    The process is assumed to have a sample time of 0.1sec.

    Di t S t

  • 8/10/2019 Simulink-02-01.ppt

    36/53

    Department of Mechatronics Engineering

    Primitive Blocks

    Discrete System

    We can build a block diagram as the following figure:

    0.094

    u(k)

    ++ z

    10.1

    x2(k)

    x1(k)

    z

    1

    x2(k-1) x

    1(k-1)

    ++

    0.05sin[x1(k-1)]

    -

    (Sources)

    (User-defined functions)

    (Discrete)

    (Math) (Math)

    Di t S t

  • 8/10/2019 Simulink-02-01.ppt

    37/53

    Department of Mechatronics Engineering

    Primitive Blocks

    Discrete System

    Therefore, we have the following Simulink model.

    Next, we should define the f(u) function and sampletime.

    Di t S t

  • 8/10/2019 Simulink-02-01.ppt

    38/53

    Department of Mechatronics Engineering

    Discrete System

    Double click the f(u) block, then modify the desiredfunction and sampling time. Click OK to close thewindow.

    0.05*sin(u)

    0.1

    Primitive Blocks

    Di t S t

  • 8/10/2019 Simulink-02-01.ppt

    39/53

    Department of Mechatronics Engineering

    Discrete System

    The Simulink model become as follows:

    Primitive Blocks

    Di t S t

  • 8/10/2019 Simulink-02-01.ppt

    40/53

    Department of Mechatronics Engineering

    Double click the unit delayblocks, then set sample timeto 0.1.

    Do the same process forother blocks: sum, gain andsource.

    Discrete System

    Primitive Blocks

  • 8/10/2019 Simulink-02-01.ppt

    41/53

    Discrete System

  • 8/10/2019 Simulink-02-01.ppt

    42/53

    Department of Mechatronics Engineering

    Discrete System

    Lets build a model and simulate the following system:

    )k(u)1k(x094.0)1k(xsin05.0)k(x

    )1k(x1.0)1k(x)k(x

    212

    211

    In z-domain,

    )z(U)z(Xsin05.0094.0z

    z)z(X

    )z(X1z

    1.0

    )z(X

    12

    21

    Transfer Function

    Discrete System

  • 8/10/2019 Simulink-02-01.ppt

    43/53

    Department of Mechatronics Engineering

    Discrete System

    Then we can construct the following block diagram:

    We can make the discrete model easily based on thisblock.

    (Math)(Discrete)

    (Sources)

    (Sinks)(User-defined functions)

    F(X1)

    U(z) X2(z)

    +- 094.0zz

    X1(z)1zz1.0

    Transfer Function

    Discrete System

  • 8/10/2019 Simulink-02-01.ppt

    44/53

    Department of Mechatronics Engineering

    Discrete System

    Therefore, we have the following Simulink model.

    Next, we should define the f(u) function and sampletime.

    Transfer Function

    Discrete System

  • 8/10/2019 Simulink-02-01.ppt

    45/53

    Department of Mechatronics Engineering

    Discrete System

    Double click the f(u) block, then modify the desiredfunction and sampling time. Click OK to close thewindow.

    0.05*sin(u)

    0.1

    Transfer Function

    Discrete System

  • 8/10/2019 Simulink-02-01.ppt

    46/53

    Department of Mechatronics Engineering

    Discrete System

    The Simulink model become as follows:

    Transfer Function

    Discrete System

  • 8/10/2019 Simulink-02-01.ppt

    47/53

    Department of Mechatronics Engineering

    Discrete System

    Double click the discretetransfer functions, thenmodify the transfer functionparameters and sample time.

    [1 0]

    [1 -0.094]

    0.1

    [0.1 0]

    [1 1]

    0.1

    Transfer Function

    Discrete System

  • 8/10/2019 Simulink-02-01.ppt

    48/53

    Department of Mechatronics Engineering

    Discrete System

    The Simulink model become as follows:

    Transfer Function

    Discrete System

  • 8/10/2019 Simulink-02-01.ppt

    49/53

    Department of Mechatronics Engineering

    Discrete System

    Before we run the Simulink model, we should carefullydetermine the step size of the simulation parameterespecially when we use fix step.

    In general, we can select the step size at integer ofmultiples of the shortest systems sample time.

    In our example, the sample time is 0.1 so we canselect 0.1 as step size. Then we can simulate themodel.

    Transfer Function

    Discrete System

  • 8/10/2019 Simulink-02-01.ppt

    50/53

    Department of Mechatronics Engineering

    Discrete System

    Simulink also has capability to handel discrete systemswith involve subsystems that operate at different.

    The Simulink sample-time color capability provides help

    in keeping track of sample tiems.

    To activates the feature, select Format:Port/SignalsDisplayand then check Sample time colors.

    Multirate Discrete Time

    Discrete System

  • 8/10/2019 Simulink-02-01.ppt

    51/53

    Department of Mechatronics Engineering

    Discrete System

    Hybrid System

  • 8/10/2019 Simulink-02-01.ppt

    52/53

    Department of Mechatronics Engineering

    Hybrid System

    Mixed continuous and discrete systems are composedof both sampled and continuous blocks. Such systemscan be simulated using any of the integration

    methods, although certain methods are more efficientand accurate than others.

    For most mixed continuous and discrete systems, theRunge-Kutta variable-step methods, ode23 and ode45,are superior to the other methods in terms of efficiencyand accuracy.

  • 8/10/2019 Simulink-02-01.ppt

    53/53

    End of Session 2

    Further ReadingUsing Simulink