Sie sind auf Seite 1von 95

Simulation in Simulink

Simulation in Simulink
Bruce A. Desmarais
Odum Institute for Research in Social Science

February 14, 2009

Simulation in Simulink Introduction

Welcome!
Who am I?

Simulation in Simulink Introduction

Welcome!
Who am I? Bruce Desmarais; Red Sox Fan and PhD student in the UNC Poli Sci Dept.

Simulation in Simulink Introduction

Welcome!
Who am I? Bruce Desmarais; Red Sox Fan and PhD student in the UNC Poli Sci Dept. Extensive experience with matrix algebra, simulation and statistical computing in Matlab and other software/languages

Simulation in Simulink Introduction

Welcome!
Who am I? Bruce Desmarais; Red Sox Fan and PhD student in the UNC Poli Sci Dept. Extensive experience with matrix algebra, simulation and statistical computing in Matlab and other software/languages No formal training in the instruction of Matlab (comments/suggestions will be much appreciated) What will we cover?

Simulation in Simulink Introduction

Welcome!
Who am I? Bruce Desmarais; Red Sox Fan and PhD student in the UNC Poli Sci Dept. Extensive experience with matrix algebra, simulation and statistical computing in Matlab and other software/languages No formal training in the instruction of Matlab (comments/suggestions will be much appreciated) What will we cover? Basic System design and Simulation in Simulink

Simulation in Simulink Introduction

Welcome!
Who am I? Bruce Desmarais; Red Sox Fan and PhD student in the UNC Poli Sci Dept. Extensive experience with matrix algebra, simulation and statistical computing in Matlab and other software/languages No formal training in the instruction of Matlab (comments/suggestions will be much appreciated) What will we cover? Basic System design and Simulation in Simulink Manipulation of Simulink Model les

Simulation in Simulink Introduction

Welcome!
Who am I? Bruce Desmarais; Red Sox Fan and PhD student in the UNC Poli Sci Dept. Extensive experience with matrix algebra, simulation and statistical computing in Matlab and other software/languages No formal training in the instruction of Matlab (comments/suggestions will be much appreciated) What will we cover? Basic System design and Simulation in Simulink Manipulation of Simulink Model les Simulink Interface with Matlab

Simulation in Simulink Introduction

Welcome!
Who am I? Bruce Desmarais; Red Sox Fan and PhD student in the UNC Poli Sci Dept. Extensive experience with matrix algebra, simulation and statistical computing in Matlab and other software/languages No formal training in the instruction of Matlab (comments/suggestions will be much appreciated) What will we cover? Basic System design and Simulation in Simulink Manipulation of Simulink Model les Simulink Interface with Matlab Examples

Simulation in Simulink Introduction

What is Simulink?

Simulation in Simulink Introduction

What is Simulink?

Graphical extension to MATLAB for modeling and simulation of systems; Similar to Winbugs

Simulation in Simulink Introduction

What is Simulink?

Graphical extension to MATLAB for modeling and simulation of systems; Similar to Winbugs Systems drawn on screen as block diagrams

Simulation in Simulink Introduction

What is Simulink?

Graphical extension to MATLAB for modeling and simulation of systems; Similar to Winbugs Systems drawn on screen as block diagrams Integrated with MATLAB

Simulation in Simulink Introduction

What is Simulink?

Graphical extension to MATLAB for modeling and simulation of systems; Similar to Winbugs Systems drawn on screen as block diagrams Integrated with MATLAB Combines intuitive graphical-user interface with excellent speed and storage capabilities

Simulation in Simulink Introduction

What is Simulink?

Graphical extension to MATLAB for modeling and simulation of systems; Similar to Winbugs Systems drawn on screen as block diagrams Integrated with MATLAB Combines intuitive graphical-user interface with excellent speed and storage capabilities Is free to you!! (or included in tuition; however you want to look at it)

Simulation in Simulink Introduction

New Model...First Steps

Open Matlab

Simulation in Simulink Introduction

New Model...First Steps

Open Matlab Type simulink

Simulation in Simulink Introduction

New Model...First Steps

Open Matlab Type simulink Simulink library browser pops up

Simulation in Simulink Introduction

New Model...First Steps

Open Matlab Type simulink Simulink library browser pops up Start a new model le Crtl + N

