Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
php
Chapter 1
Introduction to Simulink
The best way to follow the details of this book is to have a copy of Simulink and work
the examples as you read the text. The examples described in the book are all in the
Numerical Computing with Simulink (NCS) library that is available as a download from
The MathWorks (www.mathworks.com). The files are located in an ftp directory on the site.
As you work your way through this text, the Simulink and MATLAB files you will
run are in boldface Courier type. When you need to run a model or M-file, the text
shows the exact MATLAB command you need to type.
After you download the NCS library, place it on the MATLAB path using the “Set
Path” command under the MATLAB file menu. Alternatively, you can save the NCS files
and use the MATLAB directory browser (at the top of the MATLAB window) or the cd
command to change to the directory that contains the NCS library.
1
2 Chapter 1. Introduction to Simulink
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
at the MATLAB command line (or alternatively by clicking on the Simulink icon that
appears below the menu at the top of the MATLAB window). In either case, the result is
that you will open the Simulink browser and a window for building the model.
The browser consists of a catalog of 16 types of blocks. The catalog is a grouping of the
blocks into categories that make it easier for the user to find the appropriate mathematical
1.1. Using a Picture to Write a Program 3
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
element, function, operation, or grouping of elements. A quick look at the list shows
that the first element in the catalog is the “Commonly Used Blocks.” This, as the name
implies, is redundant in that it contains blocks from other elements in the catalog. The
second set of elements in the library is denoted “Continuous.” This element contains the
operator blocks needed to build continuous time ordinary differential equations and perform
numerical integration. Similarly, the library element “Discrete” contains the operators
needed to develop a discrete time (sampled data) system. All math operations are in the
“Math Operations” catalog element.
To understand how to build a Simulink model, let us build a simple system.
In many applications, simple trigonometric transformations are required to resolve
forces into different coordinates. For example, if an object is moving in two dimensions
with a vector velocity v at an angle θ relative to the x-axis of a Cartesian coordinate system
(see figure), then the velocity along the x-axis is v cos(θ ) and along the y-axis is v sin(θ ).
We will assume that the velocity v is a MATLAB variable that is in the MATLAB
workspace. To bring this variable into the Simulink model, a block from the Sources library
is required. To open this library, locate the Sources Library in the Simulink Browser.
This will open a large number of Simulink blocks that provide sources (or inputs) for the
model. The “Constant” block allows you to specify a value in MATLAB (created using the
MATLAB command window), so click on it and drag it into the untitled model window.
To make this input the velocity v, double click on the Constant block to open the “Block
Parameters” window. In general, almost all Simulink blocks have some form of dialog that
sits (virtually) behind the block; open these dialogs by double clicking the block. As we
go through the steps in building a model, we will investigate some of the dialogs. It is a
good idea to spend some time familiarizing yourself with the contents of these dialogs by
browsing through the library one block at a time and opening each of their dialogs to see
what is possible. For now, in the “Block Parameters” window change the Constant value to
v, and click OK to close the “Block Parameters” window.
We are now ready to create the signal flow in the diagram. Look carefully at the icons
in the model. You will see arrowheads at either the input or output (or both) for each of
the blocks. You can connect blocks manually or automatically. To connect them manually,
click the mouse on the arrowhead at the output of the constant block, drag the line to the
input of the Trigonometric Function block (either one), and release the mouse button when
the icon changes from a plus sign to a double plus sign. This should leave a line connecting
the input to the trig block. To connect the second trig block, start at the input arrow of
this block and drag the line to the point on the previous connection that is closest to the
block. This will take a little getting used to, so try it a couple of times. The last part of
the modeling task is to change the sin function (the default for the trigonometric function
from the library) to a cos. This block has a dialog behind it that allows changes, so double
click on the block that will become the cosine and use the pull down menu in the dialog
called “Function” to select cos. Two additional boxes in the dialog allow you to specify the
output type (auto, real, or complex) and the sample time for the signal. We can ignore these
advanced functions for now. Try to move the blocks around so they look like the blocks in
the Simplemodel figure above.
As you were connecting the first two blocks, a message from Simulink should have
appeared, telling you that there is an automatic way of connecting the blocks. Do this by
clicking (and thereby highlighting) the block that is the input and, while holding down the
control (Ctrl) key, clicking on the block that this input will go to. Simulink will then create
(and neatly route) a line to connect the two blocks.
This simple model introduces you to click and drag icon-based mathematical models.
It is simple, obviously too simple for the work needed to create it. It would be far easier to
do this in MATLAB.
Simulink provides a visual representation of the calculations involved in a process. It
is easier to follow complex calculations visually, particularly if they involve flow in time.
Toward this end, it is very possible to build a diagram that obscures the flow rather than
enhances it. We call such a diagram a “spaghetti Simulink model” (like spaghetti code).
To avoid this, it is important to make the connections clear and the model visually easy to
navigate. Simulink allows you to do this by dragging the icons around the diagram to make
it look better. It is a good idea to get used to this right from the start. Therefore, spend a
few minutes manipulating the diagram with the mouse. In addition, blocks move (nudged
1.2. Example 1: Galileo Drops Two Objects from the Leaning Tower of Pisa 5
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
up or down and back and forth) with the arrow keys. Simply highlight the block and press
the arrow key to move it one grid point.
To flip a block, highlight it and use Ctrl + i, and to rotate a block, highlight it and use
Ctrl + r. Once again, try these steps. If you want to save the example you created, click the
floppy disk icon at the top of the model window and save the model in any directory you
want. (Be careful not to overwrite the model “Simplemodel” in the NCS directory.)
1 This photograph is from my personal collection. It was taken during a visit to Pisa in May of 2006. If Galileo
performed the experiment, he most likely would have used the upper parapet at the left of the picture. (Because
of the offset at the top, it would have been difficult to use the very top of the tower.)
6 Chapter 1. Introduction to Simulink
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
object. The experiment demonstrated that the objects both hit the ground at the same time.
Is it true that this would happen? Let us see (this model is in the NCS library, where it is
called Leaningtower—you can open it from the library, but if this is your first experience
with Simulink, use the following instructions to create the model instead).
Thanks to Newton, we know how to model the acceleration due to gravity. Denote
the acceleration at the surface of the earth by g (32.2 ft per sec per sec). Since the force on
the object is mg and Newton’s first law says that
F = ma,
we have
d 2x
m = −mg.
dt 2
From this, it follows immediately that the mass of the object does not enter into the calcu-
lation of the position (and velocity), and the underlying equation of motion is
d 2x
= −g.
dt 2
We assume that Galileo dropped the balls from the initial location of 150 ft, so the initial
velocity is dx(0)
dt
= 0 and the initial position is x(0) = 150.
Even though this formulation of the equation of motion explicitly shows that Galileo’s
experiment has to work, we continue because we are interested in seeing if the experimental
results would in fact have shown that the objects would touch the ground at the same
time. Initially we model just the motion as it would be in a vacuum. A little later in this
section, we will add air drag to the model. The model of any physical device follows
this evolutionary path. Understanding the physical environment allows the developer to
understand the ramifications of different assumptions and then add refinements (such as air
drag) to the model.
The equation above is probably the first differential equation anyone solves. It has a
solution (in the foot-pound-second [fps] system) given by
dx(t)
= −32.2t,
dt
x(t) = −16.1t 2 + 150.
Since the experiment stops when the objects hit the ground, the final time for this experiment
is when x(t) = 0. From the analytic solution, we find that this time is
150
t= .
16.1
Go into MATLAB and compute this value. MATLAB’s full precision for the square root
gives
>> format long
>> sqrt(150/16.1)
ans =
3.05233847833680
1.2. Example 1: Galileo Drops Two Objects from the Leaning Tower of Pisa 7
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
We will return to this calculation after we use Simulink to model and solve this
equation because it will highlight a feature of Simulink that is numerically very important.
Now, to build the Simulink model of the motion we need to create the left-hand side
of the differential equation and then integrate the result twice (once to get the velocity and
then again to get the position).
Five types of Simulink blocks are required to complete this model:
• Three input blocks are used to input the constant g into the equation and to provide
the initial position and to provide a value to test the position against to determine
whether the object has reached the ground.
• Two integrator blocks are needed to compute the velocity and position from the
acceleration.
• One block will perform the logic test to see when the object hits the ground.
• One block will receive the signal from the logic block to stop the simulation.
• One block will create a graph of the position and velocity as a function of time.
So let us start building the model. As we did above, open up the Simulink browser
and a new untitled model window. Accumulate all of the blocks in the window before we
start to make connections. Thus, open the Sources, Continuous, Logic and Bit Operations,
and Sinks libraries one by one, and grab the icons for a Constant, an Integrator, a Relational
Operator, and the Stop and drag them into the untitled model window.
The integrator block in the Continuous library uses the Laplace operator 1s to denote
integration. This is a bit of an abuse of notation, since strictly speaking this operator
multiplies the input only if it is the Laplace transform of the time signal. However, Simulink
had its origins with control system designers, and consequently the integration symbol
remains in the form that would appear in a control system block diagram.
In fact, the integrator symbol invokes C-coded numerical integration algorithms that
are the same as those in the MATLAB ODE suite to perform the integration. The C-coded
solvers are compiled into linked libraries that are invoked automatically by Simulink when
the simulation is executed. (Simulink handles all of the inputs, outputs, initial conditions,
etc. that are needed.) We will talk a little more about the ordinary differential equation
solvers later, but if the reader is interested in more details about the numerical integration
solvers, consult Chapter 7 of NCM [29].
8 Chapter 1. Introduction to Simulink
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Make copies of the integrator and the constant block (since we need two integrators
and three constants); as before, either copy it by right clicking or drag a second block into
the model from the browser.
Make the connections in the model. Next, change the Constant block values by double
clicking to open its dialog box. Change the first of these to make the value –g, change the
second to make the value 0 (which will be used to test when the position of the object is at
the “ground”), and then change the third so its value is 150 (the initial position of the object
at the top of the Leaning Tower of Pisa).
Modify the integrator block to bring the initial conditions into the model. When
you double click the integrator block, the “Block Parameters” menu, shown in Figure 1.3,
appears.
In this menu, you can change the way the numerical integration handles the integral.
The options are as follows:
• Force an integration reset (to the initial condition) when an external reset event occurs.
This reset can be increasing as a function of time (i.e., whenever the reset signal is
increasing, the integrator is reset), it can be decreasing, or it can be either (i.e., the
integration will take place only if the reset signal is unchanging), and finally, the reset
can be forced when the reset signal crosses a preset threshold.
• The initial conditions can be internal or external. If it is internal, there is a box called
“Initial condition” (as shown above) for the user to specify the value of the initial
1.2. Example 1: Galileo Drops Two Objects from the Leaning Tower of Pisa 9
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Figure 1.4. Simulink model for the Leaning Tower of Pisa experiment.
• The output of the integrator can be limited (i.e., it can be forced to stay constant if
the integration exceeds some minimum or maximum value).
Other options are available that will be discussed as we encounter the need for them.
Set one of the integrators to have an external initial condition, and leave the other at
its default value. (The default is internal with an initial condition of 0.) Then connect the
blocks, as we did before, so the model looks like Figure 1.4.
The default time for the simulation is 10 sec, which is ample time for the object to hit
the ground. Before we start the simulation, we need to specify a value for g (we used the
value −g in the dialog for the Constant, and it must be created). To do this, in MATLAB
type the command
g = 32.2;
Now start the simulation by clicking the right pointing “play” arrow (highlighted with
a circle in Figure 1.4). The simulation will run. Double click on the Scope icon and then
click the binoculars (at the top of the Scope plot window) to scale the plot properly. The
result should look like Figure 1.5.
10 Chapter 1. Introduction to Simulink
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Figure 1.5. Scope block graph for the Leaning Tower simulation.
Notice that the simulation stopped at a time just past 3 sec. We can see how accurately
Simulink determined that time by zooming in on the plot. If you put the mouse pointer over
the zero crossing point and click, the plot scale will change by about a factor of three. If
you do this some number of times, you will see that the crossing time is about 3.0523. The
plot will not resolve any finer than that so we cannot see the zero crossing time any better
than this.
The default for Simulink is to use the variable step differential equation solver ode45.
This variable step solver forces the integration to take as long a step as it can consistent
with an accuracy of one part in 1000 (the default relative tolerance). Simulink also sends
(by default) the time points at which the integration occurred back to MATLAB. The time
values are in a vector in MATLAB called tout. To see what the zero crossing time was,
get the last entry in tout by typing
tout(end)
format long
The answer should be 3.05233847833684. Remember that the stop time computed
above with MATLAB (using the solution of the equation) was 3.05233847833680, a dif-
ference of 4 in the last decimal place (i.e., a difference of 4 × 10−14 , which is a little more
than the computation tolerance variable in MATLAB called eps). How can the Simulink
numerical integration compute this so accurately?
1.2. Example 1: Galileo Drops Two Objects from the Leaning Tower of Pisa 11
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
This remarkable accuracy in the numeric calculation of the stopping time is because
of a unique feature of Simulink. The variable step integration algorithms that are used to
solve the differential equations in a Simulink model will compute the solution at time steps
that are as big as possible consistent with the relative tolerance specified for the algorithm.
If, in the process of computing the solution, a discontinuity occurs between any two times,
the solver forces an interpolation to determine the time at which the discontinuity occurred.
Only certain Simulink blocks cause the solver to interpolate. These are as follows:
Now that we have simulated the basic motion of Galileo’s experiment, let us add the
effects of air resistance to the model and see if, in fact, the objects hit the ground at the same
time.
Air drag creates a force that is proportional to the square of the speed of the object. To
first order, an object with cross sectional area A moving at a velocity v in air with density
ρ will experience a drag force given by
1 2
fDrag = ρv A.
2
(In the model so far we have assumed that the force from gravity is downward and is
therefore negative, but the air drag force is up so it is positive.) In the fps unit system we
have been using, the value of ρ is 0.0765 lbf/ft3 . Let us assume that the objects are spherical
and that their diameters are 0.1 and 0.05 ft each. (If they are made of the same material, the
large object will then be 8 times heavier than the small object.)
The acceleration of the objects is now due to the force from the air drag combined
with the force from gravity, so the differential equation we need to model is now
2
d 2x 1 dx
= ρA − g.
dt 2 2 dt
To get the air drag force for each object we need to know their cross-sectional areas. Since
the cross-sectional area is πr 2 , the acceleration from the air drag for the large object is
1
2
ρπ r 2 = ρ2 (0.1)2 π = 0.005πρ and the acceleration for the small object is 12 ρπ (0.05)2 =
.00125πρ (they differ by a factor of 4).
To create the new model, we need to add the air drag terms. Thus, we need a way to
add the force from the air resistance to the force from gravity. This uses a Sum block from
the Math library. This block is a small circle (or a rectangle—my personal preference is
the circle since it makes it easy for the eye to distinguish summations from the other icons
that are mostly rectangular) with two inputs, (each of which has a small plus sign on it). So
click and drag this block into the leaning tower model. To get the square of the velocity, we
need to take the output of the first integration (the integration that creates the velocity from
the acceleration) and multiply it by itself. The block that does this is the Product block in
the Math library. Drag a copy of this block into the model also.
We will start by creating the model for the heavy object alone. Connect the output of
the integrator to the Product block as we did before (using either of the two inputs, it does
not matter which one), and then grab the second input to the Product block and drag it until
it touches the line that you just connected (the line from the integrator). This will cause
the two inputs to the Product block to be the same thing (the velocity), so the output is the
square of the velocity. Notice how easy it is to create a nonlinear differential equation in
Simulink. The next step is to multiply the velocity squared by 0.005πρ or 0.003825. Use
the Gain block from the Math library to do this. Click and drag a copy of this block into
the model then double click on this Gain block and change the value in the dialog to be
0.003825.
Figure 1.6 shows the resulting Simulink model, which is in the NCS library. Open it
with the MATLAB command:
Leaningtower2
1.2. Example 1: Galileo Drops Two Objects from the Leaning Tower of Pisa 13
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Figure 1.6. Leaning Tower Simulink model with air drag added.
Figure 1.7. Scope dialog showing how to change the number of axes in the Scope plot.
Note that we changed the Scope block to view both the position and the velocity of
the objects. This was done by double clicking the Scope icon and then selecting the “Scope
Parameters” dialog by double clicking the second menu item at the top of the Scope window.
(This is the icon to the right of the printer icon; it is the icon for a tabbed dialog.) The dialog
that opens looks like Figure 1.7.
Note that the number of axes in the Scope is the result of changing the value in the
“Number of Axes” box. We have made it two, which causes the Scope block to have two
inputs. We then connected the first input to the Velocity and the second to the Position lines
in the model.
14 Chapter 1. Introduction to Simulink
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Velocity
0
-20
-40
-60
-80
0 0.5 1 1.5 2 2.5 3 3.5
Position
150
100
50
-50
0 0.5 1 1.5 2 2.5 3 3.5
Time
Figure 1.8. Leaning Tower simulation results when air drag is included.
If you run this model, the results in the Scope block will resemble the graphs in
Figure 1.8.
Notice that the air drag causes the object to hit the ground later than we determined
when there was no drag (3.35085405691268 seconds instead of 3.05233847833684, about
0.3 seconds later).
Now let us compare the light and heavy object. We could do this by going back to the
model and changing the cross-sectional area to that of the smaller object. However, there
is a feature of Simulink that makes this type of calculation extremely easy. The feature is
the automatic “vectorization” of the model.
To exploit this feature, open the Gain block in the model (by double clicking) and
enter both the values for the large and the small object. Do this using MATLAB’s vector
notation. Thus the Gain block dialog will have the values [0.003825 0.000945]. (Include
the square brackets, which make the value in the dialog equal to the MATLAB vector, as
shown in Figure 1.9.)
After running this model with the 2-vector for the drag coefficients, the Scope shows
plots for the position and velocity of both of the objects (see Figure 1.10).
The heavier object hits the ground well after the light object. The velocity curve in the
top graph shows why. The speed of the heavier object comes to a maximum value of about
1.2. Example 1: Galileo Drops Two Objects from the Leaning Tower of Pisa 15
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Figure 1.9. Adding the second object to the Leaning Tower simulation by vector-
izing the air drag.
70 ft/sec, whereas the lighter object ends up at about 90 ft/sec. This difference is from the
increased air drag force on the object because the heavier object has a larger cross-sectional
area.
This difference is too large for Galileo not to have noticed, so it does lead us to suspect
that he never really did the purported experiment. (It is not clear from Galileo’s writings
that he understood the effect of drag, but if he did, he could have avoided this by making
the objects the same size.) In fact, it is very probable that Galileo relied on the thought
experiment he talks about in his 1638 book on the Copernican theory, Two New Sciences
[4]. In this thought experiment he argued that in a vacuum, by tying the two objects together
with a short rope, if the heavier object were to hit the ground first, at some point the rope
would become taut and the light object would begin dragging the heavier. That implies
that the combined object would hit the ground a little later than would have been the case
had no rope been connecting them. This meant that the combined object (which is heavier
than either one) would hit the ground a little later than the single heavy object. However,
since the combined weight of the two objects is greater than the single heavy object, this
violates the premise that the heavier object must hit the ground first. This deduction was
the reason Galileo believed that the Aristotelian statement that the heavy object falls faster
was wrong, and that prompted him to perform many experiments with inclined planes to
try to understand the dynamics of falling bodies [14].
Since this is the first Simulink model with any substance that we have built, let us use
it to illustrate some other features of Simulink.
First, let us clean up the model and annotate it. It is a good idea to do this as you
create the model so that when you return to the model later (or give the model to someone
else to use), it is clear what is simulated. The first thing to do is to assign a name to the
16 Chapter 1. Introduction to Simulink
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Velocity
0
-50
-100
0 0.5 1 1.5 2 2.5 3 3.5
Position
150
100
50
-50
0 0.5 1 1.5 2 2.5 3 3.5
Time
Figure 1.10. Simulating results. The Leaning Tower Simulink model with a heavy
and light object.
variables in the diagram, and the second is to name all of the blocks so their functions are
clear. It is also helpful to add color to the diagram to indicate the generic function of the
blocks.
To add a variable name onto a line in the Simulink model, simply double click on the
line. This causes an insertion point to appear on the line where you can type any text. The
annotation is arbitrary.
To annotate the model you created, enter the names in the model, or if you have not
created a model, open the model Leaningtower3 by typing Leaningtower3 at the MATLAB
command line. This will open the model with the annotation shown in Figure 1.11.
Simulink automatically changed from a scalar to a vector in the model when we made
the gain a MATLAB vector. After making the change, to see that the variables in the model
are vectors corresponding to the two different drag accelerations you must right click the
mouse anywhere in the model window and select Format from the menu; this will open the
submenu shown as part of Figure 1.11.
In this menu, we selected “Wide Nonscalar Lines” and “Signal Dimensions” (note
that they are checked). After you do this, all of the vector signals in the diagram have a
wider line, and the signal dimensions (vectors of size 2 here) are on the signal lines in the
model (as shown in Figure 1.11). To change the color of a block, highlight the block and
1.3. Example 2: Modeling a Pendulum and the Escapement of a Clock 17
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Figure 1.11. Right clicking anywhere in the diagram opens a pull-down menu
that allows changes to the colors of the blocks, bold vector line styles, and other model
annotations.
right click. Select “Background Color” from the resulting pop-up menu and then move the
mouse pointer to the right to select the desired color.
There are other features that Simulink supports which you can discover on your own
by playing with the model. As we work our way through the exercises and examples in this
book, you will learn a lot more.
In the next section, we continue creating some simple models by simulating a pendu-
lum clock. For the interested reader, [30] has a very nice discussion of how Galileo came
to realize that objects with different masses had the same motions. The Leaning Tower
experiment that showed that the masses would actually move the same had to wait for the
invention of the vacuum pump at the end of the 17th century.
• provide energy to the pendulum so the clock does not stop because of friction;
• regulate the speed of the gear motions to the natural period of the pendulum so that
the timing of the clock is accurate, depending only on the pendulum motion;
• provide the feedback to ensure that the force from the driver (springs or weights) and
wear in the various mechanical parts do not adversely affect the time displayed by
the clock.
2 This copyright figure is from a document titled “How a Clock Works” available from Erwin Sattler Clocks of
America. You can download the document from the web page http://www.sattlerclocks.com/ck.php. I would like
to thank Marcus Orvando, president of Sattler Clocks of America, for permission to use this diagram.
1.3. Example 2: Modeling a Pendulum and the Escapement of a Clock 19
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Before we go into the details of the design of the escapement, let us look at how it
works. The escapement is on a shaft attached to the clock case through a bushing called a
back cock. The pendulum mount does not connect to this shaft. The pendulum suspension
allows the pendulum to swing with minimal friction unencumbered by the escapement. As
the pendulum swings, it alternately engages the left and right side of the escapement anchor
(1). In the process a force from the escapement wheel (2) goes to the pendulum through the
crutch (4) and the crutch pin (5). The crutch assembly also forces the escapement to rotate
in synchronism with the pendulum. The pendulum itself (3) moves back and forth with a
period that is very closely determined by its length. (Once again we return to Galileo, who
demonstrated this in 1582.) The pendulum includes a nut at its bottom (not shown), whose
adjustment changes the effective length and thereby adjusts the period of the pendulum.
The escapement wheel (2) has a link to all of the gears in the gear train (they are not all
shown in Figure 1.12) that turns the hands (8) of the clock.
For the remainder of this description, we focus on the escapement anchor (1) and its
two sides. On the two sides of the anchor are triangular teeth; as the anchor rotates, one
of the two sides of each tooth alternately enters and exits the escapement gear, hence the
names entrance and exit pallets. If you look carefully at the anchor in Figure 1.12, you
20 Chapter 1. Introduction to Simulink
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
will see them: they are triangular pins that pass through each end of the anchor. As the
pendulum moves to the left, it forces the anchor to rotate and ultimately disengage the right
exit pallet face from the escapement wheel (2). When it does, the exit pallet on the opposite
side of the anchor engages the next tooth of the escapement wheel (9), and the escapement
gear moves one tooth. If the pendulum is set to swing at a period of 1 sec (as is the case for
the mechanism in Figure 1.12), the result of the motion of the escapement gear is to move
the second hand on the clock by one second (which implies that there are 60 teeth on the
escapement gear).
When the right face of the anchor engages the gear, the anchor and the escapement
wheel interact. The escapement wheel, which is torqued by the drive weight (7), applies a
force back through the anchor (1) and then, through the crutch and crutch pin (4 and 5), to the
pendulum. The alternate release and reengagement of the anchor and escape wheel provides
the energy to the pendulum that compensates for the energy lost to friction. As a by-product
of this interplay of the pendulum and the escapement wheel, we hear the distinctive and
comforting “tick-tock” sound that a clock makes.
d 2 θ (t) dθ (t)
J = −c − mgl sin(θ (t)).
dt 2 dt
Substituting ml 2 for J gives
d 2 θ (t) c dθ (t) g
=− 2 − sin(θ (t)).
dt 2 ml dt l
These dynamics are in the pendulum clock Simulink model shown in Figure 1.13.
This model uses Simulink primitive blocks for integrators, summing junctions, gains, and
1.3. Example 2: Modeling a Pendulum and the Escapement of a Clock 21
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Pend. Angle
Damping Accel.
dampcoef
Display
c/(m*len^2)
Variables
Thet2dot 1 Thetadot
Pendulum AngleForce 0.1148 1
Force Escapement Accel. s
xo s
Escapement Force from escapment
Model Initial
over the pendulum mass Pend. 0.2
Angle
Units of
2
dampcoef = 0.01 acceleration m/s
g = 9.8
plength = 0.2485
1/plength g*sin(u)
Gravity Accel. Pend. Angle
Gravity Force
1/length
g*sin(Theta)
the sine function (from the Continuous and the Math Operations libraries). We also used a
new concept, called a subsystem, to create a block called “Escapement Model.” Inside this
block are Simulink primitives that model the escapement. Open the subsystem by double
clicking, or view it using the model browser.
Let us follow the signal flow in the model. We start at the output of the summing junc-
tion, where the angular acceleration of the pendulum, Theta2dot, is the result of subtracting
the three terms:
The Simulink model creates the variables representing the angular position and ve-
locity, Theta and Thetadot, by integrating the angular acceleration Theta2dot. A pendulum
clock only sustains its oscillation if the pendulum starts at an angle that causes the escape-
ment to operate. Consequently, an initial condition is used in the last integrator (we use 0.2
radians, about 11.5 degrees) to start the clock.
A feature of Simulink is the ability to create a subsystem to simplify the model.
Typically, a subsystem is a Simulink model that groups the equations for a particular common
function into a single block. The details of the block are in the subsystem and are not visible
until the subsystem block is opened (by double clicking or browsing), but its functionality
is captured so that the user can see the interactions. The clock’s single subsystem is the
model for the escapement. This “Escapement Model” block has the equations shown in
Figure 1.14. These nonlinear escapement mechanism equations consist of the logic needed
to model the force created by the escapement.
22 Chapter 1. Introduction to Simulink
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Restore
Sign
1 |u|
<=
Pendulum
Abs
Angle Relational
Penhigh 0.15 Operator1
1
Force
>= Product1
Relational
Penlow 0.1 Operator2
du/dt
<
Derivative Relational
Operator3
0 penlow1
Creates a force that is applied when the pendulum is at the end of its swing
The escapement works in the same way as you would push a swing. A small accurately
timed push occurs when the exit pallet leaves the escapement. The push is always in
the direction of the pendulum’s motion and it occurs just after the pendulum reaches its
maximum or minimum swing (depending on the sign of the angle).
The attributes of the escapement model are as follows:
• Since the pendulum motion is symmetric, the calculations use positive angles (with
the absolute value function block). The force applied has the correct sign because
we multiply it by +1 or −1, depending on the sign of the angle. (The block called
“Restore Sign” does this.)
• The angle of the pendulum at which the escapement disengages (jumps from one gear
tooth to the next) is denoted by Penhigh (with a value of 0.15 radians).
• The escapement applies the force only when the pendulum is accelerating (i.e., when
the pendulum is moving toward the center of its swing). We check to see if the
derivative of the pendulum angle is positive or not, and multiply the force by 1 when
it is and by 0 when it is not in the “Relational Operator 3” block.
1.3. Example 2: Modeling a Pendulum and the Escapement of a Clock 23
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Figure 1.15. Results of the clock simulation for times around 416 sec. Upper figure
is the pendulum angle, and the lower figure is the acceleration applied to the pendulum by
the escapement.
To get the relationship between the angular acceleration and the particle’s speed we
differentiate the vector velocity using the fact that the radius is constant and the angular
velocity and unit vector perpendicular to the radial motion are changing with time. Thus
the acceleration of the particle is
dv(t) dω duθ
a(t) = = uθ r+ ωr.
dt dt dt
Following steps similar to those we used above for differentiating ur , the derivative of uθ
is − ur ω, where the minus sign comes from the fact that the difference in uθ at the times t
and t+ t points inward (along the radial direction this is negative). Using these values, we
get the acceleration of the particle as (in this expression, α = dω
dt
)
a(t) = uθ αr − ur ω2 r.
This acceleration has two components: tangential and radial. The tangential component
is the instantaneous linear acceleration and its magnitude is αr. The radial component is
the centrifugal acceleration and its magnitude is ω2 r = vr . These two results should be
2
familiar from introductory physics, but we show them here to remind you that motions under
rotations need to account for both the vector magnitude and its directions.
Ω
z
acceleration, the “Coriolis force” that accounts for the perceived motion in the rotating
frame is
dω
FCoriolis = −2mω × vRotating − mω × (ω × r) − m × r.
dt
We can now model the Foucault pendulum. Figure 1.17 shows the coordinate system
we use. The y coordinate comes out of the paper. The origin of these coordinates is the
rest coordinates of the pendulum, and the z axis goes through the point of suspension of the
pendulum. We assume that the pendulum length is L.
In the figure, the Earth is rotating at the angular velocity $, the pendulum
is at the
latitude λ, and we denote the coordinates of the pendulum with the vector x, y, z .
Since the pendulum is rotating at the Earth’s rate, the Coriolis forces on the pendulum are
dx dy dz
C = −2m(v × $) = −2m dt
, dt
, dt
× −$ cos λ, 0, $ sin λ .
In the vertical direction, the tension in the wire holding the pendulum is approximately
constant (equal to the weight of the pendulum bob mg), so the velocity along the vertical is
essentially zero. Therefore, the Coriolis forces (from the cross product) are
dy dy
2m dt
$ sin λ, − dx
dt
$ sin λ, dt
$ cos λ .
Now, when the pendulum moves, there is a restoring force from gravity. This force is
proportional to mg sin(θ ), where θ is the pendulum angle, as we saw for the pendulum
above. For the pendulum, the angle is Lx along the x-axis and Ly along the y-axis. We assume
that the pendulum motion is small so the acceleration is mgθ . Thus the accelerations on the
28 Chapter 1. Introduction to Simulink
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Pendulm Freq
Squared
y Axis (E-W)
1 dy/dt 1 Motion
2*OmegaE*sin(Lat)
Coriolis s s
Accel (y) x Axis (N-S)
Integrate y Integrate y
Accel. Vel. Pendulum Freq Motion Scope
Squared
2*OmegaE*sin(Lat) OmegaP^2
dy/dt Coriolis Pend.
Accel. (x) Accel.
Earth Rate
at Latitude
1 Integrate x
s Accel.
dx/dt dx/dt 1
s
Integrate x
Vel.
y Axis (E-W)
Motion
1
Motion of Pendulum in y (DFeet)
0.5
-0.5
-1
0 2 4 6 8 10 12 14
Time 4
x 10
x Axis (N-S)
Motion
1
Motion of Pendulum in x (Feet)
0.5
-0.5
-1
0 2 4 6 8 10 12 14
Time 4
x 10
Figure 1.19. Simulation results from the Foucault pendulum Simulink model using
the default solver.
30 Chapter 1. Introduction to Simulink
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
y Axis (E-W)
Motion
1
0.5
-0.5
-1
0 2 4 6 8 10 12 14
4
x 10
x Axis (N-S)
Motion
1
0.5
-0.5
-1
0 2 4 6 8 10 12 14
Time 4
x 10
Figure 1.20. Simulation results for the Foucault pendulum using a tighter toler-
ance for the solver.
the east-west line. If for some reason the pendulum starts with a motion that is out of the
plane, the pendulum will precess during its motion sweeping out a more elliptic path.
The parameters in the model come from code used by the pre-load function (in the
“Model Properties” dialog under “Edit” in the pull-down menu at the top of the Simulink
model). You can experiment with different latitudes for the pendulum by changing the value
of Lat (by simply typing the new value of Lat at the MATLAB command line).
Let us use this result to explore the numerical integration tools in Simulink. Chap-
ter 7 of Cleve Moler’s Numerical Computing with MATLAB contains a discussion of the
differential equation solvers in MATLAB. The solvers in Simulink are identical, except that
they are linked libraries that Simulink automatically uses when the model runs (because
you clicked the “run” button). The results in Figure 1.19 use the default solver in Simulink
(the default is ode45 with a relative tolerance of 1e-3). If you look carefully at this figure,
you will notice that the amplitude of the pendulum motion is decreasing over time. There
is, however, no physical reason for this. We have not included any pendulum damping in
the model, and the terms that couple the x and y motions should not introduce damping.
To see if this effect is a consequence of the numerical solver, let us go into the model and
change the solver to make it more accurate. Open the “Configuration Parameters” dialog
under the Simulation pull-down menu (or use Ctrl + E), and in the dialog change the relative
tolerance to 1e-5. If you run the simulation, the result appears as shown in Figure 1.20.
1.5. Further Reading 31
Downloaded 09/09/15 to 130.237.29.138. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Notice that this result is what we expect; there is no discernable damping in the
motion and the period is exactly what we predicted. The need for the tighter tolerance in
the calculation is because of the order of magnitude difference of five in the coefficients in
the model, as we noted above. In general, experimentation is required with the differential
equation solver values in order to insure that the solution is correct. The best way to do this
is to try different solvers and different tolerances to see if they create discernable differences
in the solutions. We return to the solvers as we look at other simulations. In particular, we
will look at some very stiff systems and the use of stiff solvers in Chapter 3.
In Chapter 3, we will also explore the more complex motions of objects undergoing
gyroscopic forces when the motion takes place in three dimensions. First, however, we will
look at how Simulink creates simulation models for linear systems, how Simulink handles
vectors and matrices, and, last, when to use Simulink as part of the process of designing
control systems.
the model can also overwhelm the computer’s memory. Managing the use of memory can
improve the performance of your models. Reference [41] has a good discussion of the
methods available for managing the memory in a Simulink model.
Exercises
1.1 Use the Leaning Tower of Pisa model as the starting point to add a horizontal com-
ponent to the velocity at the instant the objects start. Include the effect of air drag.
Also, add an input to the model to simulate different wind speeds. Experiment with
this model to see how these do or do not affect the experiment.
1.2 In the clock simulation, increase the friction force to see how large it must be before
the escapement ceases working. Perform some numerical experiments with the model
to see if there is any effect on the accuracy because of the increased friction. When
you evaluated the change in the period and amplitude of swing of the pendulum as
you changed the Gain, you should not have seen a significant change. What change in
the model will allow you to see the effect of lower forces applied by the escapement?
Modify the model to look at this, and experiment with different escapement forces.
What is the effect? Why is the effect so small? How does the escapement ameliorate
these forces? Look at Figure 1.12 carefully. The Sattler Company has a spring
mounted on the pendulum that transmits the force from the crutch pin to the pendulum.
Does the Simulink model have this mechanism? How would you go about adding
this mechanism to the model? Is there a good reason for the Sattler clock to use this
spring?
1.3 Experiment with the different Simulink solvers (one by one) to see what effect they
have on the simulation of the Foucault pendulum. Which ones work well? Try
changing the tolerances used for each of the solvers to see if they stop working.
1.4 Create a Simulink model of a mass suspended on a spring under the influence of
gravity. Add a force that is proportional to the velocity of the mass to model the
damping. Pick some parameter values for the mass, the spring constant, the damping,
and, of course, gravity, and simulate the system. Explore what happens when you
increase the value of the damping from zero. Can you explain the behavior you are
seeing in terms of the solution of the underlying differential equation? (If you cannot,
we will see why in the next chapter.)