Introduction To Simulink
Introduction To Simulink
Introduction To Simulink
1.1
1.2
1.3
1.4
1.5
1.6
Bouncing Ball..............................................................................................................21
Linear Programming ...................................................................................................23
Moving Ladder ..........................................................................................................210
Conical Water Reservoir........................................................................................... 212
Comparison of Alternate Proposals.......................................................................... 215
Valuation of a Depleting Asset .................................................................................219
ii
Appendixes
A Introduction to MATLAB .......................................................................................... A1
B
C
iii
ContinuousTime ........................................................................................................... D1
D.1 Derivative ................................................................................................................. D1
D.2 Integrator.................................................................................................................. D6
D.3 Integrator Limited.................................................................................................... D6
D.4 Integrator Second Order.......................................................................................... D8
D.5 Integrator Second Order Limited .......................................................................... D11
D.6 PID Controller ....................................................................................................... D12
D.7 PID Controller (2DOF) ......................................................................................... D14
D.8 StateSpace ............................................................................................................ D19
D.9 Transfer Fcn ........................................................................................................... D22
D.10 ZeroPole .............................................................................................................. D25
D.11 Transport Delay, Variable Time Delay, Variable Transport Delay ...................... D29
Discontinuities .................................................................................................................E1
E.1 Saturation................................................................................................................... E1
E.2 Saturation Dynamic ................................................................................................... E4
E.3 Dead Zone.................................................................................................................. E5
E.4 Dead Zone Dynamic .................................................................................................. E6
E.5 Rate Limiter ............................................................................................................... E8
E.6 Rate Limiter Dynamic.............................................................................................. E10
E.7 Backlash.................................................................................................................... E11
E.8 Relay ......................................................................................................................... E13
E.9 Quantizer.................................................................................................................. E15
E.10 Hit Crossing ............................................................................................................ E16
E.11 Coulomb and Viscous Friction .............................................................................. E18
E.12 Wrap to Zero........................................................................................................... E19
E.13 Nonlinear Systems Describing Functions ........................................................... E20
Discrete ............................................................................................................................. F1
F.1 Unit Delay ................................................................................................................. F1
F.2 Integer Delay ............................................................................................................. F1
F.3 Tapped Delay ............................................................................................................ F3
F.4 Discrete Time Integrator........................................................................................... F4
F.5 Discrete Transfer Fcn Block ..................................................................................... F5
F.6 Discrete Filter............................................................................................................ F8
F.7 Discrete ZeroPole .................................................................................................. F10
F.8 Difference................................................................................................................ F11
F.9 Discrete Derivative.................................................................................................. F12
F.10 Discrete StateSpace.............................................................................................. F13
F.11 Transfer Fcn First Order........................................................................................ F16
iv
Lookup Table.......................................................................................................... H1
Lookup Table (2D) ............................................................................................... H4
Lookup Table (nD).............................................................................................. H 5
Prelookup................................................................................................................ H8
Interpolation (nD) Using PreLookup .................................................................. H9
Direct Lookup Table (nD).................................................................................. H10
Lookup Table Dynamic ........................................................................................ H15
Sine and Cosine.................................................................................................... H16
Function Lookup Table Generation .................................................................... H18
vi
vii
viii
Sinks..................................................................................................................................O1
O.1
O.2
O.3
O.4
O.5
O.6
O.7
O.8
O.9
Outport ................................................................................................................... O1
Terminator.............................................................................................................. O1
To File..................................................................................................................... O1
To Workspace......................................................................................................... O3
Scope....................................................................................................................... O4
Floating Scope ........................................................................................................ O5
XY Graph................................................................................................................ O9
Display .................................................................................................................. O11
Stop Simulation.................................................................................................... O12
Sources ...............................................................................................................................P1
P.1 Inport ........................................................................................................................P1
P.2 Ground......................................................................................................................P1
P.3 From File ...................................................................................................................P1
P.4 From Workspace .......................................................................................................P1
P.5 Constant....................................................................................................................P2
P.6 Enumerated Constant...............................................................................................P2
P.7 Signal Generator .......................................................................................................P7
P.8 Pulse Generator.........................................................................................................P9
P.9 Signal Builder ..........................................................................................................P10
P.10 Ramp ......................................................................................................................P12
P.11 Sine Wave...............................................................................................................P12
P.12 Step.........................................................................................................................P14
P.13 Repeating Sequence ...............................................................................................P14
P.14 Chirp Signal ...........................................................................................................P15
P.15 Random Number ...................................................................................................P16
P.16 Uniform Random Number....................................................................................P17
P.17 Band Limited White Noise....................................................................................P18
P.18 Repeating Sequence Stair.......................................................................................P21
P.19 Repeating Sequence Interpolated ..........................................................................P22
P.20 Counter FreeRunning .........................................................................................P23
P.21 Counter Limited ....................................................................................................P24
P.22 Clock ......................................................................................................................P25
P.23 Digital Clock ..........................................................................................................P26
ix
UserDefined Functions................................................................................................. Q1
Q.1
Q.2
Q.3
Q.4
Q.5
Q.6
Q.7
Simulink Extras................................................................................................................ T1
T.1
T.2
T.3
T.4
T.5
Fcn........................................................................................................................... Q1
Interpreted MATLAB Function ............................................................................. Q2
MATLAB Function ................................................................................................ Q2
SFunction.............................................................................................................. Q6
Level2 MATLAB SFunction............................................................................... Q6
SFunction Builder ................................................................................................ Q9
SFunction Examples ........................................................................................... Q10
V Exercises............................................................................................................................ V1
W Solutions to the Exercises ............................................................................................... W1
X Weighted Moving Average .............................................................................................. X1
Y
xi
References....................................................................................................................... Ref1
Index................................................................................................................................ IN1
xii
Chapter 1
Introduction to Simulink
his chapter is an introduction to Simulink. This author feels that it is best to introduce Simulink in this chapter with a few examples. Tools for simulation and modelbased 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.
14 H
i(t)
+
C
43 F
vC ( t )
vs ( t ) = u0 ( t )
(1.1)
11
Figure 1.16. The waveform for the function v C ( t ) for Example 1.1 with the StateSpace block.
d
y
d y
d y
dy
--------+ a 3 --------3- + a 2 -------2- + a 1 ------ + a 0 y ( t ) = u ( t )
4
dt
dt
dt
dt
(1.27)
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
b. It is known that the solution of the differential equation
4
d y
d y
-------- + 2 -------- + y ( t ) = sin t
4
2
dt
dt
114
(1.28)
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 equation 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 fourthorder; therefore, we must define four state variables that will be used with the four firstorder 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
We observe that
dy
x 2 = -----dt
x1 = y ( t )
d y
x 3 = --------2dt
d y
x 4 = --------3dt
x 1 = x 2
x 2 = x 3
x 3 = x 4
(1.30)
(1.31)
d y
--------- = x 4 = a 0 x 1 a 1 x 2 a 2 x 3 a 3 x 4 + u ( t )
4
dt
0
0
=
0
a0
x 3
x 4
1
0
0
a1
0
1
0
a2
x1
0
0
1
a3
0
x2
+ 0 u(t)
0
x3
1
x4
(1.32)
x = Ax + bu
(1.33)
y = Cx + du
(1.34)
where
x =
x 1
x 2
x 3
x 4
0
0
A=
0
a0
1
0
0
a1
0
1
0
a2
0
0
,
1
a3
x1
x=
x2
x3
x4
0
b= 0,
0
1
and u = u ( t )
(1.35)
115
y = [1 0 0 0]
x3
+ [ 0 ]u ( t )
(1.36)
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 statespace form as
x 1
x 2
0
0
=
0
a0
x 3
x 4
1
0
0
0
0
1
0
2
0
0
1
0
x1
0
+ 0 sin t
0
x3
1
x4
x2
(1.37)
where
x =
x 1
x 2
x 3
x 4
0
0
A=
0
a0
1
0
0
0
0
1
0
2
0
0
,
1
0
x1
x=
x2
x3
x4
0
b= 0,
0
1
and u = sin t
(1.38)
x2
x3
+ [ 0 ] sin t
(1.39)
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
StateSpace 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
116
Figure 1.17. Model for Example 1.2 with the parameters specified below
We now doubleclick 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 doubleclick the StateSpace 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 doubleclick the Scope block, then we click the Autoscale
icon, and we obtain the waveform shown in Figure 1.18.
117
The Display block in Figure 1.17 shows the value at the end of the simulation time.
Examples 1.1 and 1.2 have clearly illustrated that the StateSpace is indeed a powerful block. We
could have also obtained the solution of Example 1.2 using four Integrator blocks.
(1.40)
a7 z1 + a8 z2 + a9 z3 + k3 = 0
118
Figure 1.19. Model for Example 1.3 with the entries specified below
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.
119
The Display blocks in Figure 1.20 show the values of z 1 , z 2 , and z 3 for the values that we specified
at the MATLAB command prompt above.
* The contents of the Subsystem block are not lost. We can doubleclick on the Subsystem block to see its contents. 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 22.
120
Clock
20
Constant 1
Sum 1
Scope
Mux
24
Constant 2
Sum 2
1/3
Constant 3
Product
aa
Figure 2.5. Plot of the constraints as displayed in Scope block in Figure 2.4
26
and the Display block in Figure 2.19 displays the value of the cost if Route A is chosen.
218
37
Figure 4.2. Model for the rise of water in a capillary tube at temperature T in
42
10
15
20
30
40
50
60
70
80
90 100];
Figure 5.6. The MATLAB Function block with the userdefined function
To the MATLAB Function block model we add the Clock block from the Sources library, and the
Scope block from the Sinks library. We connect the three blocks and the model is now as shown in
Figure 5.7, and we execute the simulation command.
The waveform indicates the maximum deflection occurs when x 0.58L . The exact result is
obtained by taking the first derivative of the given function and setting it equal to zero as shown
below.
3
dy dx = 8x 15 Lx + 6L x = x ( 8x 15Lx + 6L ) = 0
8x 1500x + 60000 = 0
2
x 187.5x + 7500 = 0
and
syms x y; y=solve('x^2187.5*x+7500')
56
The model in Figure 5.53 can now be used with the circuit of Figure 5..48 for any values of the
impedances Z .
Figure 5.54. Model for Discrete Time Integration with variable amplitude input
537
SFunction Example 2
The syntax for the diode_sfcn.m file above is the similar to that in Example L.20, Page L62,
Appendix L
Next, we open a new model window, from the UserDefined Functions Library we drag an SFunction block into it, we doubleclick this block, in the Function Block Parameters dialog box we
name it diode_sfcn, and we add and interconnect the other blocks shown in Figure 5.74.
Scope 1
Scope 2
Figure 5.56. Model and waveforms for temperature coefficients in semiconducto diodes using an SFunction block
d
d
M = J -------2- + c ------ + mgL sin
dt
dt
or
(5.50)
d
-------2- = 1--- M c d
------ mgL sin
J
dt
dt
(5.51)
541
Imaginary part of Z
400
200
0
-200
-400
-600
200
400
600
1400
1600
1800
2000
Figure A.12. Plot for the imaginary part of the impedance in Example A.15
% The last six statements (next five lines) below produce the polar plot of z
mag=abs(z);% Computes |Z|;...
rndz=round(abs(z));% Rounds |Z| to read polar plot easier;...
theta=angle(z);% Computes the phase angle of impedance Z;...
polar(theta,rndz);% Angle is the first argument
ylabel('Polar Plot of Z'); grid
90
120
1500
60
1000
Polar Plot of Z
150
30
500
180
210
330
240
300
270
Example A.15 clearly illustrates how powerful, fast, accurate, and flexible MATLAB is.
A24
Figure B.9. The Function Block Parameters window with the values of the constants
With the variables defined as above, the masked subsystem implements the quadratic equation
2
y = x 5x + 6
and the roots of this equation are x 1 = 2 and x 2 = 3 . Our model is tested for the first root as
shown in Figure B.10.
Figure B.10.
The Mask Editor also contains the Initialization tab that allows us to enter MATLAB commands
that initialize the masked subsystem, and the Documentation tab that lets us define or modify the
type description and help text for a masked subsystem. These tabs are not used in this example.
B8
C22
Vector Concatenate
The Data Type Demonstration Simulink demo is another model using Data Type Conversion
blocks. It can be accessed by typing datatypedemo at the MATLAB command prompt.
Note: The MATLAB and Simulink builtin integer data types are limited to 32 bits. If we want to
output fixedpoint numbers in the range 33 and 53 bits, we can break the number into
pieces using Gain blocks. For details please refer to the Simulink Help menu for an example.
C49
PID Controller
absence of an integral value prevents the system from reaching its target value due to the control
action.
In general, we add a proportional control to improve the rise time, we add a derivative control to
improve the overshoot, and we add an integral control to eliminate the steady-state error. Then, we
adjust each of Kp, Ki, and Kd until we obtain a desired overall response.
Example D.4
The model in Figure D.18 produces the outputs of a transfer function without and with PID control. The output waveforms are shown in Figure D19.
Figure D.19. Output waveforms for the model in Figure D.18 without and with PID control
The Simulink documentation contains the Anti-Windup Control Using a PID Controller, and Bumpless
Control Transfer Between Manual and PID Control demos.They can be accessed by typing
sldemo_antiwindup and sldemo_bumpless at the MATLAB command prompt.
Introduction to Simulink with Engineering Applications, Third Edition
Copyright Orchard Publications
D13
Ra
Jm s
Tm ( s ) = Ki Ia ( s )
Va ( s )
Ia ( s )
m ( s )
Vb ( s ) = Kb m ( s )
m ( s )
( shaft angle )
Bm
Armature circuit
Inertial load
Figure D.21 is the block diagram of the DC motor where T L ( s ) = load torque , i.e., the torque that
the motor has to overcome for motion to occur, and Figure D.22 is a model for this block diagram
where the integrator block on the right side has been omitted for simplicity.
TL ( s )
Va ( s )
1
---------------------- I a ( s )
La s + Ra
Ki
Tm ( s )
1
---------------------Jm s + Bm
m ( s )
1
--s
m ( s )
Vb ( s )
Kb
D15
Appendix D ContinuousTime
Figure D.45. Model to illustrate the use of the Variable Time Delay block
Figure D.46. Input and output waveforms for the model in Figure D.45
Example D.12
The model in this example compares the distance traveled by two automobiles that departed from
point A, one three hours after the other at different speeds, and stopped at point B ten hours later.
D30
Appendix E
Discontinuities
his appendix is an introduction to the Discontinuities Blocks library. This is the third
library in the Simulink group of libraries and contains the blocks shown below. Their functions are illustrated with examples. In this appendix we will briefly describe the function of
each block included in this group, and their application will be illustrated with examples. The complete description of each block can be found in the Simulink documentation. For emphasis, the
icon of each Simulink block described in its section is shown in drop shadows.
E.1 Saturation
The Saturation block sets upper and lower bounds on a signal. When the input signal is within the
range specified by the Lower limit and Upper limit parameters, the input signal passes through
unchanged. When the input signal is outside these bounds, the signal is clipped to the upper or
lower bound. When the Lower limit and Upper limit parameters are set to the same value, the
block outputs that value. This block is also described in Section 2.13, Chapter 2, Page 221.
Example E.1
The model shown in Figure E.1 uses a Saturation block whose upper limit is clipped at +0.25 and
the lower limit is clipped at 0.25 . The input is a sine function with amplitude 1 and frequency
0.25 Hz . The input and output waveforms are shown in Figure E.2.
* Another example with the Saturation block is presented as Example 2.13, Chapter 2, Page 221.
E1
Appendix E Discontinuities
E.11 Coulomb and Viscous Friction
As we know from physics, friction is a force that resists the relative motion or tendency to such
motion of two bodies in contact. Friction is undesirable in some parts of rotating machinery such
as bearings and cylinders, but very beneficial in the automotive industry such as the design of
brakes and tires. Theoretically, there should be no friction in a motor with zero velocity, but in reality, a small amount of static (no velocity) friction known as Coulomb friction, is always present
even in roller or ball type antifriction bearings. Viscous friction, on the other hand, is friction
force caused by the viscosity of lubricants.
The Coulomb friction function, the Viscous friction function, and the combined Coulomb plus
Viscous friction functions are illustrated in Figure E.21.
Friction force F fc
Friction force
C Fn
V v Fn
v
Sliding velocity
v
Sliding velocity
v
Sliding velocity
F fv
(E.4)
(E.5)
(E.6)
The Coulomb and Viscous Friction block produces an offset at zero and a linear gain elsewhere.
The offset corresponds to the Coulombic friction; the gain corresponds to the viscous friction.
Coulomb friction force can be represented by at least four different continuous functions. Each of
these functions involves one constant that controls the level of accuracy of that function's representation of the friction force. Simulink uses the default values [ 1 3 2 0 ] for the offset (Coulomb
friction value). For the signal gain (coefficient of viscous friction) at nonzero input points the
default is 1. For a detailed description, please refer to the Simulink documentation.
E18
F17
where the last value is the most recent. We will create a model of moving averages with k = 5 over
t h i s 10 d ay t r a d i n g p e r i o d . Fo r t h i s e x a mp l e , t h e m ov i n g ave r a g e s e qu e n c e h a s
n k + 1 = 10 5 + 1 = 6 values.
The model is shown in Figure F.30, and the settings for the Function Block Parameters dialog box
for the Discrete FIR Filter 1 block is shown in Figure F.31. The settings for the remaining Discrete
FIR Filter blocks are shown in the model.
F23
in general, ave ( x ) ( x ave ) . The term on the left implies that x must first be squared and the
average value is then to be found. The term on the right implies that the average value of x must be
found first, and then the average must be squared. For this model each of the elements of the array
[1 2 3 4 5] is first squared and the mean (average) value is found to be 11 . Then, the mean is first
found and its square is computed and found to be 9 . Since the results are different, the Relational
Operator block outputs logical 0 .
G2
Appendix H
Lookup Tables
his appendix is an introduction to the Lookup Tables Group of blocks and only a brief
description is given. A complete description of each block can be found in the The MathWorks Simulink documentation. Their functions are illustrated with examples. The terminology monotonically increasing * is used throughout this chapter, and it is defined in the footnote
below.
1.5 10 yr if a U
234
234
disintegration constant = 8.8 10 sec . The number of radioactive atoms of the original
kind still present at any time t is computed from the relation
N = N0 e
= N0 e
(H.1)
* Monotonically increasing and monotonically decreasing sequences are sequences in which the successive values either consistently
increase or decrease but do not oscillate in relative value. Each value of a monotonic increasing sequence is greater than, or equal
to the preceding value; likewise, each value of a monotonic decreasing sequence is less than, or equal to the preceding value.
Stated in other words, a monotonically increasing function is one resulting from a partially ordered domain to a partially ordered
range such that x y implies that f ( x ) f ( y ) . Likewise, a monotonically decreasing function is one resulting from a partially
ordered domain to a partially ordered range such that x y implies that f ( x ) f ( y ) .
Isotopes are atoms which have the same atomic number (number of protons) but different number of neutrons. The sum of the
number of protons and neutrons in the nucleus is referred to as the mass number. The superscript 234 represents the mass number.
H1
Lookup Table
Another example using this block appears in the Simulink Modeling AntiLock Braking System
demo. It can be accessed by typing sldemo_absbrake at the MATLAB command prompt.
H3
Sum of Elements
I.4 Sum of Elements
The Sum of Elements block is another form of the Add block described above with one input and
one output.
Example I.4
The model in Figure I.4 illustrates Gibbs phenomenon.*
I.5 Bias
The Bias block adds a bias (offset) to the input signal.
* The Gibbs phenomenon shows that Fourier series sums overshoot at a jump discontinuity, and that this overshoot does
not disappear as the number of harmonics increases. For a detailed discussion please refer to Chapter 7, Signals and
Systems, ISBN 978-1-934404-11-9.
I3
Polynomial
I.18 Polynomial
The Polynomial block accepts the real coefficients a polynomial in the Parameters dialog box and
at the input we specify real values at which the polynomial will be evaluated. It is equivalent to the
MATLAB polyval function.
Example I.18
We will create a model using the Polynomial block to evaluate the polynomial
6
p ( x ) = x 3x + 5x 4x + 3x + 2
and in the Parameters dialog box for the Polynomial block we have typed px.
Introduction to Simulink with Engineering Applications, Third Edition
Copyright Orchard Publications
I13
J5
Appendix K
ModelWide Utilities
his appendix presents a brief description of the Simulink ModelWide Utilities group. The
function of each block and their inputoutput signals are illustrated with examples. For
more information please refer to the Simulink documentation for each block.
This is the same model as that in Figure D.4, Example D.3, Page D4, Appendix D, where with the
execution of the command [A,B,C,D]=linmod('Fig_D_04') the linear model in the form of the
statespace MATLAB displayed the four matrices as
* The trim function uses a Simulink model to determine steadystate points of a dynamic system that satisfy input, output, and state conditions that we can specify. For details please type help trim at the MATLAB command prompt.
K1
Figure L.18. Outputs when the Pulse Generator block is removed from the subsystem of Figure L.17
Figure L.19. The Enable block with the Show output selected
L15
Figure L.28. The library with the addition of the Configurable Subsystem
We double click on Configurable Subsystem block in Figure L.28 and in the Configuration dialog
box we place check marks in the Member squares as shown in Figure L.29. The Port names pane
shows the inputs indicated as Inports. To see the outputs, we click the Outports tab.
Figure L.29. The Configuration dialog for the library of Example L.6
We can now select either Subsystem 1A or Subsystem 2A from the Configuration dialog to implement the 4bit Adder by cascading four onebit adders. For this example we choose Subsystem 1A.
L20
Configurable Subsystem
This is done by unselecting Subsystem 2A on the Configuration dialog box of Figure L.29 which
now appears as shown in Figure L.31.
Now, a new model is created as shown in Figure L.32 by dragging the Configurable Subsystem 1A
block* from this library into this model, copy and paste this block three times, and we interconnect
these as shown in Figure L.32.
Figure L.32. Fourbit binary adder for Example L.6 with Subsystem 1A blocks
In the model of Figure L.32, the carry bit of the rightmost onebit adder is set to zero since there
is no previous onebit adder. We observe that Simulink displays small arrows on the bottom left
corner of each of the subsystem blocks. A small arrow indicates that each subsystem block represents a library link in the model, and it is only visible if the Library Link Display option of the Format menu is selected All.
* All four subsystem and constant blocks are flipped to indicate that the addition begins at the right side and proceeds to
the left.
L21
Bus to Vector
Figure M.19. Model with the Signal Specification block acting as a virtual block
Figure M.20. Model where the attributes (sample times) of the Signal Specification block do not agree
M13
For this model, the row vectors A, B, and C are entered at the MATLAB command prompt as follows:
A=[90 85 82 80 77]; B=[84 90 85 82 80]; C=[81 84 90 86 82];
The parameter dialog box for the Selector 1 block is shown in Figure N.10.
Figure N.10. Parameter dialog box for the Selector 1 block in Figure N.9
N6
Appendix O Sinks
Example O.7
The model in Figure O.13 produces the curve in Figure O.15 and it is known as nephroid. It is
described by the equations
x = 0.5a ( 3 cos cos 3 )
y = 0.5a ( 3 sin sin 3 )
For this model, the value of a is entered at the MATLAB command prompt as a=2;
Two more examples with the XY Graph block presented in Section 5.1.6, Page 512, Chapter 5,
and Section 5.1.7, Page 515, Chapter 5.
O10
Signal Builder
7. When we select a line segment on the time axis (xaxis) we observe that at the lower end of the
waveform display window the Left Point and Right Point fields become visible. We can then
reshape the given waveform by specifying the Time (T) and Amplitude (Y) points.
8. We can choose one of several waveforms listed in New under the Signal drop menu.
The menu bar at the top contains several icons that we could use to choose from and/or modify
our waveform.
Example P.5
We begin with a new model, we drag a Signal Builder block, we click it, and on the top bar we click
Signal>Replace with>Triangle wave>OK. We would like to change it to a symmetric triangle with
Period 2, and Amplitude varying from 1 to +1 as shown in Figure P.16 where we have specified
that the Time (T) and Amplitude (Y) points as (0,1), (1,1), (2,1), (3,1), (4,1), (5,1), (6,1), (7,1),
(8,1), (9,1), and (10,1). We could also use the Repeating Sequence block discussed in Section
P.13.
To this model we add an Integrator block and a Scope block, and after the simulation command is
executed, the model of Figure P.17 displays the triangular waveform and its integrated waveform in
as shown.
P11
The Simulink documentation includes the Modeling Engine Timing Using Triggered Subsystems
demo. It can be accessed by typing sldemo_engine at the MATLAB command prompt, and clicking the Combustion block.
For this example, the author used the Microsoft Visual C++ 2010 compiler.
In this example we will create a model using a MATLAB Function block to accept a 3 3 matrix
and output the value of its determinant and its inverse matrix.
Q2
Appendix R
Additional Discrete
his appendix presents a brief description of the Simulink Additional Discrete group. The
function of each block and their inputoutput signals are illustrated with examples. For
more information please refer to the Simulink documentation for each block.
(R.1)
In Figure R.1, the Sample time for the Sine Wave block is specified as 0.1. The numerator and
denominator coefficients for the Transfer Fcn Direct Form II block are entered in the parameters
dialog box and the leading coefficient 1 in the denominator is excluded, that is, it is entered as
[ 1.7600 1.1829 ] .
The input and output waveforms are shown in Figure R.2.
* By lead we mean that the leading coefficient 1 in the denominator which has the form 1 + z1 + z 2 .
R 1
S2
Cross Correlator
The auto correlation signal above justifies the following two properties of the auto correlation function:
1. The auto correlation function is an even function, i.e., R xx ( ) = R xx ( )
2. The auto correlation function is maximum at the origin, i.e., R xx ( 0 ) R xx ( ) .
tp
x ( t )y ( t ) dt
(19.2)
The cross correlation R xy ( ) is an indication of how well the signals x and y are correlated for a
particular value of the delay . If the cross correlation function peaks for a particular value of ,
this would indicate a very good correlation, which means that the two signals match each other very
* Convolution is discussed in Signals and Systems with MATLAB Computing and Simulink Modeling, ISBN 9781
934404119, Chapter 6.
T13
Appendix U
Random Number Generation
U.2 An Example
A typical random number generator creates a sequence in accordance with the following recurrence:
x n + 1 = P 1 x n + P 2 ( mod N ) n=0,1,2,
x 0 = seed
(U.1)
where mod N is used to indicate that the sum P 1 x n + P 2 is divided by N and then is replaced by the
remainder of that division. The values of x 0 (seed), P 1 , P 2 , and N must be specified. As an example, let
x0 = 1
P 1 = 281
P 2 = 123
N = 75
To find the sequence of numbers for the random number generator, we use the following MATLAB
script:
P1=281; P2=123; N=75; x=1:100; y=zeros(100,2);
y(:,1)=x'; y(:,2)=mod((P1.*x+P2),N)'; fprintf(' x y \n'); disp(' ------');
fprintf('%3.0f\t %3.0f\n',y')
U1
Appendix V
Exercises
Dear Reader:
The solutions to the exercises in this appendix can be found in Appendix W.
You must, for your benefit, make an honest effort to solve these exercises without first looking at the
solutions. You may need to review Appendices C through U and / or the blocks in the Simulink
documentation to locate suitable blocks for your models.
1. Use Simulink with the Step function block, the ContinuousTime Transfer Fcn block, and the
Scope block shown, to simulate and display the output waveform v C of the electric RLC circuit*
shown below where u 0 ( t ) is the unit step function, and the initial conditions are i L ( 0 ) = 0 , and
vC ( 0 ) = 0 .
+
u0 t
1H
iL
vC
1F
2. Repeat Exercise 1 using integrator blocks in lieu of the transfer function block.
* The electric circuits presented in this texts can be easily converted to their mechanical equivalent circuits where a voltage source can be replaced by a force f, a resistor can be replaced by a dashpot B, an inductor can be replaced by a
mass M, and a capacitor can be replaced by an inverse spring constant 1/K. Thus the mechanical analog of the electric circuit above can be converted to the mechanical system shown below.
1K
B
f
M
x1
x2
x3
V1
Appendix W
Solutions to the Exercises
1.
The sdomain equivalent circuit is shown below.
1 +
--s
Ls
V IN ( s )
+
1/sC
V C ( s ) = V OUT ( s )
1 +
--s
s
V IN ( s )
+
1/s
V C ( s ) = V OUT ( s )
from which
V OUT ( s )
s
- = --------------------Transfer function = G ( s ) = ------------------2
V IN ( s )
s +s+1
We invoke Simulink from the MATLAB environment, we open a new file by clicking on the
blank page icon at the upper left on the task bar, we name this file Fig_W_01, and from the
Sources, Continuous, and Commonly Used Blocks in the Simulink Library Browser, we select
and interconnect the desired blocks as shown below.
As we know, the unit step function is undefined at t = 0 . Therefore, we double click the Step
block, and in the Source Block Parameters dialog box shown below we enter the values indicated. Likewise, we double click the Transfer Fcn block and in the Source Block Parameters dialog box we enter the values indicated.
W1
(X.2)
(X.3)
Example X.1
The price of a particular security (stock) over a 5day period is as follows:
77 80 82 85 90
where the last value is the most recent. We will create single-input / single output (SISO) model
with a Discrete FIR Filter block to simulate the weighted moving average over this 5day period.
For this example, we will represent the SISO output as follows:
y1 ( k ) = a1 u ( k ) + b1 u ( k 1 ) + c1 u ( k 2 ) + d1 u ( k 3 ) + e1 u ( k 4 )
(X.4)
u ( k ) = 5 15 u ( k 1 ) = 4 15 u ( k 2 ) = 3 15 u ( k 3 ) = 2 15 u ( k 4 ) = 1 15
(X.5)
where
The model is shown in Figure X.1 where in the Function Block Parameters dialog box for the Discrete FIR Filter block we have entered the parameters shown in Figure X.2.
Figure X.2. Parameters for the Discrete FIR Filter block in Figure X.1
X2
Index
Symbols
C
C MEX-file S-function Q-9
c2d function in MATLAB F-14
callback methods Q-8
canonical form 5-31
cantilever beam deflection 5-4
capillary tube 4-1
capital investment 2-20
Cartesian to Polar Transformation T-27
Cartesian to Spherical Transformation T-29
Cascade Form Realization 5-18
cascaded mass-spring system
Celsius to Fahrenheit Transformation T-29
Check Discrete Gradient block J-9, K-8
Check Dynamic Gap block J-7
Check Dynamic Lower Bound block J-4
Check Dynamic Range block J-6
Check Dynamic Upper Bound block J-5
Check Input Resolution block J-10
Check Static Gap block J-3
Check Static Lower Bound block J-1
Check Static Range Block J-2
Check Static Upper Bound block J-2
chemical solutions example 5-1
Chirp Signal block P-15
clc command in MATLAB A-2
clear command in MATLAB A-2
Clock block P-25
Clock for digital systems block T-20
closed-loop control systems 5-30
Code Reuse Subsystem block L-7
column vector in MATLAB A-17
Combinatorial Logic block G-4
command screen in MATLAB A-1
command window in MATLAB A-1
commas in MATLAB A-7
comment line in MATLAB A-2
commonly used blocks C-1
compare to constant block G-7
compare to zero block G-6
comparison of alternate proposals 2-15
complex conjugate in MATLAB A-4
complex numbers in MATLAB A-2
complex to magnitude-angle block I-23
complex to real-imag block I-25
configurable subsystem block L-17
Configuration Parameters 1-12
configuration parameters C-15
conical reservoir 12
conj (complex conjugate) I-11
Constant block C-7, P-2
Contents Pane 1-7
continuous mode I-17
control element 5-30
IN-1
IN-2
F
Fahrenheit to Celsius Transformation T-29
Fcn block Q-1
Feedback Control Systems 5-30
figure window in MATLAB A-13
Find Nonzero Elements block I-30
finite impulse response (FIR)
digital filter F-8
first harmonic 16
First-Order Hold block F-25
Fixed-Point State-Space block R-3
flip block command 1-11
Floating Bar Plot T-18
Floating Scope block O-5
For Each Subsystem block L-52
For Iterator Subsystem block L-35
format command in MATLAB A-29
fplot command in MATLAB A-25
From block N-14
From File block P-1
From Workspace block P-1
Function Block Parameters 1-9
Function Call Split block L-57
function file in MATLAB A-25
Function Lookup Table Generation
block H-17
Function-Call Feedback Latch L-58
Function-Call Generator block L-2
Function-Ccall Subsystem block L-32
fundamental frequency H-16
fzero command in MATLAB A-25, 27
G
Gain block 1-18, C-25, I-5
geometric series 2-1
Goto block N-16
Goto tag visibility block N-15
grid command in MATLAB A-11
Ground block C-4, P-1
gtext(string) command in MATLAB A-13
H
half-wave symmetry H-17
heat flow example 5-3
help matlab ofun command in MATLAB A-2
hermitian function I-11
hide name C-2
Hit Crossing block E-16
hypot function I-11
I
IC (Initial Condition) block M-8
Idealized ADC Quantizer block T-7
If Action Subsystem block L-38
If block L-38
imag(z) function in MATLAB A-22
impulse response duration F-8
Increment Real World block S-1
mass-spring-dashpot 5-25
materials costs 2-16
Math function block I-11
MATLAB demos A-2
MATLAB Fcn block Q-2
Matrix Concatenation block I-22
matrix multiplication in MATLAB A-17
mechanical accelerometer 5-29
mechanical and electrical
components - analogies of 5-25
Memory block F-24
Merge block N-9
mesh(x,y,z) command in MATLAB A-15
meshgrid(x,y) command in MATLAB A-16
Method of Frobenius 5-8
Method of Picard 5-8
m-file in MATLAB A-1, 25
M-file S-Functions L-59
MinMax block I-14
MinMax Running Resettable block I-15
mod I-11
Model block L-16, L-42
Model Info block K-6
Model Variants L-42
moving average F-22
moving ladder 2-10
Multiport Switch block N-13
Mux block C-18, N-4
N
NaN in MATLAB A-25
nephroid O-10
Newtons Second Law 3-1
nonlinear systems E-20
non-recursive realization
digital filter F-8
nonvirtual subsystems L-7, L-62
O
open--loop control systems 5-30
oscillatory motion 3-20
Outport block C-1, L-1, O-1
overdamped motion 3-20
IN-3
Z
Zero-Order Hold and First-Order Hold
as Reconstructors 5-14
Zero-Order Hold block F-26
Zero-Pole (with initial outputs) block T-11