Introduction To Simulink
Introduction To Simulink
Introduction to Simulink
HANS-PETTER HALVORSEN, 2011.06.06
Faculty of Technology, Postboks 203, Kjølnes ring 56, N-3901 Porsgrunn, Norway. Tel: +47 35 57 50 00 Fax: +47 35 57 54 01
Preface
Simulink, developed by The MathWorks, is a commercial tool for modeling, simulating and analyzing
dynamic systems. Its primary interface is a graphical block diagramming tool and a customizable set
of block libraries. It offers tight integration with the rest of the MATLAB environment and can either
drive MATLAB or be scripted from it. Simulink is widely used in control theory and digital signal
processing for simulation and design.
This training will give you the basic knowledge of Simulink and how you can use it together with
MATLAB.
For more information about MATLAB and Simulink, see my Blog: http://home.hit.no/~hansha
Table of Contents
Preface..................................................................................................................................................... 2
2.6 Examples................................................................................................................................ 10
3.1 Comments/Labels.................................................................................................................. 19
iii
iv Table of Contents
8 Subsystems ................................................................................................................................... 40
10 Exercises ....................................................................................................................................... 45
Simulink offers:
A quick way of develop your model in contrast to text based-programming language such as
e.g., C.
Simulink has integrated solvers. In text based-programming language such as e.g., C you
need to write your own solver.
1
2 Start using Simulink
You start Simulink from the MATLAB IDE:
2
3 Start using Simulink
The Simulink Library Browser is the library where you find all the blocks you may use in Simulink.
Simulink software includes an extensive library of functions commonly used in modeling a system.
These include:
Continuous and discrete dynamics blocks, such as Integration, Transfer functions, Transport
Delay, etc.
Math blocks, such as Sum, Product, Add, etc
Sources, such as Ramp, Random Generator, Step, etc
You may now drag the blocks you want to use from the Simulink Library Browser to the model
surface (or right-click on a block and select “Add to…”).
Example:
In this example we place (drag and drop) to blocks, a Sine Wave and a Scope, on the model surface:
When holding the mouse over an input or an output the mouse changes to the following symbol.
Use the mouse, while holding the left button down, to drag wires from the input to the output.
Another wiring technique is to select the source block, then hold down the Ctrl key while left-clicking
on the destination block.
If wire a connection from a wire to another block, like the example below, you need to hold down
the Ctrl key while left-clicking on the wire and then to the input of the desired block.
All standard blocks in Simulink have detailed Help. Click the Help button in the Block Parameter
window for the specific block in order to get detailed help for that block.
The Help Window then appears with detailed information about the selected block:
2.5 Configuration
There are lots of parameters you may want to configure regarding your simulation. Select
“Configuration Parameters…” in the Simulation menu.
Note! Each of the controls on the Configuration Parameters dialog box corresponds to a
configuration parameter that you can set via the “sim” and “simset” commands. You will learn more
about these commands later.
Solvers are numerical integration algorithms that compute the system dynamics over time using
information contained in the model. Simulink provides solvers to support the simulation of a broad
range of systems, including continuous-time (analog), discrete-time (digital), hybrid (mixed-signal),
and multirate systems of any size.
2.6 Examples
Below we will go through some examples in order to illustrate how to create block diagrams and
related functionality.
Create the following model (an integrator) and run the simulation:
Step 2: Configuration
Double-click on the Integrator block. The Parameter window for the Integrator block appears:
Select “Initial condition source=external”. The Integrator block now looks like this:
Double-click on the Constant block. The Parameter window for the Constant block appears:
In the Constant value field we type in the initial value for the integrator, e.g., type the value 1.
Step 3: Wiring
Use the mouse to wire the inputs and outputs of the different blocks.
When holding the mouse over an input or an output the mouse change to the following symbol.
Draw a wire between the output on the Constant block to the lower input in the Integrator block, like
this:
Wire the rest of the blocks together and you will get the following diagram:
Step 4: Simulation
Start the simulation by clicking the “Start Simulation” icon in the Toolbar:
If you want to see the signal dimensions, select “Signal Dimensions” and “Wide Nonscalar Lines” as
shown here:
The thick lines indicate vectors, while the number (8) is the size of the vector.
As you see you may use standard MATLAB functions and syntax.
Run the simulation and see the results in the Scope block.
3.1 Comments/Labels
Double-click on your surface in order to write Labels or Comments in your model block diagram.
19
20 Useful Features
In order to “flip” the input and outputs right-click on the block and select “Flip Block”.
Example:
Double-click on the Scope and select the Parameters icon in the Toolbar:
23
24 Data-driven Modelling
4.2 m-file
It is good practice to build your in Simulink and configure and run the simulation from a MATLAB
m-file.
You use the simset command to configure your simulation parameters and the sim command to run
the simulation.
The variables you refer to in the m-file is set in the Constant value field in the Parameter window for
each block.
simset
sim
Use these commands if you configure and run your Simulink model from a m-file.
Example:
%Simulator Settings
t_stop=100; %[s]
T_s=t_stop/1000; %[s]
options=simset('solver', 'ode5', 'fixedstep', T_s);
%Starting simulation
sim('mass_spring_damper', t_stop, options);
29
30 Hybrid Systems (continuous and discrete)
The black color is the continuous system while the colored part (red and green) is the discrete part of
the system.
In this exercise you will construct a simulation diagram that represents the behavior of a dynamic
system. You will simulate a spring-mass damper system.
̇ ̈
where t is the simulation time, F(t) is an external force applied to the system, c is the damping
constant of the spring, k is the stiffness of the spring, m is a mass, and x(t) is the position of the mass.
̇ is the first derivative of the position, which equals the velocity of the mass. ̈ is the second
derivative of the position, which equals the acceleration of the mass.
The goal is to view the position x(t) of the mass m with respect to time t. You can calculate the
position by integrating the velocity of the mass. You can calculate the velocity by integrating the
31
32 Example: Mass-Spring-Damper System
acceleration of the mass. If you know the force and mass, you can calculate this acceleration by using
Newton's Second Law of Motion, given by the following equation:
Therefore,
Substituting terms from the differential equation above yields the following equation:
̈ ̇
You will construct a simulation diagram that iterates the following steps over a period of time.
6.2 Simulink
Create the block diagram for the mass-spring-damper model above.
Instead of hard-coding the model parameters in the blocks you should refer to them as variables set
in an m-file.
x_init
dxdt_init
m=
c=
k
t_step_F
F_O
F_1
6.3 m-File
The following variables should then be set in the m-file:
6.4 Results
The Block Diagram should look something like this:
Graphs:
Force F
Make sure your MATLAB function is compiled as an embedded MATLAB function using the #eml
directive, e.g.:
36
37 Embedded Algorithms
Double-click on the Embedded MATLAB function give us the standard template for an embedded
function:
Select the part of your system from which you want to create a subsystem, right-click and select
“Create Subsystem”.
Example:
40
41 Subsystems
Right-click on the block and select “Edit Mask” in order to open the Mask Editor:
The Mask Editor allows you to change how the subsystem should look, e.g., the subsystem icon.
Double click on the sub system now gives the Parameter window for the subsystem:
To display the Model Explorer, select Model Explorer from the Simulink View menu.
44
10 Exercises
In this chapter we provide more exercises.
Exercise:
In this exercise we will model a rubber ball that is thrown in the air with an initial velocity of 15 m/s
from a height of 10 m. We will model the dynamics of the ball as it bounces, under the influence of
gravity. We will assume that 20% of the energy is lost on each bounce. (That is, after each impact, the
ball will travel at 80% of its prior velocity, but in the opposite direction.)
We can model this example by integrating g (g = -9.81m/s^2) over time with the initial condition set
to 15 m/s. We reset the integrator each time the position reaches zero meters and set the new initial
condition to -80% of the impact velocity. Position is modeled by integrating the velocity over time
with the initial condition set to 10m/s.
45
46 Subsystems
Faculty of Technology
Kjølnes Ring 56
www.hit.no
E-mail: [email protected]
Blog: http://home.hit.no/~hansha/
Room: B-237a