Graphics Programs Reference

In-Depth Information

1.5 The Colon Operator

To generate a vector of equally-spaced elements matlab provides the

colon operator. Try the following commands:

1:5

0:2:10

0:.1:2*pi

The syntax
x
:
y
means roughly “generate the ordered set of numbers

from
x
to
y
with increment 1 between them.” The syntax
x
:
d
:
y
means

roughly “generate the ordered set of numbers from
x
to
y
with increment

d
between them.”

1.6 Linspace

To generate a vector of evenly spaced points between two end points,

you can use the function
linspace(
start
,
stop
,
npoints
)
:

>> x = linspace(0,1,10)

x=

Columns 1 through 7

0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667

Columns 8 through 10

0.7778 0.8889 1.0000

generates 10 evenly spaced points from 0 to 1. Typing
linspace(
start
,

stop
)
will generate a vector of 100 points.

1.7 Plotting Vectors

Whereas other computer languages, such as Fortran, work on numbers

one at a time, an advantage of matlab is that it handles the matrix as

a single unit. Let us consider an example that shows why this is useful.

Imagine you want to plot the function
y
= sin
x
for
x
between 0 and 2
π
.

A Fortran code to do this might look like this:

DIMENSION X(100),Y(100)

PI = 4*ATAN(1)

DO 100 I = 1,100

X(I) = 2*PI*I/100

Y(I) = SIN(X(I))

100 CONTINUE

PLOT(X,Y)

Here we assume that we have access to a Fortran plotting package

in which
PLOT(X,Y)
makes sense. In matlab we can get our plot by

typing: