




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Contents
Fundamentals........................................... 1 Making Plots........................................... 2 Printing and Saving Graphs................................... 5 Loading Data Files........................................ 5 Title and Lab els......................................... 6 Other Interesting Features of Matlab Plotting......................... 6
Matlab is a program for solving engineering and mathematical problems. The basic Matlab ob jects are vectors and matrices, so you must b e familiar with these b efore making extensive use of this program.
To start Matlab typ e matlab; to quit, typ e quit or exit.
Matlab works with essentially one kind of ob ject, a rectangular numerical matrix. Vectors and scalars are referred to as n-by-1 and 1-by-1 matrices resp ectively. Here is some basic information on using Matlab matrix commands.
Entering Matrices
The matrix
A =
can b e entered into Matlab by typing the following three lines. Each line ends by pressing the Return key.
A = [1 3 2 2 4 1 6 6 8]
Generating Vectors With Even Space
To plot a function, you must rst sp ecify the data p oints at which the function will b e evaluated. It is common to cho ose evenly spaced p oints and put then in a vector. Here is how you generate a row vector X containing the values from 0 to 10 in increments of 0.2.
X = 0 : 0.2 : 10
Array Op erations
This term is used to refer to element-by-element arithmetic op erations on vectors, instead of the usual linear algebra op erations denoted by the symb ols *, /, or ^ (exp onentiation). Preceding an op erator with a p erio d. indicates an array or element-by-element op eration. For example, if X = [1 2 3] and Y = [4 5 6]; then
X : Y = [4 10 18].
Notice that the usual vector pro duct X Y is unde ned. The Matlab ob ject ones(m,n) is useful if you want to add or subtract a constant from each element in a vector. ones(m,n) is an m-by-n matrix of ones. Using the vector X from the last example, you write the expression X + 2 as follows in Matlab notation.
X + 2 * ones(1,3)
The dimension of ones vector must match the other vectors in the computation. The command size(A) returns the dimension of a vector or matrix A.
On-line Help
Matlab has on-line help for all its commands. For example, try any of these commands:
help print help help help general
Matlab provides a variety of functions for displaying data as 2-D or 3-D graphics.
For 2-D graphics, the basic command is:
plot(x1, y1, 'line style', x2, y2, 'line style'...)
50
100
150
200
250
30
210
60
240
90
270
120
300
150
330
180 0
Example 2
plot(x, sin(x), '-') hold plot(x, cos(x), '+')
The next example shows how Matlab generates a spiral using the p olar co ordinate system.
Example 2: Plot = 2 with 0 5 in p olar co ordinates.
theta = 0: 0.2: 5pi; rho = theta.^2; polar(theta, rho, '')
The following example illustrates how to generate a mesh surface in Matlab.
Example 3: Plot z = sin(r )=r with r =
x^2 + y 2 , 8 x 8, 8 y 8.
The rst step in displaying a function of two variables, z = f (x; y ), is to use the meshgrid function to generate X and Y matrices consisting of rep eated rows and columns, resp ectively, over the domain of the function. The function can then b e evaluated and graphed.
x = -8: .5: 8; y = x; [X,Y] = meshgrid(x,y); R = sqrt(X.^2 + Y.^2) + eps; % add eps to prevent R= Z = sin(R)./R; mesh(x, y, Z) % or mesh(X,Y,Z)
Anything following % on a line is treated as a comment. We added eps (the machine ) to R to prevent over ow.
5 10
0
5
-0.4 10
-0.
0
1
Example 3
There are two ways to print your plots. The rst one sends a copy of your graph directly to the default printer in the Thomson Hall lab. The second lets you save your graph in a le so you can use Unix printing commands to direct it to the printer of your choice.
Typ e print in the Matlab environment to send your current plot to the pre-de ned printer. On Math Sciences Matlab , the default printer is a laser printer in Thomson Hall. The print command generates a full page plot.
If you want to save graphs in a le, use another printer, change the plot orientation, or use other features of the print command, lo ok at the on-line help text within Matlab. For example, to save your graph in a PostScript le, use the command:
print -dps name-of-file
Matlab reads in values from ASCI I les using the load command. Once the data le has b een read in, you can use any of the Matlab graphing commands. Here are some of the things you need to consider when reading in data.
The name of the ASCI I data le must have two parts, separated by a p erio d. The command
load filename.extension
reads the le lename.extension, which can b e an ASCI I le with a rectangular array of numeric data, arranged in m lines with n values in each line. The result is an m-by-n matrix with the same name as the le with the extension (including the p erio d) stripp ed.
Here are some examples:
should read the Matlab User's Guide (or some other commercial do cumentation) for more information.
Multiple Plots
The command subplot(m,n,p) breaks the graph (or gure) window into an m-by-n matrix of small rectangular panes. The value of p is the pane for the next plot. Panes are numb ered from left to right, top to b ottom. To return to the default single graph p er window, use either subplot(1,1,1) or clf. You can have more than one graphics window on an X display. The Matlab command, gure op ens a new window, numb ering each new window. You can then use commands such as clf, gure(h), or close to manipulate the gure windows.
Viewp oint
You can set the angle of view of a 3-D plot with the command:
view(az,el)
az is the azimuth and el is the elevation of the viewp oint, b oth in degrees. See the viewpoint gure for an illustration of azimuth and elevation relative to the Cartesian co ordiate system. Z
Y
0 X
Azimuth
Viewpoint
=-37.
Elevation =
Default Viewp oint
Example 5: View the internal Matlab p eaks matrix from 4 di erent viewp oints. The rst one, (view(-37.5,30), is the default viewp oint.
subplot(2,2,1); mesh(peaks(20)); view(-37.5,30) subplot(2,2,2); mesh(peaks(20)); view(-7,80) subplot(2,2,3); mesh(peaks(20)); view(-90,0) subplot(2,2,4); mesh(peaks(20)); view(-7,-10)
0 10
20 0
10
-10 20
0
10
00 5 10 15 20
5
10
15
20 -
(^100)
-10 20 15 10 5 0
0
5
10
(^0 5 10 15 ) 100 -10 20
0
5
10
Example 5
Controlling Axes
You can control the scaling and app earance of plot axis with the axis function. To set scaling for the x- and y- axes on the current 2-D plot, use this command:
axis([xmin xmax ymin ymax])
To scale the axes on 3-D plot, use this:
axis([xmin xmax ymin ymax zmin zmax])
In addition, axis('auto') returns the axis scaling to its default where the b est axis limits are computed automatically; axis('square') makes the current axis b ox square in size, otherwise a circle will lo ok like an oval; axis('o ') turns o the axes axis('on') turns on axis lab eling and tic marks.