Simulation in Simulink Introduction

New Model...First Steps

Open Matlab Type simulink Simulink library browser pops up Start a new model le Crtl + N The blank le is where model will be built..save it

Simulation in Simulink Introduction

New Model...First Steps

Open Matlab Type simulink Simulink library browser pops up Start a new model le Crtl + N The blank le is where model will be built..save it Libraries contain building blocks

Simulation in Simulink Basics

Blocks
Model is a collection of blocks connected by lines. There are a number of dierent classes of blocks:

Simulation in Simulink Basics

Blocks
Model is a collection of blocks connected by lines. There are a number of dierent classes of blocks:
Sources: Used to generate signals

Simulation in Simulink Basics

Blocks
Model is a collection of blocks connected by lines. There are a number of dierent classes of blocks:
Sources: Used to generate signals Sinks: Used to output

Simulation in Simulink Basics

Blocks
Model is a collection of blocks connected by lines. There are a number of dierent classes of blocks:
Sources: Used to generate signals Sinks: Used to output Discrete: Linear, discrete-time system elements (transfer-functions)

Simulation in Simulink Basics

Blocks
Model is a collection of blocks connected by lines. There are a number of dierent classes of blocks:
Sources: Used to generate signals Sinks: Used to output Discrete: Linear, discrete-time system elements (transfer-functions) Nonlinear: Arbitrary nonlinear operators

Simulation in Simulink Basics

Blocks
Model is a collection of blocks connected by lines. There are a number of dierent classes of blocks:
Sources: Used to generate signals Sinks: Used to output Discrete: Linear, discrete-time system elements (transfer-functions) Nonlinear: Arbitrary nonlinear operators Connections: Multiplex, Demultiplex

Simulation in Simulink Basics

Blocks
Model is a collection of blocks connected by lines. There are a number of dierent classes of blocks:
Sources: Used to generate signals Sinks: Used to output Discrete: Linear, discrete-time system elements (transfer-functions) Nonlinear: Arbitrary nonlinear operators Connections: Multiplex, Demultiplex

Simulation in Simulink Basics

Blocks
Model is a collection of blocks connected by lines. There are a number of dierent classes of blocks:
Sources: Used to generate signals Sinks: Used to output Discrete: Linear, discrete-time system elements (transfer-functions) Nonlinear: Arbitrary nonlinear operators Connections: Multiplex, Demultiplex Blocks have input and output terminals indicated by wedges pointing towards and away from the block respectively. Lines transmit signals from input to output.

Simulation in Simulink Basics

Lets build our first model

Click on Source

Simulation in Simulink Basics

Lets build our first model

Click on Source Drag the radnom box into the left of the model

Simulation in Simulink Basics

Lets build our first model

Click on Source Drag the radnom box into the left of the model Produces a gaussian signal with set parameters.

Simulation in Simulink Basics

Lets build our first model

Click on Source Drag the radnom box into the left of the model Produces a gaussian signal with set parameters. Return to library main, click on User Defined

Simulation in Simulink Basics

Lets build our first model

Click on Source Drag the radnom box into the left of the model Produces a gaussian signal with set parameters. Return to library main, click on User Defined Drag the Matlab Fcn into the model

Simulation in Simulink Basics

Lets build our first model

Click on Source Drag the radnom box into the left of the model Produces a gaussian signal with set parameters. Return to library main, click on User Defined Drag the Matlab Fcn into the model Return to library main, click on Sinks

Simulation in Simulink Basics

Lets build our first model

Click on Source Drag the radnom box into the left of the model Produces a gaussian signal with set parameters. Return to library main, click on User Defined Drag the Matlab Fcn into the model Return to library main, click on Sinks Drag Scope into the model, connect all

Simulation in Simulink Basics

Block Manipulation
Blocks have many sorts of parameters

Simulation in Simulink Basics

Block Manipulation
Blocks have many sorts of parameters To set a blocks parameters, right-click on a block and select the parameters option

Simulation in Simulink Basics

Block Manipulation
Blocks have many sorts of parameters To set a blocks parameters, right-click on a block and select the parameters option Our gaussian signal has mean and variance and seed parameters. Right click and set them to 0.5 and 1.5 respectively, be sure to note or set seed in rngs

