MATLAB Basics
Download
Report
Transcript MATLAB Basics
MATLAB Basics
With a brief review of linear algebra
by Lanyi Xu
modified by D.G.E. Robertson
1. Introduction to vectors and
matrices
MATLAB= MATrix LABoratory
What is a Vector?
What is a Matrix?
Vector and Matrix in Matlab
What is a vector
A vector is an array of elements, arranged
in column, e.g.,
x1
x
x 2
xn
X is a n-dimensional column vector.
In physical world, a vector is normally
3-dimensional in 3-D space or 2dimensional in a plane (2-D space), e.g.,
x1 1
x x2 5
x3 2
, or
y1 8
y
y 2 6
If a vector has only one dimension, it
becomes a scalar, e.g.,
z z1 5 5
Vector addition
Addition of two vectors is defined by
x1 y1
x y
2
xy 2
x
y
n
n
Vector subtraction is defined in a similar manner. In both
vector addition and subtraction, x and y must have the
same dimensions.
Scalar multiplication
A vector may be multiplied by a scalar, k,
yielding
kx1
kx
2
kx
kxn
Vector transpose
The transpose of a vector is defined, such
that, if x is the column vector
x1
x
x 2
xn
its transpose is the row vector
x x1
T
x2 xn
Inner product of vectors
The quantity xTy is referred as the inner
product or dot product of x and y and
yields a scalar value (or x ∙ y).
x y x1 y1 x2 y2 xn yn
T
If xTy = 0
x and y are said to be orthogonal.
In addition, xTx , the squared length of
the vector x , is
x x x x x
T
2
1
2
2
2
n
The length or norm of vector x is
denoted by
x x x
T
Outer product of vectors
The quantity of xyT is referred as the
outer product and yields the matrix
x1 y1
x y
2 1
T
xy
xn y1
x1 y2
x2 y 2
xn y 2
x1 yn
x2 yn
xn y n
Similarly, we can form the matrix xxT as
x12
x2 x1
T
xx
xn x1
x1 x2
x22
xn x2
x1 xn
x2 xn
2
xn
where xxT is called the scatter matrix
of vector x.
Matrix operations
A matrix is an m by n rectangular array
of elements in m rows and n columns,
and normally designated by a capital
letter. The matrix A, consisting of m
rows and n columns, is denoted as
A a ij
Where aij is the element in the ith row and
jth column, for i=1,2,,m and j=1,2,…,n. If
m=2 and n=3, A is a 23 matrix
a11 a12 a13
A
a 21 a 22 a 23
Note that vector may be thought of as a
special case of matrix:
a column vector may be thought of as a
matrix of m rows and 1 column;
a rows vector may be thought of as a
matrix of 1 row and n columns;
A scalar may be thought of as a matrix
of 1 row and 1 column.
Matrix addition
Matrix addition is defined only when the
two matrices to be added are of
identical dimensions, i.e., that have the
same number of rows and columns.
A B aij bij
e.g.,
For m=3 and n=n:
a11 b11
A B a21 b21
a31 b31
a12 b12
a22 b22
a32 b32
Scalar multiplication
The matrix A may be multiplied by a
scalar k. Such multiplication is denoted
by kA where
kA kaij
i.e., when a scalar multiplies a matrix, it
multiplies each of the elements of the
matrix, e.g.,
For 32 matrix A,
ka11
kA ka21
ka31
ka12
ka22
ka32
Matrix multiplication
The product of two matrices, AB, read
A times B, in that order, is defined by
the matrix
AB C cij
p
cij aik bkj ai1b1 j ai 2b2 j aipbpj
k 1
The product AB is defined only when A
and B are comfortable, that is, the number
of columns is equal to the number of rows
in B. Where A is mp and B is pn, the
product matrix [cij] has m rows and n
columns, i.e.,
AmpBpn Cmn
For example, if A is a 23 matrix and B
is a 32 matrix, then AB yields a 22
matrix, i.e.,
a11b11 a12b21 a13b31 a11b12 a12b22 a13b32
AB C
a21b11 a22b21 a23b31 a21b12 a22b22 a23b32
In general,
AB BA
For example, if
1 4
A 2 5
3 6
and
3 2 1
B
, then
6 5 4
1 4
27 22 17
3 2 1
AB 2 5
36
29
22
6
5
4
45 36 27
3 6
and
1 4
3 2 1
10 28
BA
2 5
6
5
4
28
73
3 6
Obviously, AB BA .
Vector-matrix Product
If a vector x and a matrix A are
conformable, the product y=Ax is
defined such that
n
yi aij x j
j 1
For example, if A is as before and
x is as follow,
1
x
2
, then
1 4
9
1
y Ax 2 5 12
2
3 6
15
Transpose of a matrix
The transpose of a matrix is obtained
by interchanging its rows and columns,
e.g., if
a
a
a
A 11 12
a21 a22
a11
then T
A a12
a13
a21
a22
a23
13
a23
Or, in general,
A=[aij], AT=[aji].
Thus, an mn matrix has an nm
transpose.
For matrices A and B, of appropriate
dimension, it can be shown that
AB
T
B A
T
T
Inverse of a matrix
In considering the inverse of a matrix,
we must restrict our discussion to
square matrices. If A is a square
matrix, its inverse is denoted by A-1
such that
1
A A AA
1
I
where I is an identity matrix.
An identity matrix is a square matrix
with 1 located in each position of the
main diagonal of the matrix and 0s
elsewhere, i.e.,
1 0 0
0 1 0
I
0 0 1
It can be shown that
A A
1 T
T 1
MATLAB basic operations
MATLAB is based on matrix/vector
mathematics
Entering matrices
Enter an explicit list of elements
Load matrices from external data files
Generate matrices using built-in functions
Create vectors with the colon (:) operator
>> x=[1 2 3 4 5];
>> A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
A=
16
3
2
13
5
10
11
8
9
6
7
4
15
14
>>
12
1
Generate matrices using builtin functions
Functions such as zeros(), ones(), eye(),
magic(), etc.
>> A=zeros(3)
A=
0 0 0
0 0 0
0 0 0
>> B=ones(3,2)
B=
1
1
1
1
1
1
>> I=eye(4) (i.e., identity matrix)
I=
1
0
0
0
0
1
0
0
0
0
1
0
0
0
0
1
>> A=magic(4) (i.e., magic square)
A=
16
2
3 13
5 11 10
8
9
7
6 12
4 14 15
1
>>
Generate Vectors with Colon (:)
Operator
The colon operator uses the following rules to create
regularly spaced vectors:
j:k is the same as [j,j+1,...,k]
j:k is empty if j > k
j:i:k is the same as [j,j+i,j+2i, ...,k]
j:i:k is empty if i > 0 and j > k or if i < 0 and j < k
where i, j, and k are all scalars.
Examples
>> c=0:5
c=
0 1 2 3 4 5
>> b=0:0.2:1
b=
0 0.2000 0.4000 0.6000
>> d=8:-1:3
d=
8
7
6
5
4
3
>> e=8:2
e=
Empty matrix: 1-by-0
0.8000
1.0000
Basic Permutation of Matrix in
MATLAB
sum, transpose, and diag
Summation
We can use sum() function.
Examples, >> X=ones(1,5)
X=
1 1 1
>> sum(X)
ans =
5
>>
1
1
>> A=magic(4)
A=
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> sum(A)
ans =
34 34 34
>>
34
Transpose
>> A=magic(4)
A=
16
2
3 13
5 11 10
8
9
7
6 12
4 14 15
1
>> A'
ans =
16 5
2 11
3 10
13
8
>>
9
4
7 14
6 15
12 1
Expressions of MATLAB
Operators
Functions
Operators
+
*
/
\
^
'
()
Addition-Subtraction
Multiplication
Division
Left division
Power
Complex conjugate transpose
Specify evaluation order
Functions
MATLAB provides a large number of
standard elementary mathematical functions, including
abs, sqrt, exp, and sin.
Useful constants:
pi
i
j
3.14159265...
Imaginary unit ( 1 )
Same as i
>> rho=(1+sqrt(5))/2
rho =
1.6180
>> a=abs(3+4i)
a=
5
>>
Basic Plotting Functions plot( )
The plot function has different forms,
depending on the input arguments.
If y is a vector,
plot(y) produces a piecewise linear graph of
the elements of y versus the index of the elements of y.
If you specify two vectors
as arguments, plot(x,y) produces a graph of y versus x.
Example,
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
Multiple Data Sets in One
Graph
x = 0:pi/100:2*pi;
y = sin(x);
y2 = sin(x-.25);
y3 = sin(x-.5);
plot(x,y,x,y2,x,y3)
Distance between a Line and a
Point
given line defined by points a and b
find the perpendicular distance (d) to
point c
b a c a
d=
norm(cross((b-a),(c-a)))/norm(b-a)
ba