50
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

Embed Size (px)

Citation preview

Page 1: 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 Xumodified by D.G.E. Robertson

Page 2: 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

Page 3: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

What is a vector

A vector is an array of elements, arranged in column, e.g.,

nx

x

x

2

1

x

X is a n-dimensional column vector.

Page 4: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

In physical world, a vector is normally 3-dimensional in 3-D space or 2-dimensional in a plane (2-D space), e.g.,

2

5

1

3

2

1

x

x

x

x

6

8

2

1

y

yy, or

Page 5: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

If a vector has only one dimension, it becomes a scalar, e.g.,

551 zz

Page 6: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Vector addition

Addition of two vectors is defined by

nn yx

yx

yx

22

11

yx

Vector subtraction is defined in a similar manner. In both vector addition and subtraction, x and y must have the same dimensions.

Page 7: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Scalar multiplication

A vector may be multiplied by a scalar, k, yielding

nkx

kx

kx

k

2

1

x

Page 8: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Vector transpose

The transpose of a vector is defined, such that, if x is the column vector

nx

x

x

2

1

x

its transpose is the row vector nxxx 21Tx

Page 9: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

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).

nn yxyxyx 2211yxT

If xTy = 0

x and y are said to be orthogonal.

Page 10: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

In addition, xTx , the squared length of the vector x , is

The length or norm of vector x is denoted by

222

21 nxxx xxT

xxx T

Page 11: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Outer product of vectors The quantity of xyT is referred as

the outer product and yields the matrix

nnnn

n

n

yxyxyx

yxyxyx

yxyxyx

21

22212

12111

Txy

Page 12: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Similarly, we can form the matrix xxT as

221

22212

12121

nnn

n

n

xxxxx

xxxxx

xxxxx

Txx

where xxT is called the scatter matrix of vector x.

Page 13: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

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

Page 14: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

ijaA

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

232221

131211

aaa

aaaA

Page 15: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

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.

Page 16: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

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. ijij ba BA

e.g.,

Page 17: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

For m=3 and n=n:

3232

2222

1212

3131

2121

1111

ba

ba

ba

ba

ba

ba

BA

Page 18: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Scalar multiplication The matrix A may be multiplied by

a scalar k. Such multiplication is denoted by kA where

ijkak A

i.e., when a scalar multiplies a matrix, it multiplies each of the elements of the matrix, e.g.,

Page 19: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

For 32 matrix A,

32

22

12

31

21

11

ka

ka

ka

ka

ka

ka

kA

Page 20: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Matrix multiplication The product of two matrices, AB,

read A times B, in that order, is defined by the matrix

ijcCAB

pjipjiji

p

kkjikij babababac

22111

Page 21: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

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.,

nmnppm CBA

Page 22: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

For example, if A is a 23 matrix and B is a 32 matrix, then AB yields a 22 matrix, i.e.,

322322221221312321221121

321322121211311321121111

babababababa

babababababaCAB

In general, BAAB

Page 23: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

For example, if

6

5

4

3

2

1

A

456

123Ban

d, then

273645

222936

172227

456

123

6

5

4

3

2

1

AB

Page 24: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

and

7328

2810

63

52

41

456

123BA

Obviously,

BAAB .

Page 25: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Vector-matrix Product If a vector x and a matrix A are

conformable, the product y=Ax is defined such that

n

jjiji xay

1

Page 26: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

For example, if A is as before and x is as follow,

2

1x , then

15

12

9

2

1

6

5

4

3

2

1

Axy

Page 27: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Transpose of a matrix The transpose of a matrix is

obtained by interchanging its rows and columns, e.g., if

232221

131211

aaa

aaaA

then

23

22

21

13

12

11

a

a

a

a

a

aTA

Or, in general,

A=[aij], AT=[aji].

Page 28: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Thus, an mn matrix has an nm transpose.

For matrices A and B, of appropriate dimension, it can be shown that

TTT ABAB

Page 29: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

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

IAAAA 11

where I is an identity matrix.

Page 30: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

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.,

100

010

001

I

Page 31: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

It can be shown that

1TT1 AA

Page 32: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

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

Page 33: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

>> 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

>>

Page 34: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson
Page 35: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson
Page 36: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

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 11 11 1

Page 37: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

>> I=eye(4) (i.e., identity matrix)I =

1 0 0 00 1 0 00 0 1 00 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

>>

Page 38: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

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 > kj: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.

Page 39: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

>> c=0:5c = 0 1 2 3 4 5>> b=0:0.2:1b = 0 0.2000 0.4000 0.6000 0.8000 1.0000>> d=8:-1:3d =

8 7 6 5 4 3>> e=8:2e = Empty matrix: 1-by-0

Examples

Page 40: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Basic Permutation of Matrix in MATLAB sum, transpose, and diag

SummationWe can use sum() function.Examples,>> X=ones(1,5)

X = 1 1 1 1 1>> sum(X)ans =

5>>

Page 41: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

>> 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

>>

Page 42: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

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

>>

Page 43: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Expressions of MATLAB Operators Functions

Page 44: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Operators

+ Addition-Subtraction* Multiplication/ Division\ Left division^ Power' Complex conjugate transpose( ) Specify evaluation order

Page 45: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Functions

MATLAB provides a large number of standard elementary mathematical functions, including abs, sqrt, exp, and sin.

pi 3.14159265...i Imaginary unit ( )j Same as i

Useful constants:

1

Page 46: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

>> rho=(1+sqrt(5))/2rho = 1.6180

>> a=abs(3+4i)a = 5

>>

Page 47: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

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.

Page 48: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Example,

x = 0:pi/100:2*pi;y = sin(x);plot(x,y)

Page 49: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

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)

Page 50: MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson

Distance between a Line and a Point given line defined by points a

and b find the perpendicular distance (d) to point c

d =

norm(cross((b-a),(c-a)))/norm(b-a)

ab

acab