Simulation in Simulink Basics

Block Manipulation
Blocks have many sorts of parameters To set a blocks parameters, right-click on a block and select the parameters option Our gaussian signal has mean and variance and seed parameters. Right click and set them to 0.5 and 1.5 respectively, be sure to note or set seed in rngs The Matlab Function takes a function as a parameter with vector-valued argument u. Lets set it to sin(u) + 1.

Simulation in Simulink Basics

Block Manipulation
Blocks have many sorts of parameters To set a blocks parameters, right-click on a block and select the parameters option Our gaussian signal has mean and variance and seed parameters. Right click and set them to 0.5 and 1.5 respectively, be sure to note or set seed in rngs The Matlab Function takes a function as a parameter with vector-valued argument u. Lets set it to sin(u) + 1. Time arguments tell blocks when to act during a simulation

Simulation in Simulink Basics

Block Manipulation
Blocks have many sorts of parameters To set a blocks parameters, right-click on a block and select the parameters option Our gaussian signal has mean and variance and seed parameters. Right click and set them to 0.5 and 1.5 respectively, be sure to note or set seed in rngs The Matlab Function takes a function as a parameter with vector-valued argument u. Lets set it to sin(u) + 1. Time arguments tell blocks when to act during a simulation The scope box

Simulation in Simulink Basics

A First Run

Simulation in Simulink Basics

A First Run

Open a useful monitor of the simulation by double clicking on the scope box

Simulation in Simulink Basics

A First Run

Open a useful monitor of the simulation by double clicking on the scope box Now lets run the simulation...

Simulation in Simulink Basics

A First Run

Open a useful monitor of the simulation by double clicking on the scope box Now lets run the simulation... First set the simulation parameters typing Ctrl + E

2 3

Simulation in Simulink Basics

A First Run

Open a useful monitor of the simulation by double clicking on the scope box Now lets run the simulation... First set the simulation parameters typing Ctrl + E Many options; lets set the time from 0 to 25

2 3 4

Simulation in Simulink Basics

A First Run

Open a useful monitor of the simulation by double clicking on the scope box Now lets run the simulation... First set the simulation parameters typing Ctrl + E Many options; lets set the time from 0 to 25 Now click on Simulation, Start

2 3 4 5

Simulation in Simulink Basics

Output to Matlab

Simulation in Simulink Basics

Output to Matlab

We often want the simulation data in Matlab

Simulation in Simulink Basics

Output to Matlab

1 2

We often want the simulation data in Matlab Go to the library main, click on sinks

Simulation in Simulink Basics

Output to Matlab

1 2 3

We often want the simulation data in Matlab Go to the library main, click on sinks Drag simout into the model and connect before scope

Simulation in Simulink Basics

Output to Matlab

1 2 3 4

We often want the simulation data in Matlab Go to the library main, click on sinks Drag simout into the model and connect before scope Right click and call the object output

Simulation in Simulink Basics

Output to Matlab

1 2 3 4 5

We often want the simulation data in Matlab Go to the library main, click on sinks Drag simout into the model and connect before scope Right click and call the object output Returns a matlab structure...run the sim and lets take a look

Simulation in Simulink Basics

Output More to Matlab

Simulation in Simulink Basics

Output More to Matlab

Suppose we want to get the signal at dierent points in the simulation

Simulation in Simulink Basics

Output More to Matlab

Suppose we want to get the signal at dierent points in the simulation Drag another simout onto the screen

Simulation in Simulink Basics

Output More to Matlab

Suppose we want to get the signal at dierent points in the simulation Drag another simout onto the screen Lets grab the original gaussian signal

2 3

Simulation in Simulink Basics

Output More to Matlab

Suppose we want to get the signal at dierent points in the simulation Drag another simout onto the screen Lets grab the original gaussian signal Name the second simout output

2 3 4

Simulation in Simulink Basics

Output More to Matlab

Suppose we want to get the signal at dierent points in the simulation Drag another simout onto the screen Lets grab the original gaussian signal Name the second simout output Connect to the line between the signal and the function..run the simulation

2 3 4 5

Simulation in Simulink Basics

Output to File

Simulation in Simulink Basics

Output to File

