Professional Documents
Culture Documents
MATLAB Basics: With A Brief Review of Linear Algebra by Lanyi Xu Modified by D.G.E. Robertson
MATLAB Basics: With A Brief Review of Linear Algebra by Lanyi Xu Modified by D.G.E. Robertson
MATLAB Basics: With A Brief Review of Linear Algebra by Lanyi Xu Modified by D.G.E. Robertson
xn
x1 1 y1 8
x x2 5 , or y
x3 2 y2 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
xy 2 2
xn y n
kxn
Vector transpose
The transpose of a vector is defined, such
that, if x is the column vector
x1
x
x 2
xn
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
x1 y1 x1 y2 x1 yn
x y
x2 y 2 x2 y n
xy
T 2 1
xn y1 xn y 2 xn y n
Similarly, we can form the matrix xxT as
x12 x1 x2 x1 xn
x2 x1 x22 x2 xn
xx
T
2
xn x1 xn x2 xn
A B aij bij
e.g.,
For m=3 and n=n:
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 ka12
kA ka21 ka22
ka31 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.,
A m p B p n C m n
For example, if A is a 23 matrix and B
is a 32 matrix, then AB yields a 22
matrix, i.e.,
In general, AB BA
For example, if
1 4
3 2 1
A 2 5 and B , then
6 5 4
3 6
1 4 27 22 17
3 2 1
AB 2 5 36 29 22
6 5 4
3 6 45 36 27
and
1 4
3 2 1 10 28
BA 2 5
6 5 4 3 6 28 73
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 , then
2
1 4 9
1
y Ax 2 5 12
3 6 2
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 13
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 1
A A AA I
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 12
4 15 14 1
>>
Generate matrices using built-
in 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:
>> 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 9 4
2 11 7 14
3 10 6 15
13 8 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 3.14159265...
i Imaginary unit ( 1 )
j 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.
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=
ba
norm(cross((b-a),(c-a)))/norm(b-a)