Using MATLAB in Linear Algebra_Math221 - tutorial3.pdf

(111 KB) Pobierz

Edward Neuman
Department of Mathematics
Southern Illinois University at Carbondale
edneuman@siu.edu
One of the nice features of MATLAB is its ease of computations with vectors and matrices. In
this tutorial the following topics are discussed: vectors and matrices in MATLAB, solving
systems of linear equations, the inverse of a matrix, determinants, vectors in n-dimensional
Euclidean space, linear transformations, real vector spaces and the matrix eigenvalue problem.
Applications of linear algebra to the curve fitting, message coding and computer graphics are also
included.

For the reader's convenience we include lists of special characters and MATLAB functions that
are used in this tutorial.
;
'
.'
*
.
^
[]
:
=
==
\
/
i, j
~
~=
&
|
{}
Special characters
Semicolon operator
Conjugated transpose
Transpose
Times
Dot operator
Power operator
Emty vector operator
Colon operator
Assignment
Equality
Backslash or left division
Right division
Imaginary unit
Logical not
Logical not equal
Logical and
Logical or
Cell
2
Function
acos
axis
char
chol
cos
cross
det
diag
double
eig
eye
fill
fix
fliplr
flops
grid
hadamard
hilb
hold
inv
isempty
legend
length
linspace
logical
magic
max
min
norm
null
num2cell
num2str
ones
pascal
plot
poly
polyval
rand
randn
rank
reff
rem
reshape
roots
sin
size
sort
Description
Inverse cosine
Control axis scaling and appearance
Create character array
Cholesky factorization
Cosine function
Vector cross product
Determinant
Diagonal matrices and diagonals of a matrix
Convert to double precision
Eigenvalues and eigenvectors
Identity matrix
Filled 2-D polygons
Round towards zero
Flip matrix in left/right direction
Floating point operation count
Grid lines
Hadamard matrix
Hilbert matrix
Hold current graph
Matrix inverse
True for empty matrix
Graph legend
Length of vector
Linearly spaced vector
Convert numerical values to logical
Magic square
Largest component
Smallest component
Matrix or vector norm
Null space
Convert numeric array into cell array
Convert number to string
Ones array
Pascal matrix
Linear plot
Convert roots to polynomial
Evaluate polynomial
Uniformly distributed random numbers
Normally distributed random numbers
Matrix rank
Reduced row echelon form
Remainder after division
Change size
Find polynomial roots
Sine function
Size of matrix
Sort in ascending order
3
subs
sym
tic
title
toc
toeplitz
tril
triu
vander
varargin
zeros
Symbolic substitution
Construct symbolic bumbers and variables
Start a stopwatch timer
Graph title
Read the stopwatch timer
Tioeplitz matrix
Extract lower triangular part
Extract upper triangular part
Vandermonde matrix
Variable length input argument list
Zeros array

The purpose of this section is to demonstrate how to create and transform vectors and matrices in
MATLAB.
This command creates a row vector
a = [1 2 3]
a =
1
2
3
Column vectors are inputted in a similar way, however, semicolons must separate the components
of a vector
b = [1;2;3]
b =
1
2
3
The
quote operator
'
is used to create the
conjugate transpose
of a vector (matrix) while the
dot-
quote operator
.'
creates the
transpose
vector (matrix). To illustrate this let us form a complex
vector
a + i*b'
and next apply these operations to the resulting vector to obtain
(a+i*b')'
ans =
1.0000 - 1.0000i
2.0000 - 2.0000i
3.0000 - 3.0000i
while
4
(a+i*b').'
ans =
1.0000 + 1.0000i
2.0000 + 2.0000i
3.0000 + 3.0000i
Command
length
returns the number of components of a vector
length(a)
ans =
3
The
dot operator.
plays a specific role in MATLAB. It is used for the componentwise application
of the operator that follows the dot operator
a.*a
ans =
1
4
9
The same result is obtained by applying the
power operator
^
to the vector
a
a.^2
ans =
1
4
9
Componentwise division of vectors
a
and
b
can be accomplished by using the
backslash operator
\
together with the dot operator
.
a.\b'
ans =
1
1
1
For the purpose of the next example let us change vector
a
to the column vector
a = a'
a =
1
2
3
The
dot product
and the
outer product
of vectors
a
and
b
are calculated as follows
dotprod = a'*b
5
dotprod =
14
outprod = a*b'
outprod =
1
2
3
2
4
6
3
6
9
The
cross product
of two three-dimensional vectors is calculated using command
cross.
Let the
vector
a
be the same as above and let
b = [-2 1 2];
Note that the semicolon after a command avoids display of the result. The cross product of
a
and
b
is
cp = cross(a,b)
cp =
1
-8
5
The cross product vector
cp
is perpendicular to both
a
and
b
[cp*a cp*b']
ans =
0
0
We will now deal with operations on matrices. Addition, subtraction, and scalar multiplication are
defined in the same way as for the vectors.
This creates a 3-by-3 matrix
A = [1 2 3;4 5 6;7 8 10]
A =
1
4
7
2
5
8
3
6
10
Note that the
semicolon operator
;
separates the rows. To extract a submatrix
B
consisting of
rows 1 and 3 and columns 1 and 2 of the matrix
A
do the following
B = A([1 3], [1 2])
B =
1
7
2
8
To interchange rows 1 and 3 of
A
use the vector of row indices together with the colon operator
C = A([3 2 1],:)
Zgłoś jeśli naruszono regulamin