The best way to assure your simulated data is not lost is to save it immediately

Simulation in Simulink Basics

Output to File

The best way to assure your simulated data is not lost is to save it immediately Go into sinks again and select a untitled.mat le.

Simulation in Simulink Basics

Output to File

The best way to assure your simulated data is not lost is to save it immediately Go into sinks again and select a untitled.mat le. Right click to set le extension...will write to working directory if no extension is provided

2 3

Simulation in Simulink Basics

Output to File

The best way to assure your simulated data is not lost is to save it immediately Go into sinks again and select a untitled.mat le. Right click to set le extension...will write to working directory if no extension is provided The signal can be saved in dierent times along the model path

2 3

Simulation in Simulink Basics

Output to File

The best way to assure your simulated data is not lost is to save it immediately Go into sinks again and select a untitled.mat le. Right click to set le extension...will write to working directory if no extension is provided The signal can be saved in dierent times along the model path Somewhat inconvenient, every time you want a new position on the path, you need a new le.

2 3

Simulation in Simulink Basics

Using Data From Matlab Workspace I

There are many reasons we could want to use data from the matlab workspace

Simulation in Simulink Basics

Using Data From Matlab Workspace I

There are many reasons we could want to use data from the matlab workspace
1

We may want to use data in memory rather than generate it

Simulation in Simulink Basics

Using Data From Matlab Workspace I

There are many reasons we could want to use data from the matlab workspace
1 2

We may want to use data in memory rather than generate it It may be useful to use real-world data in the model.

Simulation in Simulink Basics

Using Data From Matlab Workspace I

There are many reasons we could want to use data from the matlab workspace
1 2 3

We may want to use data in memory rather than generate it It may be useful to use real-world data in the model. It may be dicult to use specic generators/rng streams in the simulation

Simulation in Simulink Basics

Using Data From Matlab Workspace I

There are many reasons we could want to use data from the matlab workspace
1 2 3

We may want to use data in memory rather than generate it It may be useful to use real-world data in the model. It may be dicult to use specic generators/rng streams in the simulation We could use the inputs/outputs of previous simulations as inputs to the current simulation

Simulation in Simulink Basics

Using Data From Matlab Workspace II


All of the objects in the workspace are available

Simulation in Simulink Basics

Using Data From Matlab Workspace II


All of the objects in the workspace are available
1

First, lets put something in the workspace, issue the command x = [1:100; rand(1,100)]

Simulation in Simulink Basics

Using Data From Matlab Workspace II


All of the objects in the workspace are available
1

First, lets put something in the workspace, issue the command x = [1:100; rand(1,100)] Creates a 100x2 matrix with the rst column indicating the time stamp

Simulation in Simulink Basics

Using Data From Matlab Workspace II


All of the objects in the workspace are available
1

First, lets put something in the workspace, issue the command x = [1:100; rand(1,100)] Creates a 100x2 matrix with the rst column indicating the time stamp Right Click and delete the random generator

Simulation in Simulink Basics

Using Data From Matlab Workspace II


All of the objects in the workspace are available
1

First, lets put something in the workspace, issue the command x = [1:100; rand(1,100)] Creates a 100x2 matrix with the rst column indicating the time stamp Right Click and delete the random generator Now go to Sources and drag simin

3 4

Simulation in Simulink Basics

Using Data From Matlab Workspace II


All of the objects in the workspace are available
1

First, lets put something in the workspace, issue the command x = [1:100; rand(1,100)] Creates a 100x2 matrix with the rst column indicating the time stamp Right Click and delete the random generator Now go to Sources and drag simin Right click on simin and tell it to use x

3 4 5

Simulation in Simulink Basics

Using Data From Matlab Workspace II


All of the objects in the workspace are available
1

First, lets put something in the workspace, issue the command x = [1:100; rand(1,100)] Creates a 100x2 matrix with the rst column indicating the time stamp Right Click and delete the random generator Now go to Sources and drag simin Right click on simin and tell it to use x Now lets set the simulation time to 0-250 and run it

3 4 5 6

Simulation in Simulink Basics

Using Data From Matlab Workspace II


All of the objects in the workspace are available
1

