File

advertisement
Programming For Nuclear Engineers
Lecture 13
MATLAB (4)
1
1- M-Books
2- Enabling M-Books
3- Starting M-Books
4- Working with M-Books
5- SIMULINK
2
1- M-Books
The M-book interface allows the user to operate MATLAB from a special
Microsoft Word document instead of from a MATLAB Command Window.
In this mode, the user should think of Word as running in the foreground and
MATLAB as running in the background. Lines that you enter into your Word
document are passed to the MATLAB engine in the background and executed
there, whereupon the output is returned to Word (through the intermediary of
Visual Basic), and then both input and output are automatically formatted.
One obtains a living document in the sense that one can edit the document as
one normally edits a word processing document. So one can revisit input lines
that need adjustment, change them, and reexecute on the spot — after which
the old outdated output is automatically overwritten with new output.
The graphical output that results from MATLAB graphics commands appear in
the Word document, immediately after the commands that generated them.
3
2- Enabling M-Books
To run the M-book interface you must have Microsoft Word on your computer.
It is possible to run the interface with earlier versions of Word, but it works best if
you have Word 97.(it runs better in Word 97 than it does in Word 2000, though the
difference is not usually significant.)
After installation MATLAB; type notebook –setup from the Command
Window, this step will give you:
>> notebook -setup
Welcome to the utility for setting up the MATLAB Notebook
for interfacing MATLAB to Microsoft Word
Setup complete
4
3- Starting M-Books
To start up the M-book interface type notebook at the Command Window
prompt.
After you type notebook, you will see Microsoft Word launch and a blank Word
document will fill your screen. We will refer to this document as an M-book.
The difference between a blank M-book and a normal Word document is only
apparent if you peruse the menu bar. There you will see an entry that is not
present in a normal Word document — namely, the Notebook menu.
You can now type into the M-book in the usual way. In fact you could prepare
a document in this screen in precisely the same manner that you would in a normal
Word screen. The background features of MATLAB are only activated if you do
one of two things: either access the items in the Notebook menu or press the key
combination CTRL+ENTER.
5
Type into your M-book the line 23/45 and press CTRL+ENTER. After a short
delay you will see what you entered change font to bold New Courier, encased in
brackets, and then the output
ans =
0.5111
will appear below, also in New Courier font (but not bold).
It is also likely that the input and output will be colored (the input in green, the
output in blue).
Your cursor should be on the line following the output, but if it is at the end
of the output line, move it down a line and type:
solve(’xˆ2 - 5*x + 5 =0’) followed by CTRL+ENTER. After some
thought MATLAB feeds the answer to the M-book:
ans=
[5/2+1/2*5^(1/2)]
[5/2-1/2*5^(1/2)]
6
Finally, try typing ezplot(’xˆ3 - x’), then CTRL+ENTER, and watch the
graph appear.
At this point your M-book should look like the figure below
7
4- Working with M-Books
You interact with data in your M-book in two ways — via the keyboard or through
the menu bar.
It is important to understand that your M-book can be handled in exactly the same
way that you would any Word document.
In particular, you can save the file, print the document, change fonts or margins,
move or export a graphic, etc. This has the advantage of allowing you to present
the results of your MATLAB session in an attractively formatted style.
It also has the disadvantage of affording the user the opportunity to muck with
MATLAB’s input or output and so to create input and output that may not truly
correspond to each other. One must be very careful!
Note that the help item on the menu bar is Word help, not MATLAB help. If
you want to invoke MATLAB help, then either type help (with CTRL+ENTER)
or bring the MATLAB Command Window to the foreground and use MATLAB
help in the usual fashion.
8
5- SIMULINK
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 Windows Systems
in the right below figure
9
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, by clicking on the
to the left of the
“Continuous” icon or by clicking on the small icon to the left of the word
“Continuous” in the left panel of the SIMULINK Library Browser.
10
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.
11
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:
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 multiplication by a
constant, and one Sum block, used for addition.
These are all chosen from the “Math” library. Hooking them up the same way we did
with the Integrator blocks gives a model window that looks something like this:
12
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.
13
Position the mouse in the middle of the arrow connecting the two Integrators, hold
down the CTRL key with one hand, simultaneously push down 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 next:
14
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.
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 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:
15
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:
1- It is harder to put in the initial conditions,
2- and also numerical differentiation is much less stable than numerical integration.
Now go to the Simulation menu and hit Start.
You should see in the Scope window something like the figure next.
This of course is simply the graph of the function 3e-t sin(2t) + e-t cos(2t).
16
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.
What will he do? (HW)
17
Download