Tutorial 01
Tutorial 01
Introduction to
Simulink®
Simulink is a software package for modeling, simulating, and analyzing
dynamic systems. It supports linear and nonlinear systems, modeled in
continuous time, sampled time, or a hybrid of the two.
This chapter shows the basics of simulation with Simulink. It contains
five examples, and they aim to make you familiar with the Simulink
environment and how to do a simulation there. You can start from the next
chapter if you are familiar with the Simulink environment.
us to test our ideas before we go wasting all that time building it with
a breadboard or printed circuit board (PCB), just to find out it doesn’t
really work.
MATLAB®/Simulink® is one of the most powerful software for
simulation of power electronic circuits. MATLAB/Simulink simulation
results are accurate and quite close to laboratory measurements. When
you want to simulate a power electronic converter with MATLAB/
Simulink, you can use all of the ready-to-use blocks available in the
Simulink environment. This is a very interesting property. For instance,
assume that you want to simulate a DC-DC converter, which is controlled
with a Fuzzy Logic controller. In this case you can simply use the ready-
to-use Fuzzy Logic Toolbox® blocks. If you try to do the same simulation
in other software, you need to consume a considerable amount of time to
model the Fuzzy Logic controller from the ground up.
2
CHAPTER 1 INTRODUCTION TO SIMULINK®
The Simulink Start Page window appears. Click the Blank Model.
Now the Simulink environment with a blank project is ready
(Figure 1-3).
3
CHAPTER 1 INTRODUCTION TO SIMULINK®
4
CHAPTER 1 INTRODUCTION TO SIMULINK®
After clicking the Library Browser icon, the Simulink Library Browser
window (see Figure 1-5) will be opened, and you can add required
components to the model.
5
CHAPTER 1 INTRODUCTION TO SIMULINK®
Locating Blocks
Simulink Library Browser contains many blocks, and it is impossible to
memorize each block location. The Enter search term box is useful to find
a block when you forgot its location. For instance, assume that you need a
PID controller block, but you don’t know where it is.
In this case just type “pid” in the Enter search term box (see Figure 1-6)
and press the Enter key of your keyboard. After pressing the Enter key,
Simulink will list blocks related to the entered term on the right side of
the window.
6
CHAPTER 1 INTRODUCTION TO SIMULINK®
7
CHAPTER 1 INTRODUCTION TO SIMULINK®
Figure 1-8. Addition of the Transfer Fcn block to the Simulink model
Add a Step block (see Figure 1-9) to the model (see Figure 1-10).
8
CHAPTER 1 INTRODUCTION TO SIMULINK®
9
CHAPTER 1 INTRODUCTION TO SIMULINK®
Add a Scope block (see Figure 1-11) to the model (see Figure 1-12).
10
CHAPTER 1 INTRODUCTION TO SIMULINK®
When you bring the mouse pointer close to the block terminals, it will be
changed to crosshair and permits you to start connecting them. After seeing
the crosshair, hold down the left button on the mouse and drag the connection
toward the destination terminal and release the button on the destination
terminal. Use this method to connect the blocks together (Figure 1-13).
11
CHAPTER 1 INTRODUCTION TO SIMULINK®
12
CHAPTER 1 INTRODUCTION TO SIMULINK®
Assume that you want to simulate the behavior of the system for time
length of 2 s. Enter 2 in the Stop Time box and click the Run button (or
press Ctrl+T) to simulate the behavior of the system (see Figure 1-16).
Sometimes you need to do the simulation with a specific solver. In these
cases, use Model Settings (see Figure 1-17) to select the desired solver.
After clicking the Model Settings icon (or pressing Ctrl+E), the window
shown in Figure 1-18 appears, and you can select the desired type of solver.
13
CHAPTER 1 INTRODUCTION TO SIMULINK®
14
CHAPTER 1 INTRODUCTION TO SIMULINK®
The simulation result is shown in Figure 1-19. It can be copied into the
clipboard by pressing Ctrl+C. You can paste the copied waveform in other
software by pressing Ctrl+V. This is very useful when you want to prepare a
presentation or report.
You can use Cursor Measurement (see Figure 1-20) to read the
coordinates of different points on the graph. After clicking Cursor
Measurement, two vertical lines will be added to the graph (see
Figure 1-21). You can move them to read the coordinates of different points
on the graph.
15
CHAPTER 1 INTRODUCTION TO SIMULINK®
16
CHAPTER 1 INTRODUCTION TO SIMULINK®
17
CHAPTER 1 INTRODUCTION TO SIMULINK®
100
Figure 1-22. Entering the H s into MATLAB
s 2 8s 100
The pidTuner command (see Figure 1-23) helps you tune the PID
controller. After running the pidTuner command, the window shown in
Figure 1-24 appears.
18
CHAPTER 1 INTRODUCTION TO SIMULINK®
Move the sliders until you obtain a good response. By default, the PID
Tuner does the tuning in the time domain (see Figure 1-25). You can do it
in the frequency domain, as well (see Figure 1-26).
19
CHAPTER 1 INTRODUCTION TO SIMULINK®
20
CHAPTER 1 INTRODUCTION TO SIMULINK®
21
CHAPTER 1 INTRODUCTION TO SIMULINK®
In this example we designed a PID controller for the given plant. In the
next example, we will simulate the behavior of a closed-loop system in the
Simulink environment.
100
s 8 s 100
2
22
CHAPTER 1 INTRODUCTION TO SIMULINK®
Sum (see Figure 1-32) and PID Controller (see Figure 1-33) blocks. Settings
of the Sum and PID Controller blocks are shown in Figures 1-34 and 1-35,
respectively.
23
CHAPTER 1 INTRODUCTION TO SIMULINK®
24
CHAPTER 1 INTRODUCTION TO SIMULINK®
25
CHAPTER 1 INTRODUCTION TO SIMULINK®
26
CHAPTER 1 INTRODUCTION TO SIMULINK®
27
CHAPTER 1 INTRODUCTION TO SIMULINK®
has the default parameter values. Double-click the PID Controller block
and click the Tune button (see Figure 1-38). After clicking the Tune button,
the window shown in Figure 1-39 appears and permits you to tune the
controller.
28
CHAPTER 1 INTRODUCTION TO SIMULINK®
29
CHAPTER 1 INTRODUCTION TO SIMULINK®
30
CHAPTER 1 INTRODUCTION TO SIMULINK®
31
CHAPTER 1 INTRODUCTION TO SIMULINK®
Up to this point, the Scope blocks showed a graph of one signal only.
Sometimes you need to view two or more signals simultaneously. We will
learn how to do this in the next example.
32
CHAPTER 1 INTRODUCTION TO SIMULINK®
Click the connection between the scope and output of the system (see
Figure 1-44) and press the Delete key to remove it (see Figure 1-45).
34
CHAPTER 1 INTRODUCTION TO SIMULINK®
Connect the inputs of the Scope block to the desired nodes of the
system (Figure 1-48).
35
CHAPTER 1 INTRODUCTION TO SIMULINK®
Run the simulation. The result shown in Figure 1-49 is obtained. One of
the signals has round markers on it. You can remove these round markers
by clicking the Style icon (see Figure 1-50). After clicking the Style icon, the
window shown in Figure 1-51 appears. Convert the Marker box to none (see
Figure 1-52). Now the waveform has no round markers on it (see Figure 1-53).
36
CHAPTER 1 INTRODUCTION TO SIMULINK®
37
CHAPTER 1 INTRODUCTION TO SIMULINK®
38
CHAPTER 1 INTRODUCTION TO SIMULINK®
39
CHAPTER 1 INTRODUCTION TO SIMULINK®
40
CHAPTER 1 INTRODUCTION TO SIMULINK®
The block diagram shown in Figure 1-56 shows the output of the
system and control input simultaneously (see Figure 1-57).
41
CHAPTER 1 INTRODUCTION TO SIMULINK®
Summary
In this chapter you learned the basics of simulation with Simulink. You learned
how to observe the step response of a system, how to tune a PID controller
with MATLAB and Simulink, how to simulate a closed-loop feedback control
system, and how to observe two or more signals simultaneously.
Power electronic converters are dynamic systems. In the next
chapter, we will learn how to simulate a dynamic system in the Simulink
environment.
42
CHAPTER 2
Simulation of
Dynamic Systems
in Simulink®
Basics of Simulink were studied in the previous chapter. In this chapter
we will focus on the modeling of dynamic equations in the Simulink
environment. Dynamic equations can be divided into two categories:
continuous time dynamic equations (differential equations) and discrete
time dynamic equations (difference equations). Modeling of both of these
equations is studied in this chapter.
Differential equations can be divided into two groups: time domain
equations and state space equations. In order to simulate a differential
equation in the Simulink environment, you need to convert it into
state space.
In this chapter we will learn how to convert a time domain differential
equation into a state space equation, how to decrease the number of
Integrator blocks used in the Simulink model, how to simulate a state
space equation with the aid of State-Space and MATLAB Function blocks,
how to transfer the simulation result from Simulink to the MATLAB
environment, how to simulate a DC-DC boost converter, and how to
simulate difference equations.
Example 1: Simulation of
Differential Equations
In this example we want to simulate the following system:
y 5 y 10 y 7 sin 3t , y 0 1, y 0 4
(2.1)
3
Let’s define two new variables and convert the given equation into the
state space system:
dy
x1 y, x2 y (2.2)
dt
x1 x2 , x2 10 x1 5 x2 7 sin 3t , x0 14 (2.3)
3
where x0 indicates the initial condition of the system. This state space
representation is suitable for drawing the Simulink model. Add two
Integrator blocks (see Figure 2-1) to the Simulink model (see Figure 2-2).
44
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
45
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
We need Gain (see Figure 2-5), Sum (see Figure 2-6), and Sine Wave
(see Figure 2-7) blocks to implement x2 10 x1 5 x2 7 sin 3t . The
3
implementation of this equation is shown in Figure 2-8. Note that Gain
blocks are rotated by clicking them and pressing Ctrl+R.
46
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
47
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
48
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
49
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
Figure 2-8. Implementation of x1 x2 and x2 10x1 5x2 7sin 3t
3
equations
50
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
51
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
52
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
53
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
54
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
55
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
56
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
57
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
58
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
x1 x2 , x2 10 x1 5 x2 7 sin 3t , x0 [14] (2.3)
3
x Ax u , x0 [14] (2.4)
where x = [x1;x2 ], A = [0 1;10 − 5 ], and u 0; 7 sin 3t . The model
3
shown in Figure 2-19 implements the state space equation with only one
Integrator block. Settings of the blocks are shown in Figures 2-20 and 2-21.
This model used a Constant block (see Figure 2-22) to implement the zero
of vector u 0 7 sin 3t .
3
59
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
60
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
61
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
Run the simulation. The result shown in Figure 2-23 is obtained. Both
of the signals are shown on one scope. The model in Figure 2-24 shows
how to separate the signals and send each signal to a separate scope. This
model used a demultiplexer (Demux) block (see Figure 2-25) to separate
the signals.
62
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
63
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
64
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
y
t sin t y t 2 y 7 sin 3t , y 0 1, y 0 4
3
(2.5)
Let’s convert the equation to state space. The new variables x1 and x2
are defined as
dy
x1 y, x2 y (2.6)
dt
x1 x2 , x2 t 2 x1
t sin t x2 7 sin 3t
3
(2.7)
x Ax u (2.8)
where x = [x1 x2 ], A 01; t 2
t sin t , and u 0; 7 sin 3t .
3
The obtained state space equation is implemented with the block diagram
shown in Figure 2-26. This model used MATLAB Function and Clock
blocks (Figures 2-27 and 2-28).
65
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
67
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
68
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
Run the simulation. The result shown in Figures 2-31 and 2-32 is
obtained.
69
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
The given system in this example can be simulated with the aid of
the following block diagram as well. This Simulink model used more
blocks in comparison to the model shown in Figure 2-26. If you run the
Simulink model shown in Figure 2-33, you will obtain the results shown in
Figures 2-31 and 2-32.
70
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
The model shown in Figure 2-33 used Sqrt (see Figure 2-34) and Math
Function (see Figure 2-35) blocks.
71
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
72
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
73
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
Settings of blocks used in Figure 2-33 are shown in Figures 2-36 to 2-40.
74
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
75
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
76
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
77
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
In the next example, we will learn how to copy and take out a block
from our Simulink model.
78
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
Assume that we want to take out the Sqrt block of Figure 2-43 and
replace it with something else. In order to do this, hold down the Shift key,
click the Sqrt block, and drag the block. This separates the block from the
model without affecting the connections (see Figure 2-44). Now you can
add the new block to the model. Just bring the new block terminals close
to the connection shown with the red dotted line. Simulink makes the
connection for you.
80
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
In the next example, we will learn how to use the State-Space block to
simulate linear state space models.
x Ax Bu
y Cx Du (2.9)
81
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
82
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
The Simulink model is shown in Figure 2-46. This diagram used the
Ramp block shown in Figure 2-47 to stimulate the system with a unit ramp
signal. Settings of the State-Space block are shown in Figure 2-48.
83
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
84
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
85
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
86
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
Enter the commands shown in Figure 2-53. t, y1, and y2 indicate time,
first output, and second output, respectively. The result of this code is
shown in Figures 2-54 and 2-55. This result is the same as the result shown
in Figure 2-49.
Figure 2-53. Reading the data imported from Simulink and drawing
its graph
87
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
88
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
89
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
90
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
d V d v
iL IN , vC C (2.10)
dt L dt R.C
d V v d i v
iL IN C , vC L C (2.11)
dt L L dt C R.C
d v 1 d i v
iL 1 u C VIN , vC 1 u L C (2.12)
dt L L dt C R.C
92
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
93
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
94
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
95
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
Run the simulation. The results shown in Figures 2-65 and 2-66 are
obtained. According to the obtained results, the steady-state voltage of
the capacitor is about 30 V, and the steady-state inductor current has
minimum of 0.5457 A and maximum of 2.453 A. You need to zoom the
waveform in order to see these values.
96
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
97
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
98
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
99
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
100
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
101
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
Figure 2-71. The discrete solver must be selected for discrete time
simulations
102
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
Run the simulation. The result is shown in Figure 2-72. The obtained
result is the same as hand calculations.
103
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
104
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
105
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
Run the simulation. The result shown in Figure 2-76 is obtained. The
obtained result is the same as the hand calculations.
106
CHAPTER 2 SIMULATION OF DYNAMIC SYSTEMS IN SIMULINK®
Summary
In this chapter you learned how to simulate a dynamic system in the
Simulink environment. To do so, the given time domain differential
equations must be converted into a state space model. The obtained state
space model can be implemented with the aid of Integrator blocks. You
learned how to decrease the number of integrators used in the Simulink
model, how to transfer the simulation results from Simulink into MATLAB,
and how to simulate discrete time models.
In the next chapter, we will learn how to simulate uncontrolled AC-DC
converters with the aid of Simscape™ blocks.
107