First, lets put something in the workspace, issue the command x = [1:100; rand(1,100)] Creates a 100x2 matrix with the rst column indicating the time stamp Right Click and delete the random generator Now go to Sources and drag simin Right click on simin and tell it to use x Now lets set the simulation time to 0-250 and run it Notice anything odd?...extrapolation

3 4 5 6 7

Simulation in Simulink Basics

Scope Plot of Multiple Characteristics

It is easy to visualize how things move together

Simulation in Simulink Basics

Scope Plot of Multiple Characteristics

It is easy to visualize how things move together


1

Go to search, enter mux

Simulation in Simulink Basics

Scope Plot of Multiple Characteristics

It is easy to visualize how things move together


1 2

Go to search, enter mux This block concatenates multiple signals into larger vector-valued signals

Simulation in Simulink Basics

Scope Plot of Multiple Characteristics

It is easy to visualize how things move together


1 2

Go to search, enter mux This block concatenates multiple signals into larger vector-valued signals Drag to the graphic

Simulation in Simulink Basics

Scope Plot of Multiple Characteristics

It is easy to visualize how things move together


1 2

Go to search, enter mux This block concatenates multiple signals into larger vector-valued signals Drag to the graphic Connect the input and output to the two mux input wedges

3 4

Simulation in Simulink Basics

Scope Plot of Multiple Characteristics

It is easy to visualize how things move together


1 2

Go to search, enter mux This block concatenates multiple signals into larger vector-valued signals Drag to the graphic Connect the input and output to the two mux input wedges Run the simulation and observe

3 4 5

Simulation in Simulink Basics

Scope Plot of Multiple Characteristics

It is easy to visualize how things move together


1 2

Go to search, enter mux This block concatenates multiple signals into larger vector-valued signals Drag to the graphic Connect the input and output to the two mux input wedges Run the simulation and observe Look at the Scope

3 4 5 6

Simulation in Simulink Basics

Scope Plot of Multiple Characteristics

It is easy to visualize how things move together


1 2

Go to search, enter mux This block concatenates multiple signals into larger vector-valued signals Drag to the graphic Connect the input and output to the two mux input wedges Run the simulation and observe Look at the Scope Which is which? We need a legend.

3 4 5 6 7

Simulation in Simulink Basics

An Alternative Multiple Characteristic Plot


It is easy to visualize how things move together..again

Simulation in Simulink Basics

An Alternative Multiple Characteristic Plot


It is easy to visualize how things move together..again
1

Right click in model area and choose Signal & Scope manager

Simulation in Simulink Basics

An Alternative Multiple Characteristic Plot


It is easy to visualize how things move together..again
1 2

Right click in model area and choose Signal & Scope manager Under viewers expand Simulink

Simulation in Simulink Basics

An Alternative Multiple Characteristic Plot


It is easy to visualize how things move together..again
1 2 3

Right click in model area and choose Signal & Scope manager Under viewers expand Simulink Double Click Scope

Simulation in Simulink Basics

An Alternative Multiple Characteristic Plot


It is easy to visualize how things move together..again
1 2 3 4

Right click in model area and choose Signal & Scope manager Under viewers expand Simulink Double Click Scope Right click on the scope and choose Edit Signal Connections

Simulation in Simulink Basics

An Alternative Multiple Characteristic Plot


It is easy to visualize how things move together..again
1 2 3 4

Right click in model area and choose Signal & Scope manager Under viewers expand Simulink Double Click Scope Right click on the scope and choose Edit Signal Connections Select the inputs and outputs then close.

Simulation in Simulink Basics

An Alternative Multiple Characteristic Plot


It is easy to visualize how things move together..again
1 2 3 4

Right click in model area and choose Signal & Scope manager Under viewers expand Simulink Double Click Scope Right click on the scope and choose Edit Signal Connections Select the inputs and outputs then close. Run the simulation and double click on the glasses

5 6

Simulation in Simulink Basics

An Alternative Multiple Characteristic Plot


It is easy to visualize how things move together..again
1 2 3 4

Right click in model area and choose Signal & Scope manager Under viewers expand Simulink Double Click Scope Right click on the scope and choose Edit Signal Connections Select the inputs and outputs then close. Run the simulation and double click on the glasses Now right click on the plot space and select legend to get a legend

5 6 7

Das könnte Ihnen auch gefallen