You are on page 1of 35

Course Introduction to Matlab and Simulink

Simulink/1

Emanuele Ruffaldi
May 11, 2017
https://github.com/eruffaldi/course_simulink

Scuola Superiore Sant’Anna, Pisa


Simulink Use Cases

Doheny Eye
Nissan  350Z

Airbus  used  Model-­Based  


Design  to  model  the  A380’s  fuel  
management  system,  validate   NASA  X-­43A
requirements  through  simulation,  
and  clearly  communicate  the  
functional  specification [MW]

PERCRO  BE
DLR  Robotics
©  2016  Scuola  Superiore  Sant’Anna
Simulink Features

1. Visual  Programming
– Defining  a  program  by  means  of  a  graphical  
representation  of  the  problem
– Alternative  to  Textual  Programming
2. Model-­based  Simulation
– Connections  to  the  dynamics  and  physics  of  
the  problems
3. From  Simulation  to  Embedding
– Code  Generation
– Live  Connection  to  the  Embedded  Target

©  2016  Scuola  Superiore  Sant’Anna


Simulink History

• Introduced  in  1990  inside  MATLAB  (1984)


• Real-­Time  Workshop  (2002)
• Concepts  are  a  bit  older  (1968)

"Doing With Images Makes Symbols:


Communicating With Computers” (1968)

©  2016  Scuola  Superiore  Sant’Anna


Alternatives

• As concerns MATLAB there are Open


Source alternatives with similar capabilities
(Python+packages, Julia, SciLab) or clones
(Octave).
• As concerns Simulink the most similar
solution is Scicos (INRIA), while Ptolemy II
(Berkeley) is worth mentioning

©  2016  Scuola  Superiore  Sant’Anna


Simulink Material

• Inside  MATLAB
– doc  simulink
• Online  PDF (3000  pages)
• Online  Web

©  2016  Scuola  Superiore  Sant’Anna


Simulink Concepts

• System,  Block,  Signals


• Execution  Modes
• Sampling  Time
• Scopes  and  Logging

©  2016  Scuola  Superiore  Sant’Anna


Starting Simulink

• From  MATLAB  command  line


• simulink
• From  Toolbar  (version  dependent)

• Opening  Simulink  SLX/MDL  file


• Command  line  for  opening  a  model
– modelname
– Open_system(‘modelname’)

©  2016  Scuola  Superiore  Sant’Anna


Simulink Model Window (Design)

Play/Stop Show  values Debug

Model  Browser

Usual  New/Open/Save Model  Explorer


Execution  Mode

Simulation  Duration  (seconds  or  Inf) Library  Browser

Zoom Integrator

©  2016  Scuola  Superiore  Sant’Anna


Visual Programming

What  does  it  mean?

A B

This  is  an  example    of  Dataflow/Graph  based  


Visual  Programming    that  is  alternative  to  the  
approach  called  Block  Visual  Programming
as  used  in  Scratch  (MIT)

©  2016  Scuola  Superiore  Sant’Anna


Simulink Block

Parameters
Input  u(t) Block Output  y(t)

(time) State  x(t)

Example  of  minimal  plot  of  sinus

©  2016  Scuola  Superiore  Sant’Anna


Nature of a Simulink Block

Enabler Trigger Output

OUTSIDE Input

Label

Discrete/Continuous
Type  of  sampling  time
Sample
State
Time
INSIDE
Parameters Costant/Tunable

©  2016  Scuola  Superiore  Sant’Anna


Simulink Block and Lines

• Blocks are  computational  units


• Blocks can  be  non-­virtual  and  virtual
• Blocks  are  connected  by  lines
• Lines  have  the  meaning  of  signals
• Signals  are
• Typed  /  Loggable /  Viewable
• A  Simulink  system  describes  the  time-­based  
relationship  between  blocks  and  their  signals

©  2016  Scuola  Superiore  Sant’Anna


Block Types
• Source  – generates  data
• Sink  – receives  data
• Virtual  Block  – deals  with  logical  structure
• Subsystem  – aggregation  of  blocks  (real  or  virtual)
• Custom  Blocks  (S-­Functions)  – C  or  M-­code  based

Source Output  y(t) Plot,  Store,  Send  to  Network  …,  Goto

Input  u(t) Sink Load  from  File,  Workspace,  Curve


Constant,  …,  Label,  From  Network  …

Input  u(t) Block Output  y(t)

©  2016  Scuola  Superiore  Sant’Anna


Simulink Data Types

• Defined  at  Start  of  Simulation


• Types
– float/double
– various  integers:  [u]int8/16/32
– fixed  types
– boolean
– enumeration
– structures  (BUS  next  lecture)
• Dimensionality:  from  scalar  to  matrices
• Conversion  is  possible

©  2016  Scuola  Superiore  Sant’Anna


Example Data Types
Enter  “datatypedemo”  at  the  MATLAB  command  window

©  2016  Scuola  Superiore  Sant’Anna


Signal Routing

• Signals  can  be  routed


– Mux/Demux
– From/GoTo
– Switch
– Selection

