https://www.halvorsen.
blog
Simulink
Graphical Programming and Simulation with MATLAB
Hans-Petter Halvorsen
What is Simulink?
• Simulink is an “add-on” to MATLAB.
• You need to have MATLAB in order to use
Simulink
• Simulink is used for Simulation of dynamic models
• In Simulink we create a Graphical Block Diagram
for the system (based on the differential
equations(s))
Simulink Model
In Simulink we create and configure graphical blocks and wire them together (based on the differential equations)
Start Simulink from MATLAB
Click the “Simulink” button in the
Toolbar - or type “simulink” in the
Command Window
Simulink Start Page
Simulink Model Editor
Library Browser
Start creating your Simulink Model here
with blocks from the “Simulink Library
Browser” (just “Drag and Drop”)
Simulink Library Browser
Simulink Example
Simulink Example II
Example My First Simulink Model
Model:
We start by drawing a simple Block Diagram
𝑥̇ = 𝑎𝑥 for the model like this (“Pen & paper”):
Where
𝑇 is the Time constant
We will use the following:
We will create and simulate this block
diagram with Simulink
Using ODE Solvers in MATLAB
Step 1: Define the differential equation as a MATLAB function (mydiff.m):
function dx = mydiff(t,x)
T = 5;
Step 2: Use one of the built-in ODE solver
a = -1/T; (ode23, ode45, ...) in a Script.
dx = a*x;
clear
clc
x0
tspan = [0 25];
x0 = 1;
[t,x] = ode23(@mydiff,tspan,x0);
plot(t,x)
tspan
My First Simulink Model
Solution
My First Simulink Model
Start the Simulation by
clicking this icon
Simulation Time
Set Initial Value
See the Simulation Results
Double-Click on the different
Blocks to enter values
Bacteria Population
Here we will simulate a simple model of a bacteria population in a
jar.
The model is as follows:
birth rate=𝑏𝑥
death rate = 𝑝𝑥2
Then the total rate of change of bacteria population is:
𝑥̇ = 𝑏𝑥 − 𝑝𝑥 !
Set b=1/hour and p=0.5 bacteria-hour
→ We will simulate the number of bacteria in the jar after 1 hour,
assuming that initially there are 100 bacteria present.
In MATLAB We would do like this
function dx = bacteriadiff(t,x)
% My Simple Differential Equation
b=1;
p=0.5;
dx = b*x - p*x^2;
clear
clc
tspan=[0 1];
x0=100;
[t,y]=ode45(@bacteriadiff, tspan,x0);
plot(t,y)
[t,y]
Block Diagram for the Model (“Pen and Paper”)
Simulink Block Diagram for the Model
Combining MATLAB and Simulink
Data-driven Modelling
• You may use Simulink together with MATLAB in order to specify
data and parameters to your Simulink model.
• You may specify commands in the MATLAB Command Window or
as commands in an m-file (Script).
• This is called data-driven modeling
• Instead of using values directly we use variables instead - This is
more flexible because we can easily change Simulation Parameters
without touching the Simulink Model
Example Instead of using values directly we use variables instead – This is more flexible
𝑥̇ = 𝑎𝑥 because we can easily change Simulation Parameters without changing the
Simulink Model
Data-driven Modelling
MATLAB Script for running the Simulink Simulation:
clear
clc
%Set Simulator Settings
x0=1;
T=5;
a=-1/T;
t_stop=25; %[s]
T_s=t_stop/1000; %[s]
options=simset('solver', 'ode5', 'fixedstep', T_s);
%Starting Simulation
sim('simulink_ex2', t_stop, options);
We get the same results:
This is the Name for our Simulink Model
Mass-Spring-Damper System
In this example we will create a mass-spring-damper model in Simulink and
configure and run the simulation from a MATLAB m-file.
The differential equation for the system is as follows:
!
𝑥̈ = " (𝐹 − 𝑐 𝑥̇ − 𝑘𝑥)
Where:
𝑥 - position
𝑥̇ - speed
𝑥̈ - acceleration
Instead of hard-coding the model parameters in the blocks you should refer
to them as variables set in an m-file.
The following variables should then be set in the m-file:
x_init = 4; %[m]. Initial position.
dxdt_init = 0; %[m/s]. Initial Speed.
m = 20; %[kg]
c = 4; %[N/(m/s)]
k = 2; %[N/m]
t_step_F = 50; %[s]
F_O = 0; %[N]
F_1 = 4; %[N]
Force F:
Position 𝑥 and speed 𝑥:̇
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no
E-mail: hans.p.halvorsen@usn.no
Web: https://www.halvorsen.blog