Basic
Introduction to matlab and linear algebra
This
tutorial is meant to get you off the ground with Matlab and simultaneously
review some basic concepts from linear algebra which are essential for the
class. I recommend the tutorial at http://www.fi.uib.no/Fysisk/Teori/KURS/WRK/mat/singlemat.html
to get more information about matlab. You can also access the tutorial through
the course web site.
Matlab
is a software package that makes it easier for you to enter matrices and
vectors and manipulate them. The interface uses an interpreted language that is
designed to look a lot like the notation use in linear algebra. Almost all of
Matlab basic commands revolve around the use of vectors and matrices. Matlab
also provides control structures for writing programs, mathematical functions
for performing basic mathematical operations, plotting functions for
visualizing data and a host of utilities for performing complex operations,
like computing Fourier transforms.
The
tutorial is organized into 5 sections. Each section has exercies embedded in it
which you will be expected to do. These are bracketed by *****s. You should
also type in the example commands used in the tutorial. Exercises should be
saved in a scratch file (see below), with appropriate comments.
1.
Basic Matlab operations
2.
Vectors
3.
Matrices
4.
Some functions for creating and initializing matrices
5.
Plotting functions and data
1.
Basic operations
To
start Matlab, click on the Matlab icon. This will start up the software, and it
will wait for you to enter your commands in the command window.
In
the text that follows, any line that starts with two greater than signs
(>>) is used to denote the matlab command line. This is where you enter
your commands. To quit Matlab, simply type the command:
>>quit
The
basic operation in Matlab is the assignment operator
>>a
= 3
a
=
3
>>
You
can suppress output by following the line with a semi-colon
>>a
= 3;
>>
Note
that in the previous example, if you end the line with a semi-colon, the result
is not displayed. This will come in handy later when you want to use Matlab to
work with very large systems of equations. Matlab has standard mathematical
operators, +, -, / , *, ^ (exponent), and uses standard arithmetic notation for
grouping oeprations, ().
>>a
= 2; b = 3;
>>c
= a + b
c
=
5
>>c
= a^b
c
=
8
>>d
= c^(b-a)
d
=
8
>>
It
also has a library of standard mathematical functions, which you can access by
typing
>>help elfun
Elementary math functions.
Trigonometric.
sin - Sine.
sinh - Hyperbolic sine.
asin - Inverse sine.
asinh - Inverse hyperbolic sine.
cos - Cosine.
cosh - Hyperbolic cosine.
acos - Inverse cosine.
acosh - Inverse hyperbolic cosine.
tan - Tangent.
tanh - Hyperbolic tangent.
atan - Inverse tangent.
atan2 - Four quadrant inverse
tangent.
atanh - Inverse hyperbolic tangent.
sec - Secant.
sech - Hyperbolic secant.
asec - Inverse secant.
asech - Inverse hyperbolic secant.
csc - Cosecant.
csch - Hyperbolic cosecant.
acsc - Inverse cosecant.
acsch - Inverse hyperbolic cosecant.
cot - Cotangent.
coth - Hyperbolic cotangent.
acot - Inverse cotangent.
acoth - Inverse hyperbolic
cotangent.
Exponential.
exp - Exponential.
log - Natural
logarithm.
log10 - Common logarithm.
sqrt - Square root.
Complex.
abs - Absolute value.
angle - Phase angle.
conj - Complex conjugate.
imag - Complex imaginary
part.
real - Complex real part.
Numeric.
fix - Round towards
zero.
floor - Round towards minus
infinity.
ceil - Round towards plus
infinity.
round - Round towards nearest
integer.
rem - Remainder after
division.
sign - Signum function.
Typing
'help' by itself will list a table of contents of topics on which you can get
help. If you type 'help' followed by a specific function name (e.g. 'help
sin'), Matlab will print a description of the function.
Scratch
Files
Rather
than type commands in the Matlab
command window one by one, you can
type a sequence of commands in a file and cut and paste the commands into the
command window. Typically files containing Matlab code are saved with a '.m'
extension. Thus, you might create a scratch file for this tutorial called
tutorial1.m.
For
example, I can create a file with the lines
a
= 2;
b
= 3;
c
= a/b
d
= c*sin(a)
If
I were to cut and paste these four lines into the command window, I would see
the following appear
»a
= 2;
b
= 3;
c
= a/b
d
= c*sin(a)
c
=
0.6667
d
=
0.6062
»
Comments
can be included in Matlab code using the '%' character; thus,
%
this line will not be executed by the command interpreter,
%
nor will the text following the next bit of matlab code
x
= 20 * a + b; % doing something mindless
here
*****
Exercise
1
Implement
the standard normal function in a Matlab command line (be sure to initialize
the variable, x, before
calculating a). The number p is represented in Matlab by the character string, 'pi'.
![]()
*****
Matlab
maintains a workspace in which all variables you have defined are stored. You
can find out what variables you have defined so far by typing
>>
who
You
can find out what variables you have defined so far along with their sizes by
typing
>>
whos
This
is particularly useful for vectors and matrices.
To
clear the workspace, type
>>
clear
To
clear individual variables, type
>>
clear a
Try
this, and then retype 'who'.
2.
Vectors
Defining and referencing
Almost all of Matlab basic commands revolve around the
use of vectors. In fact, scalar values, like the ones you've been using up
until now, are, from the point of view of Matlab, one-dimensional vectors.
There are many ways to create vectors in Matlab. The simplest is to simply
assign a list of numbers to a variable as in
>>x = [1, 2, 3, 4, 5, 6]
x =
1 2 3 4 5 6
>>
Another convenient way to specify vectors is to use
the colon operator, as in
>>x = [1:6]
x =
1 2 3 4 5 6
>>
or
>>x = 1:6
x =
1 2 3 4 5 6
>>
You can also use the colon operator to specify a
vector whose elements are spaced by a common factor, as in
>>x = 1:2:5
x =
1 3 5
>>
If you specify bounds that can't be divided into the
specified increments, Matlab generates the largest vector within the specified
bounds, as in
>>x = 1:2:6
x =
1 3 5
>>
One can reference individual elements of a vector
using the notation
x
= [1, 3, 5, 1, 4, 2];
»x(2)
ans
=
3
»x(4)
ans
=
1
»
One can also reference subvectors using the colon
notation
»x(1:3)
ans
=
1 3 5
»
Operations on vectors
One way to use vectors in Matlab is as a simple data
structure. If you are familiar with other programming languages like C, we
would better refer to such vectors as arrays, since the term "vectors"
carries with it a certain mathematical baggage (vectors being mathematical
entities generally associated with linear algebra and Euclidean geometry, which
we will come to later). From the point of view of Matlab, of course, the
distinction is semantic. The meaning of a vector in Matlab is determined
entirely by how you use it. For now, we will treat them as arrays (or lists) of
numbers. We can perform point-wise operations on arrays, such as addition,
subtraction, multiplication, computing sines, cosines, etc. Beginning with
addition, we can add and subtract vectors, as in
»x
= [2, 2, 2, 2];
»y
= [-1, 1, 1, -1];
»x-y
ans
=
3 1 1 3
»
Notice that you didn't assign the result of the
subtraction to any variable. In this case, Matlab simply prints out the result.
Point-wise multiplication and division are somewhat more complicated.
Point-wise addition and subtraction of arrays is equivalent to the algebraic
operations of addition and subtraction applied to vectors. No such equivalence
exists for other algebraic operations. In fact, the operators, '*' and '/',
have specific meanings to Matlab when applied to vectors and matrices ('*'
corresponds to matrix multiplication). If we tried to use one of these, we
would get something like
»x*y
???
Error using ==> *
Inner
matrix dimensions must agree.
»
If we wanted to perform a point-wise multiplication of
two vectors, we would use the operator '.*'
»
x .* y
ans
=
-2 2 2 -2
»
The dot preceding the algebraic operator tells Matlab
to apply the operator to each pair of numbers in the vectors. We could use the
same notation with other algebraic operators; thus, we have
»x
.^ y
ans
=
0.5000 2.0000 2.0000 0.5000
»
One can apply a scalar operation to each element of a
vector using the standard operators, as in
»x
* 2
ans
=
4 4 4 4
»x
/ 3
ans
=
0.6667 0.6667 0.6667 0.6667
»x
+ 4
ans
=
6 6 6 6
»
The exception is the exponent operator, '^'. For this
operator, one must use the .^ notation
»x.^3
ans
=
8 8 8 8
»
Similarly, one can apply a mathematical function to
each element of a vector by simply passing the vector as an argument to the
function, as in
»x
= [1:100] * 2 * pi / 100;
»y
= sin(x);
Look
at the values in x and y to see what this has done (Just type 'x' or 'y' at the
prompt). You can also plot y as a function of x using
»plot(x,
y);
We'll
say more about plotting later.
*****
Exercise
2
a.
Compute the values of the standard
normal function at a reasonably large number of points between -3 and 3 using
two lines of Matlab code. Plot the result.
b.
Compute the values of a cosine wave
with a frequency of 1/2 Hz for the same points between -3 and 3 (a cosine wave
function of a given frequency is given by cos (2pfx),
where f is the frequency).
c.
Compute the values of a sine wave
with a frequency of 1/2 Hz for the same points between -3 and 3.
d.
Compute the vectors given by the
point-wise product of the standard normal function with the sine and cosine
functions. Plot each of these. These are examples of Gabor functions, which
play a significant role in modeling neurons in primary visual cortex.
*****
Vectors
in linear algebra
Linear
algebra is the basic mathematics of vectors and matrices. A convenient way to
visualize vectors is as points in Euclidean space. 2-dimensional vectors
specify points in a plane, 3
dimensional vectors specify points in a volume. Higher-dimensional vectors specify
points in higher-dimensional spaces, and thus are harder to visualize directly.
For our purposes, one can reason about operations on higher-dimensional vectors
by analogy with the same operations applied to points in 2 or 3 dimensions.
The
basic operations one can apply to vectors are addition and subtraction (we'll
get to the analogues of multiplication and division later). Addition of two
vectors can be visualized in a standard way by representing vectors as arrows
from the origin to the point specified by the sets of numbers in the vectors
Algebraically,
this is done by point-wise addition or multiplication of elements in two
vectors. Naturally, addition and subtraction of vectors obey the standard
algebraic properties of commutativity and associativity (a + b
= b + a, (a
+ b) + c = a
+ (b + c)).
A
fundamental operation which one can perform on a pair of vectors is the
inner-product of the two vectors. We will represent this as <a, b>. The
inner product of a pair of vectors returns a scalar value which is the sum of
the pair-wise products of the elements of the vectors. That is, if
![]()
and
![]()
then
![]()
which
we can write as
![]()
In
Matlab, one can compute the inner product of two vectors using the function,
dot (the inner product is often known as the dot product)
»x
= [1, 1, 1];
»y
= [-2, 1, 3];
»dot
(x,y)
ans
=
2
The
inner product of a vector with itself gives the square of the length of the
vector (in Matlab, the function, 'length', does not calculate the Euclidean
length of a vector, rather it returns the number of elements in the vector). In
other words, the length of a vector is given by the square-root of the
inner-product of the vector with itself. This is a generalization of the
Pythagorean relationship,
![]()
where
x and y are the lengths of two sides of a right triangle and z is the length of the hypotenuse. The general notation
for the length of a vector is | a |. Thus,
![]()
or
![]()
The
length of a vector can be computed in Matlab using
»sqrt(dot(x,x))
ans
=
1.7321
A
special class of vectors are unit-length vectors, also called normal vectors.
These have length 1.
Two
examples are
»x
= [.5099, .7, .3, .4];
»y
= [.7124, -.5, .2, -.45];
Confirm
that x and y are unit length.
The
inner product between two normal vectors is equivalent to the arc-cosine of the
angle between the two vectors. That is, if a and b are unit-length vectors, and q is the angle
between them
.
*****
Exercise
3
a.
Using two examples, show that the
inner-product obeys commutativity and distributivity; that is,
![]()
and
![]()
b.
Again, using two examples, show that
scalar multiplication of the inner product has the following property
![]()
c.
Define two vectors
x
= [1, 1, 1, 1];
y
= [2, 3, 1, 4];
Normalize
the vectors - transform them into unit-length vectors in the same direction as
the originals.
Compute
the angle between x and y (note that the vectors are not unit-length).
d.
A simple way to define a
two-dimensional unit vector is to specify an angle away from the x-axis and
form the vector
![]()
Define
two vectors for arbitrary angles, q and w. Compute the angle between the two vectors using the inner-product and
show that it is equal to q - w (assuming q > w).
Define
several pairs of two-dimensional vectors which are perpendicular to each other.
What is the inner-product between them? Vectors whose inner-products have this
property are generally referred to as orthogonal vectors.
*****
3.
Matrices
Defining
and referencing
A
matrix is represented in Matlab as an n x m array of numbers, where n is the number of rows in the matrix and m is the number of columns. We can define a matrix
using
»A
= [1 2 3; 3 2 4; 1 6 4]
A
=
1 2 3
3 2 4
1 6 4
Note that I have dropped the commas in the
specification of the rows. A matrix is defined in this way, as a set of row
vectors, seperated by a semi-colon. As with vectors, matrices in Matlab may be
used simply as a data structure to store a 2-dimensional array of data, for
example, the intensities of pixels in an image.
Matrix elements are referenced by row and column
number; thus, we have
>>A(2,3)
ans =
5
As with vectors, you can use the : notation to
reference sub-matrices,
»A(1:2,
2:3)
ans
=
2 3
2 4
If
you want to reference all rows, but only a subset of columns, you can type
»A(:,1)
ans
=
1
3
1
or
»A(:,2:3)
ans
=
2 3
2
4
6 4
If
we think of a matrix as set of column vectors, doing this will extract a subset
of the vectors. Similarly, for all columns, but a subset of rows, you can type
»A(2:3,:)
ans
=
3 2 4
1 6 4
Thinking
of a matrix as a set of row vectors (which is often a useful way to think of
them), this extracts a subset of the vectors.
An
important operation which one can apply to matrices is the transpose.
Transposing a matrix switches the rows and columns (the rows become columns and
vice-versa). In most texts, the transpose of a matrix, A, is represented by AT. In matlab, the transpose of an array (or matrix) is represented by
A'; thus we have
»B=A'
B
=
1 3 1
2 2 6
3 4 4
Note
that if A is not
"square" (the number of rows does not equal the number of columns),
taking the transpose of A changes
its "shape".
»A
= [1 2 3; 3 2 4]
A
=
1 2 3
3 2 4
»B
= A'
B
=
1 3
2 2
3 4
We
would say that A is a 2 x 3
matrix, and B is therefore a 3 x 2
matrix. In general, an n x m matrix is one with n rows and m columns.
As
we defined vectors above, they are 1 x n arrays. These are so-called row vectors. The transpose of a row vector
is a column vector.
»x
= [1, 6, -1]
x
=
1 6 -1
»x'
ans
=
1
6
-1
In
matlab, you can determine the size of an array using the "size"
command
»size(x)
ans
=
1 3
»size(x')
ans
=
3 1
»size(A)
ans
=
2 3
»size(A')
ans
=
3 2
If
an array is a vector, you can determine its length using the "length"
command
»length(x)
ans
=
3
»length(x')
ans
=
3
Array functions
Just
as one could with vectors, one can perform many basic algebraic operations on
piece-wise elements of an array. Thus, we have, for example
»A
= [1 2 3; 3 2 4]
A
=
1 2 3
3 2 4
»sin(A)
ans
=
0.8415 0.9093 0.1411
0.1411 0.9093 -0.7568
Similarly,
if we wanted to multiply the elements of two arrays together in a piece-wise
fashion to create another array, we would use the '.' notation, as in
»B
= [1 .5 .33333; .33333 .5 .25]
B
=
1.0000 0.5000 0.3333
0.3333 0.5000 0.2500
»A.*B
ans
=
1.0000 1.0000 1.0000
1.0000 1.0000 1.0000
»
Note
that this is not matrix multiplication, which has a different meaning in
mathematics (see below). algebraic operations performed on elements of an array
in a piece-wise fashion like this are not standard linear algebraic oeprations
with matrices. In a sense, we are simply using arrays as a data structure for
holding a two-dimensional list of values, and the special operators in Matlab
provide a short-hand way to express large numbers of calculations on such
lists.
Matrices
in linear algebra
Any
linear operation which transforms one vector to another one may be represented
by a matrix. Vectors can be "multiplied" by a matrix to generate a
new vector. We normally write matrix multiplication as
![]()
where
A is a matrix, and x and y are
column vectors. To compute the multiplication of A with x,
simply compute the inner product of each row of A with the vector x - the results give the elements of the transformed
vector y.
In
matlab, matrix multiplication is represented by
»y
= A * x
******
Exercise
4
a.
Define an arbitrary 3 x 3 matrix, A, and an arbitrary 3-element column vector, x
b.
Compute a new vector given by the
inner product of each row of A with x
c.
Compute the multiplication of A with
x in the standard way. The results should be the same as above.
d.
Define an arbitrary 2 x 3 matrix, B
e.
Multiply B by x
f.
Multiply B by x' (note that this
doesn't work - the number of columns in A must be the same as the number of
rows in x)
*******
In
general, you can multiply any n x 1 vector by a m x n matrix. Matrices can be
serially applied to a vector (assuming the dimensions are correct). For example
»x
= [1; 6; -1] % notice the semi-colons - this makes a column vector, rather than
a row vector
x
=
1
6
-1
»A
= [1 2 3; 4 2 1; 3 1 2]
A
=
1 2 3
4 2 1
3 1 2
»B
= [1 1 1; -1 1 1; 1 1 -1]
B
=
1 1 1
-1 1 1
1 1 -1
»y
= A * x
y
=
10
15
7
»z
= B * y
z
=
32
12
18
»z
= B*A*x
z
=
32
12
18
»
Not
surprisingly, the operation of multiplying a vector by one matrix and then
multiplying the result by another matrix, can be represented by multiplying the
original vector by a single matrix computed from the first two. This matrix is
given by multiplication of the first two matrices
»
C = B*A
C
=
8 5 6
6 1 0
2 3 2
»C*x
ans
=
32
12
18
»
Matrix
multiplication has an important property - it does not commute. That is, AB does not equal BA. You can see this in this example
»B*A*x
ans
=
32
12
18
»A*B*x
ans
=
38
40
38
»
An
important property of multiplication is that a number exists, called the
identity, which when multiplied by another number gives that number (the
identity is, of course, one). Similarly, for matrices, a matrix exists which,
when multiplied by a vector, gives the original vector. This is the identity
matrix. It is a square matrix with ones in the diagonals and zeros everywhere
else
»I
= [1 0 0; 0 1 0; 0 0 1]
I
=
1 0 0
0 1 0
0 0 1
»x
x
=
1
6
-1
»I*x
ans
=
1
6
-1
»
Every
square matrix also has an important dual matrix associated with it - its
inverse. The inverse of a matrix A
is represented by A-1. Multiplication of A by A-1 gives the
identity matrix. In matlab, the inverse of a matrix is written as inv(A). Thus
we have
»A
A
=
1 2 3
4 2 1
3 1 2
»inv(A)
ans
=
-0.2308 0.0769 0.3077
0.3846 0.5385 -0.8462
0.1538 -0.3846 0.4615
»A*inv(A)
ans
=
1.0000 0.0000 0.0000
0 1.0000 0.0000
0 0.0000 1.0000
»
Note
that A A-1x = x. Thus, we have
»y
= A*x
y
=
10
15
7
»inv(A)
* y
ans
=
1.0000
6.0000
-1.0000
»
which
is, of course, just the original value of x.
An important class of
matrices are ones that perform
simple rotations of a vector. In
order to see how to construct such a matrix, note that rotating a
two-dimensional vector, x
= [x, y], by an angle Ω requires the following
calculation
x’ = x * cos Ω
+ y * sin Ω
y’ = x * sin Ω
- y • cos Ω
where x’ = [x’,y’] is the rotated vector. This is equivalent to
multiplying the vector x by the
matrix, R
![]()
**********
Exercise
5
1. Create a matrix that will
rotate vectors by 30 degrees. By applying the matrix to two random vectors,
show that the result maintain the lengthes of the vectors and the angle between
the vectors.
2. Without using the matlab
facility for computing the inverse of the rotation matrix, define the inverse
of the matrix created in (1). Apply it to the rotated vectors to show that it
gives back the original vectors. Apply it to the original rotation matrix to
show that it results in the identity matrix.
***********
4. Matrix building functions
Matlab
provides a number of functions for creating and manipulating matrices. We will
go over
three
of them here,
ones(n,m)
zeros(n,m)
rand(n,m)
These
functions do pretty much what their names suggest. ones (n,m) creates an n x
m matrix full of ones. Similarly for
zeros. rand (n,m) creates an n x m
matrix filled with random numbers uniformly distributed between 0 and 1. Some
examples are
»ones(2,3)
ans
=
1 1 1
1 1 1
»zeros(3,2)
ans
=
0 0
0 0
0 0
»rand(2,2)
ans
=
0.9501 0.6068
0.2311 0.4860
»rand(2)
ans
=
0.8913 0.4565
0.7621 0.0185
»
Notice
that we only used one argument in the last example - this causes the functions
to create a square (n x n) matrix.
Matlab
provides a very useful trick for creating a matrix which contains multiple
copies of a single vector. An example is given below
»x
= 1:10
x
=
1 2 3 4 5 6 7 8 9 10
»X
= x(ones(10,1), :)
X
=
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
»y
= x';
»Y
= y(:,ones(1,10))
Y
=
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10 10 10
The
notation is rather opaque, but the syntax should be clear from close inspection
of the expressions.
You
can use the trick in exactly the way shown here to create two-dimensional
functions on a regular x-y grid. I will leave it as an exercise for you to
create a matrix holding the values of a 2-dimensional Gaussian defined on a
grid.
**********
Exercise
6
a.
Using no more than four lines of
matlab code, create a matrix of values of the 2-dimensional gaussian
![]()
in
the range
![]()
b.
Plot the results using the 'surf'
command
surf(x,y,z)
(Type
'help surf' to get more information on the arguments to the function)
**************
5.
Plotting and image functions
Matlab
provides a large number of functions to plot and visualize data. The simplest
type of plot is a Cartesian or orthogonal x,y plot, based on plotting the x,y
data pairs from two specified vectors. Clearly, the vectors x and y must have
the same number of elements. Imagine that you wish to plot the function exp for
values of x from 0 to 2.
x
= 0:.1:2;
y
= exp(x);
plot(x,y)
Of
course the symbols x,y are arbitrary. If we wanted to plot temperature on the
ordinate and time on the abscissa, and vectors for temperature and time were
loaded in 'matlab', the command would
be
plot(time,temperature)
Notice
that the command plot(x,y) opens a graphics window. If you now
execute the command 'grid', the graphics window is redrawn (Note you must move
the cursor to the command window before typing new commands). To avoid
redrawing the window, you may use the line continuation ellipsis. Consider
plot(x,y),
grid, title('Exponential Function'), xlabel('x'), ylabel('exp(x)'), ...
text(.6,.4,'
y = exp(x)','sc');
Note
that the commands appear on one line seperated by commas. The end of the first
line is a series of three dots. this tells matlab to continue the line on the
following one (a useful trick when command lines get inordinately long). The
reason for doing this here is that matlab would redraw the window for each of
the individual plotting commands if they were given on seperate lines.
Try
the commands plot(x) and plot(y) to ensure you understand how they differ from
plot(x,y).
More
than a single graph can be presented on one graphic plot. One common way to
accomplish this is hold the graphics window open with the 'hold' command and
execute a subsequent plotting command.
x1=0:.05*pi:pi;
y1=sin(x1);
plot(x1,y1)
hold
on
y2=cos(x1);
plot(x1,y2)
The
"hold on" command will remain active until you turn it off with the
command 'hold off'.
You
can create multiple graphs by using multiple arguments. In addition to the vectors x,y created
earlier, create the vectors a,b and plot both vector sets simultaneously as
follows.
a
= 1 : .1 : 3;
b
= 10*exp(-a);
plot(x,y,a,b)
Multiple
plots can be accomplished also by using matrices rather than simple vectors in
the argument. If the arguments of
the 'plot' command are matrices, the COLUMNS of y are plotted on the ordinate
against the COLUMNS of x on the abscissa. Note that x and y must be of the same
order! If y is a matrix and x is a
vector, the rows or columns of y are plotted against the elements of x. In this
instance, the number of rows OR columns in the matrix must correspond to the
number of elements in 'x'. The matrix 'x' can be a row or a column vector!
Recall
the row vectors 'x' and 'y' defined earlier. Augment the row vector 'y' to
create the 2-row matrix, yy.
yy=[y;exp(1.2*x)];
plot(x,yy)
In
order to plot 2-dimensional functions, you can use either "thresh" or
"surf". These functions take as arguments three arrays (of the same
size), which specify the x coordinates of each point in a two-dimensional grid,
the y coordinates of each point in the grid, and the function values at each
point in the grid. Try the following, using the X and Y
arrays defined earlier
surf(X,Y,(X-5)
.* (Y-5))