Introduction To Simulink
Introduction To Simulink
Introduction to Simulink
T
his chapter is an introduction to Simulink. This author feels that it is best to introduce Sim-
ulink in this chapter with a few examples. Tools for simulation and model−based designs are
presented in the subsequent chapters. Some familiarity with MATLAB is essential in
understanding Simulink, and for this purpose, Appendix A is included as an introduction to
MATLAB.
Example 1.1
For the electric circuit of Figure 1.1, u 0 ( t ) *is the input and the initial conditions are i L ( 0 − ) = 0 ,
and v c ( 0 − ) = 0.5 V . We will compute v C ( t ) .
R L
+
1Ω 1⁄4 H C
+ vC ( t )
−
i(t) 4⁄3 F
−
vs ( t ) = u0 ( t )
Figure 1.1. Circuit for Example 1.1
For this example,
dv
i = i L = i C = C --------C- (1.1)
dt
* Throughout this text, the designation u 0 ( t ) will be used to denote the unit step function. We will use u ( t ) to
denote any other input to be consistent with the MATLAB and Simulink designations.
2
d vC dv
----------- + 4 --------C- + 3v C = 3u 0 ( t ) (1.4)
dt
2 dt
2
d v dv
----------C- + 4 --------C- + 3v C = 3 t>0 (1.5)
dt
2 dt
To appreciate Simulink’s capabilities, for comparison, three different methods of obtaining the
solution are presented, and the solution using Simulink follows.
First Method − Assumed Solution
Equation (1.5) is a second−order, non−homogeneous differential equation with constant coeffi-
cients, and thus the complete solution will consist of the sum of the forced response and the natu-
ral response. It is obvious that the solution of this equation cannot be a constant since the deriva-
tives of a constant are zero and thus the equation is not satisfied. Also, the solution cannot
contain sinusoidal functions (sine and cosine) since the derivatives of these are also sinusoids.
– at
However, decaying exponentials of the form ke where k and a are constants, are possible can-
didates since their derivatives have the same form but alternate in sign.
–s t –s t
It can be shown* that if k 1 e 1 and k 2 e 2 where k 1 and k 2 are constants and s 1 and s 2 are the
roots of the characteristic equation of the homogeneous part of the given differential equation,
–s1 t –s2 t
the natural response is the sum of the terms k 1 e and k 2 e . Therefore, the total solution will
be
* For a thorough discussion, please refer to Circuit Analysis II with MATLAB Applications, ISBN 0−
9709511−5−9, Appendix B.
−4
−3
1. On the Simulink Library Browser, we click the leftmost icon shown as a blank page on
the top title bar. A new model window named untitled will appear as shown in Figure 1.6.
Figure 1.8. Dragging the unit step function into File Equation_1_26
Figure 1.9. File Equation_1_26 with added Step and Gain blocks
5. Next, we need to add a thee−input adder. The adder block appears on the right side of the
Simulink Library Browser under Math Operations. We select it, and we drag it into the
Equation_1_26 model window. We double click it, and on the Function Block Parameters
window which appears, we specify 3 inputs. We then connect the output of the Step block to
the input of the Gain block, and the output of the of the Gain block to the first input of the
Add block as shown in Figure 1.10.
Figure 1.10. File Equation_1_26 with added Add block and connections between the blocks
6. From the Commonly Used Blocks of the Simulink Library Browser, we choose the Integra-
tor block, we drag it into the Equation_1_26 model window, and we connect it to the output
of the Add block. We repeat this step and to add a second Integrator block. We click the text
“Integrator” under the first integrator block, and we change it to Integrator 1. Then, we
change the text “Integrator 1” under the second Integrator to “Integrator 2” as shown in Fig-
ure 1.11.
double−click the Unit block and in the Source Block Parameters window we change the
Step time value from 1 to 0. We leave all other parameters in their default state. We also need
to specify the simulation time. This is done by specifying the simulation time to be 10 seconds
on the Configuration Parameters from the Simulation drop menu. We can start the simula-
tion on Start from the Simulation drop menu or by clicking the icon.
9. To see the output waveform, we double click the Scope block, and then clicking on the
Autoscale icon. Then we right−click near the vertical axis, we click on Axes properties,
we specify Y−min =0, Y−max = 1.5, we click OK, and we obtain the waveform shown in Fig-
ure 1.13. Henceforth, we will use this procedure to scale the vertical axis in our subsequent
Scope block displays.
Figure 1.14. Obtaining the function v C ( t ) for Example 1.1 with the State−Space block.
We double−click the State−Space block, and in the Functions Block Parameters window we
enter the constants shown in Figure 1.15.
Figure 1.15. The Function block parameters for the State−Space block.
* who displays only the variables names, not the function to which each variable belongs. whos lists
more information about each variable.
The initials conditions [ x1 x2 ]' are specified at the MATLAB command prompt as
x1=0; x2=0.5;
As before, to start the simulation we click the icon, and to see the output waveform, we dou-
ble click the Scope block. Then we click on the Autoscale icon, and we scale the vertical
axis as we did with the waveform of Figure 1.13. The waveform shown in Figure 1.16.
Figure 1.16. The waveform for the function v C ( t ) for Example 1.1 with the State−Space block.
The state−space block is the best choice when we need to display the output waveform of three or
more variables as illustrated by the following example.
Example 1.2
A fourth−order network is described by the differential equation
4 3 2
d y d y d y dy
--------- + a 3 --------3- + a 2 -------2- + a 1 ------ + a 0 y ( t ) = u ( t ) (1.27)
dt
4
dt dt dt
where y ( t ) is the output representing the voltage or current of the network, and u ( t ) is any input,
and the initial conditions are y ( 0 ) = y' ( 0 ) = y'' ( 0 ) = y''' ( 0 ) = 0 .
a. We will express (1.27) as a set of state equations
subject to the initial conditions y ( 0 ) = y' ( 0 ) = y'' ( 0 ) = y''' ( 0 ) = 0 , has the solution
2
y ( t ) = 0.125 [ ( 3 – t ) – 3t cos t ] (1.29)
In our set of state equations, we will select appropriate values for the coefficients
a 3, a 2, a 1, and a 0 so that the new set of the state equations will represent the differential equa-
tion of (1.28) and using Simulink, we will display the waveform of the output y ( t ) .
1. The differential equation of (1.28) is of fourth−order; therefore, we must define four state vari-
ables that will be used with the four first−order state equations.
We denote the state variables as x 1, x 2, x 3 , and x 4 , and we relate them to the terms of the
given differential equation as
2 3
dy d y d y
x1 = y ( t ) x 2 = ------ x 3 = --------- x 4 = --------- (1.30)
dt 2 3
dt dt
We observe that
x· 1 = x 2
x· 2 = x 3
x· 3 = x 4 (1.31)
4
d y
--------- = x· 4 = – a 0 x 1 – a 1 x 2 – a 2 x 3 – a 3 x 4 + u ( t )
4
dt
and in matrix form
x· 1 0 1 0 0 x1 0
x· 2 0 0 1 0 x2
= + 0 u(t) (1.32)
x· 3 0 0 0 1 x3 0
x· 4 –a0 –a1 –a2 –a3 x4 1
x· 1 0 1 0 0 x1 0
x· 2 0 0 1 0 x2
x· = , A= , x= , b= 0, and u = u ( t ) (1.35)
x· 3 0 0 0 1 x3 0
x· 4 –a0 –a1 –a2 –a3 x4 1
x1
x2
y = [1 0 0 0] ⋅ + [ 0 ]u ( t ) (1.36)
x3
x4
2. By inspection the differential equation of (1.27) will be reduced to the differential equation of
(1.28) if we let
a3 = 0 a2 = 2 a1 = 0 a0 = 1 u ( t ) = sin t
and thus the differential equation of (1.28) can be expressed in state−space form as
x· 1 0 1 0 0 x1 0
x· 2 0 0 1 0 x2
= + 0 sin t (1.37)
x· 3 0 0 0 1 x3 0
x· 4 –a0 0 –2 0 x4 1
where
x· 1 0 1 0 0 x1 0
x· 2 0 0 1 0 x2
x· = , A= , x= , b= 0, and u = sin t (1.38)
x· 3 0 0 0 1 x3 0
x· 4 –a0 0 –2 0 x4 1
x1
x2
y = [1 0 0 0] ⋅ + [ 0 ] sin t (1.39)
x3
x4
We invoke MATLAB, we start Simulink by clicking on the Simulink icon, on the Simulink
Library Browser, we click the Create a new model (blank page icon on the left of the top
bar), and we save this model as Example_1_2. On the Simulink Library Browser we select
Sources, we drag the Signal Generator block on the Example_1_2 model window, we click
and drag the State−Space block from the Continuous on Simulink Library Browser, and we
click and drag the Scope block from the Commonly Used Blocks on the Simulink Library
Browser. We also add the Display block found under Sinks on the Simulink Library
Browser. We connect these four blocks and the complete block diagram is as shown in Figure
1.17.
Figure 1.17. Model for Example 1.2 with the entries specified below
We now double−click the Signal Generator block and we enter the following in the Function
Block Parameters dialog box the following:
Wave form: sine
Time (t): Use simulation time
Amplitude: 1
Frequency: 2
Units: Hertz
Next, we double−click the State−Space block and we enter the following parameter values in
the Function Block Parameters:
A: [0 1 0 0; 0 0 1 0; 0 0 0 1; −a0 −a1 −a2 −a3]
B: [0 0 0 1]’
C: [1 0 0 0]
D: [0]
Initial conditions: x0
Absolute tolerance: auto
Now, we switch to the MATLAB Command window and at the command prompt we type the
following values:
a0=1; a1=0; a2=2; a3=0; x0=[0 0 0 0]’;
We change the Simulation Stop time to 25 , and we start the simulation by clicking on the
icon. To see the output waveform, we double− click the Scope block, then we click the
Examples 1.1 and 1.2 have clearly illustrated that the State−Space is indeed a powerful block. We
could have also obtained the solution of Example 1.2 using four Integrator blocks.
Example 1.3
We will create a model that will produce the simultaneous solution of three equations with three
unknowns using Algebraic Constraint blocks found in the Math Operations library, Display
blocks found in the Sinks library, and Gain blocks found in the Commonly Used Blocks library.
The model will display the values for the unknowns z 1 , z 2 , and z 3 for the system of the equations
a1 z1 + a2 z2 + a3 z3 + k1 = 0
a4 z1 + a5 z2 + a6 z3 + k2 = 0 (1.40)
a7 z1 + a8 z2 + a9 z3 + k3 = 0
Figure 1.19. Model for Example 1.3 with the entries specified below
Next, at the MATLAB command prompt we enter the following values:
a1=2; a2=−3; a3=−1; a4=1; a5=5; a6=4; a7=−6; a8=1; a9=2;...
k1=−8; k2=−7; k3=5;
After clicking on the simulation icon, we obtain the values of the unknowns as z 1 = 2 , z 2 = – 3 ,
and z 3 = 5 as shown in the Display blocks in Figure 1.19.
An Algebraic Constraint block constrains the input signal f ( z ) to zero, outputs a value for z ,
and this value eventually produces a zero at the input. Thus, output is fed back to the input via a
feedback path. We can improve the efficiency of the algebraic loop solver by providing an initial
guess for the algebraic state z that is close to the final solution value. By default, the initial guess
value is zero.
For instance, to group all blocks and lines in the model of Figure 1.19 except the display blocks,
from the Edit drop menu we choose Create Subsystem and this model will be shown as in Figure
1.20* where at the MATLAB command prompt we have entered the following values:
a1=5; a2=−1; a3=4; a4=11; a5=6; a6=9; a7=−8; a8=4; a9=15;...
k1=14; k2=−6; k3=9;
The Display blocks in Figure 1.20 show the values of z 1 , z 2 , and z 3 for the values that we speci-
fied at the MATLAB command prompt above.
The Subsystem block is described in detail in Chapter 2, Section 2.1, Page 2−2.
1.2 Simulink Demos
At this time, the reader with no prior knowledge of Simulink, should be ready to learn Simulink’s
additional capabilities. We will explore other features in the subsequent chapters. However, it is
highly recommended that the reader becomes familiar with the block libraries found in the Sim-
ulink Library Browser. Then, the reader can follow the steps delineated in The MathWorks Sim-
ulink User’s Manual to run the Demo Models beginning with the thermo model. This model can
invoked by typing thermo at the MATLAB command prompt.
In the subsequent chapters, we will study each of the blocks under each of libraries in the Tree
Pane. They are listed in Table 1.1 below in alphabetical order, the library where they appear, the
chapter where they are described in this text, section/subsection, and page number in which they
are described.
* The contents of the Subsystem block are not lost. We can double−click on the Subsystem block to see its con-
tents. The Subsystem block replaces the inputs and outputs of the model with Inport and Outport blocks. These
blocks along with the Subsystem block are described in Section 2.1, Chapter 2, Page 2−2.
ing on the icon. To see the output waveform, we double click on the Scope block, and