Geoscience Reference
In-Depth Information
We now type
global m n
in the Command Window. At er running the function as described in the
previous example we i nd the two variables
m
and
n
in the workspace. We
have therefore transferred the variables
m
and
n
between the function
average
and the workspace.
2.9 Basic Visualization Tools
MATLAB provides numerous routines for displaying data as graphics. h is
section introduces the most important graphics functions. h e graphics can
be modii ed, printed and exported to be edited with graphics sot ware other
than MATLAB. h e simplest function producing a graph of a variable
y
versus another variable
x
is
plot
. First, we dei ne two one-dimensional arrays
x
and
y
, where
y
is the sine of
x
. h e array
x
contains values between 0 and 2ˀ
with ˀ/10 increments, whereas
y
is the element-by-element sine of
x
.
clear
x = 0 : pi/10 : 2*pi;
y = sin(x);
h ese two commands result in two one-dimensional arrays with 21 elements
each, i.e., two 1-by-21 arrays. Since the two arrays
x
and
y
have the same
length, we can use
plot
to produce a linear 2D graph of
y
against
x
.
plot(x,y)
h is command opens a
Figure Window
named
Figure 1
with a gray
background, an
x
-axis ranging from 0 to 7, a
y
-axis ranging from -1 to +1
and a blue line. We may wish to plot two dif erent curves in a single plot, for
example the sine and the cosine of
x
in dif erent colors. h e command
x = 0 : pi/10 : 2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x,y1,'--',x,y2,'-')
creates a dashed blue line displaying the sine of
x
and a solid red line
representing the cosine of this array (Fig. 2.4). If we create another plot, the
window
Figure 1
will be cleared and a new graph displayed. h e command
figure
, however, can be used to create a new i gure object in a new window.