Introduction to Simulink
Linguaggio Programmazione Matlab-Simulink (2018/2019)
Introduction to Simulink
• Simulink 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.
• A dynamic system may be given as a differential equation
Example: electric circuit
Example: electric circuit
Example: electric circuit
• Characteristic equation:
𝑠 2 + 4𝑠 + 3 = 0 (B.6)
• The transfer function:
3
H s = (B.7)
s2 +4s+3
• We can plug this directly in Simulink
Implementation in Simulink
Running Simulink
Implementation in Simulink
Implementation in Simulink
Simulink Library Browser
Implementation in Simulink
Implementation in Simulink
Drag&Drop
Implementation in Simulink
• From the ‘Continous’ module choose ‘Transfer Fcn’ (drag
& drop)
• From the ‘Commonly Used Blocks’ choose ‘Scope’
• Merge the blocks with arrows
• Change the ‘Transfer Fcn’ (through double click)
• Optional: change the name of the blocks (double click on
names)
Implementation in Simulink
Run simulation
Double click on
‘Scope’
Implementation in Simulink
• Change the ‘Transfer Fcn’ to 𝑠 2 + 0.1𝑠 + 10 = 0 and run
simulation
Accuracy of the solution depends on
- Solver (numerical integration algorithm)
- Step size
Implementation in Simulink
Implementation in Simulink
Implementation in Simulink
Improved accuracy!
Implementation: second method
• Simulate the same system using integrator blocks and
sum by rewriting (B.4):
𝑑 2 𝑣𝑐 𝑑𝑣𝑐
= −4 − 3𝑣𝑐 − 3 𝑢0 (𝑡) (B.7)
𝑑𝑡 2 𝑑𝑡
𝑑 2 𝑣𝑐
• The right side of (B.7) is the sum that yields !
𝑑𝑡 2
• Insert a ‘Sum’ block from the ‘Math Operations’ library (put
3 inputs)
Implementation: second method
• Simulate the same system using integrator blocks and
sum by rewriting (B.4):
𝑑 2 𝑣𝑐 𝑑𝑣𝑐
= −4 − 3𝑣𝑐 − 3 𝑢0 (𝑡) (B.7)
𝑑𝑡 2 𝑑𝑡
𝑑 2 𝑣𝑐
• The right side of (B.7) is the sum that yields !
𝑑𝑡 2
• Insert a ‘Sum’ block from the ‘Math Operations’ library (put
3 inputs)
Implementation: second method
𝑑 2 𝑣𝑐
• Rember: the sum outputs 𝑑𝑡 2
Implementation: second method
𝑑 2 𝑣𝑐
• Integrating 𝑑𝑡 2
we obtain
𝑑𝑣𝑐
𝑑𝑡
𝑑2 𝑣𝑐
𝑑𝑡 2
Implementation: second method
• Add a multiply block ‘Gain’ and set it to -4
𝑑2 𝑣𝑐
𝑑𝑡 2
Implementation: second method
• Invert the ‘Gain’ (right click on ‘Gain’, then ‘Flip Block’ or
CTRL-I)
𝑑2 𝑣𝑐
𝑑𝑡 2
Implementation: second method
• Connect with the output of the integrator
𝑑𝑣𝑐
𝑑2 𝑣𝑐 𝑑𝑡
𝑑𝑡 2
Implementation: second method
• Connect to the sum
𝑑𝑣𝑐
𝑑2 𝑣𝑐 𝑑𝑡
𝑑𝑡 2
Implementation: second method
• Integrate 𝑑𝑣𝑐
𝑑𝑡
to compute 𝑣𝑐
𝑑𝑣𝑐
𝑣𝑐
𝑑2 𝑣𝑐 𝑑𝑡
𝑑𝑡 2
Implementation: second method
• Multiply 𝑣𝑐 by -3 and insert into the sum
𝑑𝑣𝑐
𝑑2 𝑣𝑐 𝑑𝑡 𝑣𝑐
𝑑𝑡 2
Implementation: second method
• Insert the input (step function)
𝑑𝑣𝑐
𝑑2 𝑣𝑐 𝑑𝑡 𝑣𝑐
𝑑𝑡 2
Implementation: second method
• Multiply the input by 3
𝑑𝑣𝑐
𝑑2 𝑣𝑐 𝑑𝑡 𝑣𝑐
𝑑𝑡 2
Implementation: second method
• Insert the ‘Scope’ block
𝑑𝑣𝑐
𝑑2 𝑣𝑐 𝑑𝑡 𝑣𝑐
𝑑𝑡 2
Implementation: second method
• Now all the signals are inserted in the block diagram:
𝑢0 3𝑢0 𝑑𝑣𝑐
𝑑2 𝑣𝑐 𝑑𝑡 𝑣𝑐
𝑑𝑡 2
Implementation: second method
• The initial conditions are inserted by double clicking the
Integrator blocks and entering the values 0 for the first
integrator, and 0.5 for the second integrator.
𝑑𝑣𝑐
𝑢0 𝑣𝑐
𝑑𝑡
𝑑 2 𝑣𝑐
𝑑𝑡 2
Implementation: second method
• Run the simulation: the result
looks similar with the previous
Implementation!
𝑑𝑣𝑐
𝑢0 𝑣𝑐
𝑑𝑡
𝑑 2 𝑣𝑐
𝑑𝑡 2
Implementation: putting all together
• Copy and paste the first diagram in the same window:
This block do no accept
initial conditions!
Implementation: putting all together
• Add a ‘Mux’ with two inputs and a ‘Scope’.
Implementation: putting all together
• Change the initial conditions and see what happens. How
do you explain it?
Implementation: putting all together
• Change the equations, the solver and the step size:
analyze the results.
Implementation: putting all together
• Change the simulation time…
Exercise 1
• Following the same steps, simulate the following dynamic
system:
𝑑𝑥
+ 2𝑥 𝑡 = 𝑢(𝑡)
𝑑𝑡
where 𝑢(𝑡) is a square wave with an amplitude of 1 and a
frequency of 1 rad/sec and 𝑥 𝑡 is the output of the system.
To generate a Square Wave use a ‘Signal Generator’ block and
select the Square Wave form but change the default units to
radiants/sec.
• Method 1: use a single block for the transfer function
• Method 2: use an ‘Integrator’ block, a ‘Gain’ block and a ‘Sum’
block.
• What do you note in the absence of initial conditions?
• Hint: type ‘Model a Continuous System’ in the Help search of
Matlab.
Exercise 2
• For the following mass-spring-damper system, the inputs
are:
• M = 1Kg (mass)
• u = the gravity force Mg (g = 9.8)
• K = 5.0 N/m (spring constant)
• D = 0.05 Ns/m (damping coefficient)
• x(0) = 10 (initial position)
• x’(0) = 0 (initial velocity)
• Find the equation of motion x(t) and perform the simulation with Simulink.
• Optional: use a Matlab file parameters.m to set (all) the input parameters.
• Hints:
1. for the input u(t) use the step function and multiply it by 9.8.
2. The equation is:
𝑑2𝑥 𝐷 𝑑𝑥 𝐾
= − − 𝑥+𝑔
𝑑𝑡 2 𝑀 𝑑𝑡 𝑀