introduccion al simulink_4.pdf

Upload: victorhuqranga

Post on 02-Jun-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 introduccion al simulink_4.pdf

    1/49

    Introduction to Simulink

    Larry Michaels

    [email protected]

    The MathWorks Inc.

    24 Prime Park Way

    Natick, MA 01760-1500

    USA

  • 8/10/2019 introduccion al simulink_4.pdf

    2/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    2Introduction to Simulink

    What Is Simulink?Simulink is an interactive, block-diagram-based

    tool for modeling and analyzing dynamicsystems. It is tightly coupled with MATLAB andsupported by Blocksets and Extensions.

    BlocksetsStateflowToolboxes

    MATLAB

    Real TimeWorkshop Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    3/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    3Introduction to Simulink

    What Is Simulink?A simulation tool in MATLABs Technical

    Computing Environment

    Among other things, this means

    Block diagram editing

    Nonlinear simulation Continuous and discrete simulation

    Asynchronous (non-uniform sampling)

    simulation Integration with MATLAB, Extensions, Blocksets

    & Toolboxes

  • 8/10/2019 introduccion al simulink_4.pdf

    4/49Copyright 1984 - 1997 by The MathWorks, Inc.

    4Introduction to Simulink

    What can you model withSimulink?Just about anything you can model mathematically

    Communicationsand satellitesystems

    Ship systems

    Aircraft and spacecraftdynamics and systems

    Biologicalsystems

    Monetarysystems

    Automotive systems

  • 8/10/2019 introduccion al simulink_4.pdf

    5/49Copyright 1984 - 1997 by The MathWorks, Inc.

    5Introduction to Simulink

    Building & Running a Model

    Creating a model Running the simulation

    Analyzing the results

  • 8/10/2019 introduccion al simulink_4.pdf

    6/49Copyright 1984 - 1997 by The MathWorks, Inc.

    6Introduction to Simulink

    Creating a model Editing models

    Annotations Setting block dialog parameters

    Creating Subsystems

    Signal Labels

    Saving (MDL File format)

  • 8/10/2019 introduccion al simulink_4.pdf

    7/49Copyright 1984 - 1997 by The MathWorks, Inc.

    7Introduction to Simulink

    Open a new model window

    Typingsimulinkwill also open a newmodel window if noother model is open

    simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    8/49Copyright 1984 - 1997 by The MathWorks, Inc.

    8Introduction to Simulink

    Collecting blocks

    Pull the required

    blocks into a newSIMULINK block

    diagram window.

  • 8/10/2019 introduccion al simulink_4.pdf

    9/49Copyright 1984 - 1997 by The MathWorks, Inc.

    9Introduction to Simulink

    Connecting blocks

    Left mousedrag fromport to addline

    Right mouse drag toadd branch line

  • 8/10/2019 introduccion al simulink_4.pdf

    10/49Copyright 1984 - 1997 by The MathWorks, Inc.

    10Introduction to Simulink

    Saving Model {BlockDefaults {}AnnotationDefaults {}System {Block}

    Line {Branch {}

    }}

    }

  • 8/10/2019 introduccion al simulink_4.pdf

    11/49Copyright 1984 - 1997 by The MathWorks, Inc.

    11Introduction to Simulink

    Creating Subsystems

    Rename and save

  • 8/10/2019 introduccion al simulink_4.pdf

    12/49Copyright 1984 - 1997 by The MathWorks, Inc.

    12Introduction to Simulink

    Annotations

    Double click onbackground

    Type in the text

  • 8/10/2019 introduccion al simulink_4.pdf

    13/49Copyright 1984 - 1997 by The MathWorks, Inc.

    13Introduction to Simulink

    Signal labels

    Signal labelswork just likeannotations

  • 8/10/2019 introduccion al simulink_4.pdf

    14/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    14Introduction to Simulink

    Signal labels

    Signal labels canpass through

    "virtual" blocks

  • 8/10/2019 introduccion al simulink_4.pdf

    15/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    15Introduction to Simulink

    Signal labels

  • 8/10/2019 introduccion al simulink_4.pdf

    16/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    16Introduction to Simulink

    Vector I/O and Scalar ExpansionVectorizedInputs and

    Outputs

    Scalar

    expansion ofparameter Wide Vector

    Lines

    Scalar

    expansion ofinputs

  • 8/10/2019 introduccion al simulink_4.pdf

    17/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    17Introduction to Simulink

    Defining variables

    Both MATLAB and Simulink Windows See Intothe Same Workspace.

    x=21;

    t = 0:1

    x=21

    pi=3.14159...

    t=[0 1]

  • 8/10/2019 introduccion al simulink_4.pdf

    18/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    18Introduction to Simulink

    Goto / From blocks

    Local [ ] is onlyon the currentwindow

  • 8/10/2019 introduccion al simulink_4.pdf

    19/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    19Introduction to Simulink

    Goto / From blocksGlobal is allsystems

  • 8/10/2019 introduccion al simulink_4.pdf

    20/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    20Introduction to Simulink

    Goto / From blocksTagVisibilityis here

    Scoped {} is allsystems

    beneath theTag Visibilityblock

  • 8/10/2019 introduccion al simulink_4.pdf

    21/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    21Introduction to Simulink

    Running a simulation

    Parameters dialog box

    Continuous systems

    Discrete systems

    Getting data into models From the command line

  • 8/10/2019 introduccion al simulink_4.pdf

    22/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    22Introduction to Simulink

    Simulating

    First simulation:jagged!

    d i i li k

  • 8/10/2019 introduccion al simulink_4.pdf

    23/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    23Introduction to Simulink

    Setting parameters

    I d i Si li k

  • 8/10/2019 introduccion al simulink_4.pdf

    24/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    24Introduction to Simulink

    Simulating SecondSimulation:Much Better...

    Use:Variable-step discrete time solverMax. Step Size = 0.1

    I t d ti t Si li k

  • 8/10/2019 introduccion al simulink_4.pdf

    25/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    25Introduction to Simulink

    Setting parameters

    ht tstop start

    max =

    50

    I t d ti t Si li k

  • 8/10/2019 introduccion al simulink_4.pdf

    26/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    26Introduction to Simulink

    Available Solvers Variable-step solvers

    Modify step size during simulation

    Error control and zero crossing detection ode45, ode23, ode113, ode15s, ode23s, discrete

    Fixed-step solvers

    Same step size throughout simulation

    No error control or zero crossing detection

    ode5, ode4, ode3, ode2, ode1, discrete

    Simulink selects a default solver ode45 used for models with continuous states

    Variable-step discrete-time solver used for models

    with no continuous states

    27Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    27/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    27Introduction to Simulink

    Getting data into and out ofmodels

    Applies toInports and

    Outports on toplevel only

    28Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    28/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    28Introduction to Simulink

    Continuous models

    Ordinary differential equations

    F mx bx kx= + +

    xF

    yu ms bs k

    = =

    + +

    12

    k

    b

    x(t)

    F(t)

    m

    [m b k]

    29Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    29/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    29Introduction to Simulink

    Spring-Mass-Damper

    30Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    30/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    30Introduction to Simulink

    Discrete models

    For discrete

    models, usea fixed-stepintegrator

    31Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    31/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    31Introduction to Simulink

    An interest rate exampleLet's look at what happens if you gain 6%, 7%and 8% interest on a $1000 investment over 50

    years

    M Mn n= 1 06 1. M0 1000=

    WOW!!!

    That's a

    $28,000difference!

    32Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    32/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    32Introduction to Simulink

    Example: Fibonacci

    xxnn==xxnn--11 ++ xxnn--22xx11 = 1, x= 1, x22 = 1= 1

    Generate theGenerate the FibonacciFibonacci sequence withsequence with SimulinkSimulinkusing unit delay, summing junction, and tousing unit delay, summing junction, and to

    workspace blocks, create a system that willworkspace blocks, create a system that willgenerate the first 20 numbers in this series.generate the first 20 numbers in this series.

    A chambered nautilus is

    really an example of thegolden rectangle which isthe Fibonacci sequence.

    33Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    33/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    33Introduction to Simulink

    Solution: Fibonacci

    34Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    34/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    34

    Enabled subsystemsAn enabled subsystem is executed at eachsimulation step for which the control signal to thesubsystem is positive.

    Outputs may be held

    or resetStates may be heldor reset

    35Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    35/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    35

    Triggered subsystemsA triggered subsystem executes each time a triggerevent occurs. The execution is for one time step only.

    A triggered event may

    occur on rising, fallingor both rising and fallingedges of signal.

    36Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    36/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    36

    Triggered and enabledsubsystems

    Trigger event

    Isthe enableinput signal

    > 0?

    No Don't execute thesubsystem

    Yes

    Execute thesubsystem

    37Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    37/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    From the command line

    Why simulate from the command line?

    Automate repeated simulations

    Parameter tuning Parameter Optimization (NCD)

    38Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    38/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    From the command line(The most basic method)

    [t,x,y]=sim(model)

    [t,x,y1,y2,, yn]=sim(model)

    Simulates the model using all of the modelparameters.

    The time, state and output vectors are returnedfrom the simulation.

    Individual outputs may be obtained.

    Outputs are obtained from the outport blocks onthe top level of the model.

    39Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    39/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    From the command line

    options=simget('external_pendulum')

    options =

    Solver: 'ode45'RelTol: 1.0000e-003

    AbsTol: 1.0000e-006Refine: 1

    MaxStep: 0.1000InitialStep: 'auto'MaxOrder: 5FixedStep: 'auto'

    OutputPoints: 'all'OutputVariables: ''

    MaxRows: 1000Decimation: 1

    InitialState: []FinalStateName: 'xFinal'

    Debug: 'off'Trace: ''

    SrcWorkspace: 'current'

    DstWorkspace: 'current'ZeroCross: 'on'

    Use simget and simset toget and set the options

    structure.

    [t,x,y]=sim model, timespan, options)

    options is a datastructure that containsall of the solver

    parameters The options structure

    overrides the simulationparameters settings

    40Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    40/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    From the command line

    [t,x,y]=sim model, timespan, options, ut)

    ut allows external inputs to be supplied to the inports

    on the top level of the model. [] in place of timespan or options indicates that the

    current model settings are used.

    41Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    41/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    Analyzing results

    Getting data out of models Floating Scope and Display blocks

    Trim

    Linearization

    42Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    42/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    Getting data out of modelsThe Scope, ToWorkspace,ToFile and Parameters

    Dialog all have similaroptions

    Remember:

    These blocks use the data

    created by the solvers

    43Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    43/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    The floating Scope and Displayblocks

    Selecting any signal willdisplay the value(s) of that

    signal on any floatingscope or display block onthat diagram

    Unselected

    44Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    44/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    The floating Scope and Displayblocks

    Selected

    45Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    45/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    The floating Scope and Displayblocks

    You can even select multiple signals...

    Selected

    46Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    46/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    Trim Find the values of states x and inputs u that

    bring the system to steady state (dx=0)

    The solution is not necessarily unique

    [x, u, y]=trim('sys')

    To provide an initial guess at the solution[x, u, y]=trim('sys', x0, u0, y0)

    Individual elements of x,u,y can be constrained

    Individual derivatives can be fixed to non-zerovalues

    Can also add a fourth output argument to get the

    state derivatives at trim ([x,u,y,dx]=)

    47Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    47/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    Linearization Use linmod to extract linear models from

    Simulink systems

    Perturbs each state around the operating point todetermine the rate of change in the state

    derivatives and outputs (Jacobians)

    Use linmod2 for advanced linearization

    attempts to balance roundoff and truncation error

    perturbation levels of each state-space matrix

    element set individually

    can detect discontinuities and produce warning

    messages

    48Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    48/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    Discrete linearization - dlinmod

    Used for discrete, multi-rate and hybrid systems

    One additional input argument, Ts, after thesystem name

    [Ad, Bd, Cd, Dd]=dlinmod('sys',Ts,x,u)

    Use Ts=0 for a continuous model approximation

    49Introduction to Simulink

  • 8/10/2019 introduccion al simulink_4.pdf

    49/49

    Copyright 1984 - 1997 by The MathWorks, Inc.

    Summary Block diagram modeling

    Interactive operation

    Continuous/discrete/hybrid systems

    Linear & nonlinear components

    Hierarchical models - subsystems Conditional execution

    Command-line operation

    Tightly integrated with MATLAB

    Linearization for Control Design

    Supported by Stateflow, Real-Time Workshop