Diary File: At times you will need to record the results
of your MATLAB session as part of an assignment.
Have a floppy disk or flash
drive ready to use so you can save your MATLAB work. Change the Current
Directory field in the desktop toolbar to a: or a particular directory on the
floppy or the drive letter as signed to
your flash drive.
Now type command diary filename.txt followed by the Enter key. Each computation
(but not graphics) you
make in MATLAB will be saved in your directory in a text
file named filename.txt. You can then edit this file
using your favorite text
editor. When you have finished your MATLAB session you can turn off the
recording
by typing diary off at the MATLAB prompt . If you want to stop your
MATLAB session for any reason, you
can reopen the diary file the next time you
start MATLAB. If you use the same file name, the results of your
new MATLAB
session will be written at the end of the old diary file. You may want to use
different names for
each session on an assignment, and then merge the files. For
more information use command help diary.
Some particular commands.
The colon operator
J:K is the same as [J, J+1, ..., K].
|
 |
J:D:K is the same as [J, J+D, ..., J+m*D] where m = fix((K-J)/D).

The colon notation can be used to pick out selected rows,
columns and elements of vectors, matrices, and
arrays. A(:) is all the elements
of matrix A, regarded as a single column.
A(:,J) is the J-th column of A. A(J:K) is [A(J),A(J+1),...,A(K)].
A(:,J:K) is [A(:,J),A(:,J+1),...,A(:,K)] and so on.
The ez plot command
The easiest way to use ezplot is to ex press a function is via a string:
Example ezplot('x^2 - 2*x + 1')
The default domain -2*pi < x < 2*pi.
To change the domain use
EZPLOT(FUN,[A,B]) which plots FUN(X) over A < X < B.
Example ezplot('x^2 - 2*x + 1', [-1, 4]) or ezplot('x^2 - 2*x + 1', -1, 4)
The vector operators and the vectorize command
The arithmetic operators *, /, and ^ work with arguments that are scalars.
MATLAB has
extensions of these that work with vectors, performing entry by entry
ope rations . These are
denoted .*, ./, and .^ respectively. (Note the periods.)

If you have a function expression like '3*t^3 + 5*t^2 + t
- 6' then to evaluate it at a set of
points between t = -3 and t = 4 like the
set -3:4 there is a looping process that is executed for
the values -3 -2 -1 0 1
2 3 4. We can make this much more efficient as follows:

Function & Plotting Practice:
1. Type the following commands in MATLAB. Move the mouse pointer onto the
plot. Notice the symbol is a cross -hair. Position the cross-hair to estimate the
6 local max-min points shown. Click the left mouse button to record a point.
Compare the values you obtain with those of other students in the class.
f='x*(x-1)*sin(x)'
ezplot(f)
[x,y]=ginput(6);[x y]
Record your estimates in the following table:

2. Enter the string g =sym( 'exp(sin(x))' ). Compute its
second derivative from the command g2 =
diff(g,2) . Graph the second derivative
over the interval [0.7,1.5]. Use command
ezplot(g2,[0.7,1.5]). Type [x y]=ginput(2); Then
using the mouse click on the curve to estimate the largest and smallest value
on the picture displayed. To show these points type [x y]
What is an upper bound on the absolute value of g2 ? __________________
3. Find the max of the absolute value of f(x) over [2,4] where
f(x) = 2xcos(2x) - (x-2)2. Explain your procedure. State pertinent MATLAB
commands.
4. Labeling graphs; changing axes; and other interesting things.
Enter the following commands.
If your graph is not visible, type command figure( gcf ) .
Note at the top of your graph you will see the following display:

Click on Insert then Text Box in the drop down menu; move
your mouse to white space on the
graph then click and draw a box. Now type your
name in the resulting box. Click the mouse outside
the box with your name. Now
use your mouse to drag your name inside the ellipse. Click outside the
box
containing your name to affix it at that position.
Experiment with other icons on the tool bar.
Now let’s superimpose another graph on this figure. Type the following commands.
hold on
|
“freezes” the current graph so it is not erased
when the next plot command is executed |
| |
|
t=-3:.01:3;
f=’t^2+2*t’;
yf=eval(vectorize(f));
plot(t,yf) |
|
5. In a future assignment we will be approximating the
solution of a system of differential equations .
There will be an independent
variable t and two dependent variables
(t) and
(t). A table will be
returned
from the computing algorithm which will be a 3 × n matrix. The first row will
contain values
of the independent variable t, the second row values of
and
the third row values of
. We will
want to plot t vs
and t vs
, and maybe even
vs
. This exercise illustrates techniques for
making such plots.
|
load lab1ex8data |
loading the data set; its name is data |
| |
|
|
data |
viewing the data set |
| |
|
|
plot(data(1,:),data(2,:),'-k') |
plot t vs
with black line segments |
| |
|
|
plot(data(1,:),data(3,:),'-r'), figure(gcf) |
plot t vs
with red line
segments;
previous plot is overlaid & new plot is shown |
| |
|
hold on
plot(data(1,:),data(2,:),'-k'), figure(gcf)
|
plot t vs
on the existing
t vs graph & figure displayed |
| |
|
|
figure |
create a new figure for the next plot |
| |
|
|
plot(data(2,:),data(3,:),'-b') |
plot vs
in blue |
| |
|
|
close all |
closes all existing figures |