©  2016  Scuola  Superiore  Sant’Anna


Simulink Library Browser

New  Model  (CTRL+N) Search  Block  by  Name  and  Description

Block  list
Library  Tree

The  library  browser  


manages  the  
available  blocks

Explore  it  for   Description


understanding  and  
finding  solutions

©  2016  Scuola  Superiore  Sant’Anna


Library Browser along Lectures

©  2016  Scuola  Superiore  Sant’Anna


Building a Model

Drag  and  Drop  blocks  from  Library  


Browser

or  activate  context  menu  of  block    


and  select  “Add  to  …”

©  2016  Scuola  Superiore  Sant’Anna


Manipulating Blocks

• Selection
• Multiple  Selection  with  Shift
• Multiple  Selection  with  Box  Selection
• Clone
• Drag  with  CTRL  
• Move  Blocks
• Drag
• Rotate  (CTRL+R)
• Connect  Blocks
• Select  first  and  select  second  using  CTRL/CMD
• Branch  line  by  holding  CTRL  from  an  existing  line
• Disconnect  block  by  drag  a  block  holding  SHIFT

©  2016  Scuola  Superiore  Sant’Anna


Simulation

• Execution  of  the  Simulation  from  


startTime to  stopTime times
• Expressed  in  simulation seconds
• Can  be  an  expression
• Can  be  up  to  Infinity  (Inf)
• Can  be  stopped  by  the  “Stop  Block”
• Simulation  is  decomposed  in  Time  Steps  
as  Fixed  or  Variable  steps

©  2016  Scuola  Superiore  Sant’Anna


Running a Model

• Ex:  Play  with  


time  step   Pause/Stop
min/max
• Try  other  
function   Current  Time Integration  Time
Status
generators

©  2016  Scuola  Superiore  Sant’Anna


Invoking Simulation from Matlab

• The  “sim”  command  allows  to  run  a  model  


from  MATLAB  changing  parameters  and  
input  data
– sim(modelname,param,paramvalue…)
– sim(modelname,struct)
• Example  Options
– SimulationMode
– SaveState
– StateSaveName
• The  model(..)  command  allows  for  finer  
control  of  the  Simulation
©  2016  Scuola  Superiore  Sant’Anna
Looking at Results

• Insert  Scope/Floating  Scope/XY  Graph


• Context  menu:  Create  and  Connect  Viewer
• Use  Signal  selector  for  modifying  the  signal  
Signal  selector

Parameters
Autozoom
Properties Zoom

©  2016  Scuola  Superiore  Sant’Anna


Bouncing Ball - Integrator

• Integrates  a  differential  
equation

• Inputs  and  Ports


– Input  (always)
– Reset
– Initial  Condition
– Saturation
– State
©  2016  Scuola  Superiore  Sant’Anna
Bouncing Ball

• This  is  an  example  of  Continuous  System  


with  Simulink

©  2016  Scuola  Superiore  Sant’Anna


Bouncing Ball
sldemo_bounce_two_integrators

©  2016  Scuola  Superiore  Sant’Anna


Heat Example

• From  Mathworks website


• https://it.mathworks.com/help/simulink/gs/
define-­system.html?s_cid=learn_doc

©  2016  Scuola  Superiore  Sant’Anna


Bacteria Example

• Birth  rate  =  b  x
– b  =  1/hour
• Death  rate  =  p x2
– p=0.5

©  2016  Scuola  Superiore  Sant’Anna


Exercise

• Create  a  2D  source  (e.g.  sin  wave  and  )


• Compute  the  polar  coordinates  (modulus  
and  angle)  and  plot  them

©  2016  Scuola  Superiore  Sant’Anna


Exercise

• Cannon  Dynamics  with  bouncing


• Ballistics  with  air  resistance  (1D)
– Fd =  -­D  v|v|
– F  =  Fd – mg  =  ma
• Parameters
– m=0.145kg
– D=0.02
– x0=(0,0)
– v0=(0.1,0.2)  m/s

©  2016  Scuola  Superiore  Sant’Anna


Exercise/2

• Based  on  the  previous  Simulation  modify  


the  initial  conditions  from  Matlab and  use  
the  “sim”  function  to  execute  the  
simulation  and  collect  the  final  end  
position

©  2016  Scuola  Superiore  Sant’Anna


Reference of Blocks in this Lecture

IC  (initial  value) Scope


Signal  Attributes Sinks

Display
Mux/DeMux Sinks
Signal  Routing

Switch STOP
Signal  Routing Sinks

From/GoTo
To  Workspace
Signal  Routing
Sinks
Selector
Signal  Routing

©  2016  Scuola  Superiore  Sant’Anna


Reference of Blocks in this Lecture / 2

Constant Integrator
Sources Continuous

Comparison
Ground Logical
Sources

Sine  Wave Gain


Sources Math

Clock Product/Sum
Sources Math

From  Workspace Trigonometric


Sources Math

SQRT
Math
©  2016  Scuola  Superiore  Sant’Anna

You might also like