MATLAB Examples - Simulink
MATLAB Examples - Simulink
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
Library Browser
[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
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
%Starting Simulation
sim('simulink_ex2', t_stop, options);
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:
E-mail: [email protected]
Web: https://www.halvorsen.blog