Tải bản đầy đủ (.pdf) (15 trang)

SIMULINK and GUIs

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (413.56 KB, 15 trang )

Chapter 8
SIMULINK and GUIs
In this chapter we describe SIMULINK, a MATLAB accessory for simulat-
ing dynamical processes, and GUIDE, a built-in tool for creating your own
graphical user interfaces. These brief introductions are not comprehensive,
but together with the online documentation they should be enough to get you
started.
SIMULINK
If you want to learn about SIMULINK in depth, you can read the massive PDF
document SIMULINK: Dynamic System Simulation for MATLAB that comes
with the software. Here we give a brief introduction for the casual user who
wants to get going withSIMULINK quickly. You start SIMULINK by double-
clicking on SIMULINK in the Launch Pad, by clicking on the SIMULINK
button on the MATLAB Desktop tool bar, or simply by typing simulink in
the Command Window. This opens the SIMULINK library window, which is
shown for UNIX systems in Figure 8-1. On Windows systems, you see instead
the SIMULINK Library Browser, shown in Figure 8-2.
To begin to use SIMULINK, click New : Model from the File menu. This
opens a blank model window. You create a SIMULINK model by copying units,
called blocks, from the various SIMULINK libraries into the model window.
We will explain how to use this procedure to model the homogeneous linear
ordinary differential equation u

+ 2u

+ 5u = 0, which represents a damped
harmonic oscillator.
First we have to figure out how to represent the equation in a way that
SIMULINK can understand. One way to do this is as follows. Since the time
variable is continuous, we start by opening the “Continuous” library, in UNIX
121


122
Chapter 8: SIMULINK and GUIs
Figure 8-1: The SIMULINK Library in UNIX.
Figure 8-2: The SIMULINK Library Browser.
SIMULINK
123
Figure 8-3: The Continuous Library.
by double-clicking on the third icon from the left in Figure 8-1, or in Windows
either by clicking on the

to the left of the “Continuous” icon at the top
right of Figure 8-2, or else by clicking on the small icon to the left of the
word “Continuous” in the left panel of the SIMULINK Library Browser. When
opened, the “Continuous” library looks like Figure 8-3.
Notice that u and u

are obtained from u

and u

(respectively) by integrating.
Therefore, drag two copies of the Integrator block into the model window, and
line them up with the mouse. Relabel them (by positioning the mouse at the
end of the text under the block, hitting the
BACKSPACE
key a few times to erase
what you don’t want, and typing something new in its place) to read u

and u.
Note that each Integrator block has an input port and an output port. Align

the output port of the u

Integrator with the input port of the u Integrator
and join them with an arrow, using the left button on the mouse. Your model
window should now look like this:
1
s
u’
1
s
u
This models the fact that u is obtained by integration from u

. Now the
differential equation can be rewritten u

=−(5u + 2u

), and u

is obtained
by integration from u

. So we want to add other blocks to implement these
relationships. For this purpose we add three Gain blocks, which implement
124
Chapter 8: SIMULINK and GUIs
multiplication by a constant, and one Sum block, used for addition. These
are all chosen from the “Math” library (fourth from the right in Figure 8-1, or
fourth from the top in Figure 8-2). Hooking them up the same way we did with

the Integrator blocks gives a model window that looks something like this:
1
s
u’
1
s
u
1
Gain2
1
Gain1
1
Gain
We need to go back and edit the properties of the Gain blocks, to change
the constants by which they multiply from the default of 1 to 5 (in “Gain”),
−1 (in “Gain1”), and 2 (in “Gain2”). To do this, double-click on each Gain
block in turn. A Block Parameters box will open in which you can change the
Gain parameter to whatever you need. Next, we need to send u

, the output
of the first Integrator block, to the input port of block “Gain2”. This presents
a problem, since an Integrator block only has one output port and it’s already
connected to the next Integrator block. So we need to introduce a branch line.
Position the mouse in the middle of the arrow connecting the two Integrators,
hold down the
CTRL
key withone hand, simultaneously pushdown the left
mouse button with the other hand, and drag the mouse around to the input
port of the block entitled “Gain2”. At this point we’re almost done; we just
need a block for viewing the output. Open up the “Sinks” library and drag a

copy of the Scope block into the model window. Hook this up with a branch
line (again using the
CTRL
key) to the line connecting the second Integrator
and the Gain block. At this point you might want to relabel some more of the
blocks (by editing the text under each block), and also label some of the arrows
(by double-clicking on the arrow shaft to open a little box in which you can
type a label). We end up with the model shown in Figure 8-4.
Now we’re ready to run our simulation. First, it might be a good idea to save
the model, using Save as... from the File menu. One might choose to give it
the name li e OD . (MATLAB automatically adds the file extension .l.)
To see what is happening during the simulation, double-click on the Scope
block to open an “oscilloscope” that will plot u as a function of t. Of course
one needs to set initial conditions also; this can be done by double-clicking on
SIMULINK
125
Figure 8-4: A Finished SIMULINK Model.
the Integrator blocks and changing the line of the Block Parameters box that
reads “Initial condition”. For example, suppose we set the initial condition for
u

(in the first Integrator block) to 5 and the condition for u (in the second
Integrator block) to 1. In other words, we are solving the system





u


+ 2u

+ 5u = 0,
u(0) = 1,
u

(0) = 5,
which happens to have the exact solution
u(t) = 3e
−t
sin(2t) + e
−t
cos(2t).

Your first instinct might be to rely on the Derivative block, rather than the
Integrator block, in simulating differential equations. But this has two
drawbacks: It is harder to put in the initial conditions, and also numerical
differentiation is muchless stable than numerical integration.
Now go to the Simulation menu and hit Start. You should see in the Scope
window something like Figure 8-5. This of course is simply the graph of the
function 3e
−t
sin(2t) + e
−t
cos(2t). (By the way, you might need to change the
scale on the vertical axis of the Scope window. Clicking on the “binoculars” icon
does an “automatic” rescale, and right-clicking on the vertical axis opens an
Axes Properties... menu that enables you to manually select the minimum
and maximum values of the dependent variable.) It is easy to go back and
change some of the parameters and rerun the simulation again.

Finally, suppose one now wants to study the inhomogeneous equation for
“forced oscillations,” u

+ 2u

+ 5u = g(t), where g is a specified “forcing” term.
126
Chapter 8: SIMULINK and GUIs
Figure 8-5: Scope Output.
For this, all we have to do is add another block to the model from the “Sources”
library. Click on the shaft of the arrow at the top of the model going into the
first Integrator and use Cut from the Edit menu to remove it. Then drag in
another “Sum” block before the first Integrator and input a suitable source to
one input port of the “Sum” block. For example, if g(t) is to represent “noise,”
drag the Band-Limited White Noise block from the “Sources” library into the
model and hook everything up as shown in Figure 8-6.
The output from this revised model (with the default values of 0.1 for the
noise power and 0.1 for the noise sample time) looks like Figure 8-7. The effect
of noise on the system is clearly visible from the simulation.
Figure 8-6: Model for the Inhomogeneous Equation.

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×