introduction to matlab module #2 page 1 introduction to matlab module #2 – arrays topics 1.numeric...

27
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1. Numeric arrays (creation, addressing, sizes) 2. Element-by-Element Operations 3. Matrix Operations 4. Polynomial Operations Textbook Reading Assignments 1. 2.1-2.5 Practice Problems 1. Chapter 2, Problems: 1, 2, 3, 8, 9, 13

Upload: franklin-waters

Post on 03-Jan-2016

232 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2

Page 1

Introduction to Matlab

Module #2 – Arrays• Topics

1. Numeric arrays (creation, addressing, sizes)

2. Element-by-Element Operations

3. Matrix Operations

4. Polynomial Operations

• Textbook Reading Assignments1. 2.1-2.5

• Practice Problems1. Chapter 2, Problems: 1, 2, 3, 8, 9, 13

Page 2: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2

Page 2

1) Numeric Arrays

• Arrays

- An array is a collection of data that can be described with a single variable

- Arrays are entered into Matlab using square brackets (i.e., y = [1, 2, 3])

• Row Vectors

- A horizontal arrangement of elements:

- Entered in Matlab using comma delimiters within the square brackets: >> x = [5, 7 2]

- Individual elements are addressed using indexes starting at 1: x(1) → 5 x(2) → 7 x(3) → 2

275x

275x

1 2 3

Page 3: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2

Page 3

1) Numeric Arrays

• Column Vectors

- A vertical arrangement of elements

- Column entries are entered in Matlab using the semicolon within the square brackets >> y = [8; -2; 4]

- Individual elements are addressed using indexes: y(1) → 8 y(2) → -2 y(3) → 3

4

2

8

y

4

2

8

y

1

2

3

Page 4: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2

Page 4

1) Numeric Arrays

• Automatic Creation of Arrays

- Equally space elements can be created using: >> t = [0:1:10]

start value step size end value

- the same thing can be accomplished using the linspace(a,b,n) command, which allows you to enterthe start (a), the end (b), and the number of elements in the array (n). Matlab will create the arraywith regular spacing between elements.

>> t = linspace[0,10,11]

start value end size # of points in array

- logarithmic spacing can be accomplished using the logspace(a,b,n) command

Page 5: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2

Page 5

1) Numeric Arrays

• 2D Arrays or Matrices

- Matrices are 2D arrays that are m x n in size where: m = # of rows n = # of columns

- Matrices can be directly entered in Matlab using a combination of commas and semicolons:

>> A = [2,5; -3,4; -7,1]

- When talking about Matrices, the row always comes first

i.e., (row, column)

- We say that A is a 3 x 2 matrix (i.e., it has 3 rows and 2 columns)

17

43

52

A

n

m

Page 6: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2

Page 6

1) Numeric Arrays

• Addressing Matrices

- Individual entries can be addressed using the (row, column) location: A(1,1) → 2 A(1,2) → 5

A(2,1) → -3 A(2,2) → 4

A(3,1) → -7 A(3,2) → 1

- The colon (:) represents all element addresses

i.e., >> B = A(:,1) would yield: i.e., All rows, 1st column

>> C = A(2,:) would yield i.e., 2nd row, all columns

1

2

3

17

43

52

A

1 2

7

3

2

B

43C

Page 7: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2

Page 7

1) Numeric Arrays

• Addressing Matrices

- A range of entries can be addressed using the semicolon:

i.e., >> D = A(1:2,1:2) would yield:

i.e., rows 1 & 2, columns 1 & 2

1

2

3

17

43

52

A

1 2

43

52D

Page 8: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2

Page 8

1) Numeric Arrays

• Creating 2D Arrays with the Matrix Editor

- A 2D array variable can be created using the empty or null operator []

D = [] this creates an empty matrix variable D

- This variable can now be double clicked on in the Workspace to launch the Array editor

Page 9: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2

Page 9

2) Array Operations (element-by-element)

• A variety of built-in functions exist to help analyze a Matrix

- length(M) % if a vector, returns the number of elements in the array % if a matrix, returns the largest number of elements in the array (either row or column)

>> length(X) → 4

>> length(Y) → 4

>> length(Z) → 3

17

43

52

Z 9520 X

7

6

8

4

Y

Page 10: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 10

2) Array Operations (element-by-element)

• A variety of built-in functions exist to help analyze a Matrix

- size(M) % returns the matrix size in row, column (m x n) format

>> size(X) → [1 4]

>> size(Y) → [4 1]

>> size(Z) → [3 2]

17

43

52

Z 9520 X

7

6

8

4

Y

Page 11: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2

Page 11

2) Array Operations (element-by-element)

- max(M) % if a vector, returns the algebraically largest value % if a matrix, returns the row vector containing the largest value

>> max(X) → 9 >> max(Y) → 8 >> max(Z) → [2 5]

- min(M) % if a vector, returns the algebraically smallest value % if a matrix, returns the row vector containing the smallest value

>> min(X) → -5 >> min(Y) → -6 >> min(Z) → [-7 1]

17

43

52

Z 9520 X

7

6

8

4

Y

Page 12: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 12

2) Array Operations (element-by-element)

- sort(M) % if a row vector, returns a row vector of the same size elements in ascending order % if a column vector, returns a column vector of the same size elements in ascending order % if a matrix, returns a matrix of the same size with columns sorted in ascending order

>> sort(X) →

>> sort(Y) →

>> sort(Z) →

17

43

52

Z 9520 X

7

6

8

4

Y

9205

8

7

4

6

52

43

17

Page 13: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 13

- sum(M) % if a vector, returns a scalar with the sum of all elements % if a matrix, returns a row vector with the sum of each columns

>> sum(X) → 6

>> sum(Y) → 5

>> sum(Z) → [-8 10]

17

43

52

Z 9520 X

7

6

8

4

Y

2) Array Operations (element-by-element)

Page 14: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 14

2) Array Operations (element-by-element)

• Matrices can also be “Transposed” to interchange rows and columns

• The transpose command in Matlab is the tick (‘)

>> X’ >> Y’

>> Z’

• The transpose command is often used to make the sort/sum commands work more efficiently

17

43

52

Z 9520 X

7

6

8

4

Y

7

5

2

0

TX 7684 TY

145

732TZ

Page 15: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 15

2) Array Operations (element-by-element)

• Scalar-Array Operations

- mathematical operations between a scalar and matrix are performed on eachelement within the matrix (element-by-element).

- addition (+), subtraction (-), and multiplication (*) are performed on each element

ex) >> A+1 → >> 1+A →

>> A-1 →

>> A*2 → >> 2*A →

12108

642A

13119

753

1197

531

242016

1284

Page 16: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 16

2) Array Operations (element-by-element)

• Scalar-Array Operations

- division requires that the Array be the numerator

- both left and right scalar division works

ex) >> A/2 → >> 2\A →

12108

642A

654

321

Page 17: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 17

2) Array Operations (element-by-element)

• Array Addition (or element-by-element addition)

- when performing mathematical operations with two matrix inputs, care needs to be taken to follow the rules of matrix algebra.

- element-by-element operations are NOT always the same as traditional Matrix operations.

- addition of two matrices requires that the two inputs be of the same size. The output is a matrixof the same size where each element is the sum of the two corresponding locations in the inputs.

ex) >> C = A + B →

12108

642A

1197

531B

231915

1173

)1112()910()78(

)56()34()12(

)()()(

)()()(

232322222121

131312121111

BABABA

BABABAC

Page 18: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 18

2) Array Operations (element-by-element)

• Array Subtraction (or element-by-element subtraction)

- subtraction of two matrices requires that the two inputs be of the same size. The output is a matrixof the same size where each element is the difference of the two corresponding locations in the inputs.

ex) >> C = A - B →

12108

642A

1197

531B

111

111

)1112()910()78(

)56()34()12(

)()()(

)()()(

232322222121

131312121111

bababa

bababaC

Page 19: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 19

2) Array Operations (element-by-element)

• Array Multiplication (or element-by-element multiplication)

- The rules of traditional Matrix multiplication are different than element-by-element multiplication.

- If you wish to perform element-by-element multiplication on two matrices of the same size,you use the operator (.*)

>> F = D .* E →

75

31D

86

42E

5630

122

)8)(7()6)(5(

)4)(3()2)(1(

22222121

12121111

2221

1211

2221

1211

eded

eded

ee

ee

dd

ddF

Page 20: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 20

2) Array Operations (element-by-element)

• Array Division (or element-by-element multiplication)

- The rules of traditional Matrix division are different than element-by-element division.

- If you wish to perform element-by-element division on two matrices of the same size,you use the operator (./) or (.\)

>> F = D ./ E → >> F = E .\ D →

75

31D

86

42E

875.0833.0

750.0500.0

8/76/5

4/32/1

//

///

22222121

12121111

2221

1211

2221

1211

eded

eded

ee

ee

dd

ddF

Page 21: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 21

2) Array Operations (element-by-element)

• Array Exponentiation (or element-by-element exponentiation)

- The rules of traditional Matrix exponentiation are different than element-by-element exponentiation.

- If you wish to perform element-by-element exponentiation on a matrixyou use the operator (.^)

>> F = A .^ 3 →

12108

642A

1728100512

216648

12108

642333

333

F

Page 22: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 22

3) Matrix Operations

• Matrix Multiplication (formal definition)

- The rules of Matrix multiplication is that for C=AB:

- that the number of columns in A is equal to the number or rows in B. - the product will be a matrix with the same # of rows in A and same # of columns in B

i.e.,

- If the inputs to matrix-matrix multiplication follow these size rules, the multiplication operatoris simply (*)

>> A*y →

323222121

313212111

3

2

1

232221

131211

bababa

bababa

b

b

b

aaa

aaaABC

12108

642A

4

2

8

y

92

32

)4)(12()2)(10()8)(8(

)4)(6()2)(4()8)(2(

Page 23: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 23

3) Matrix Operations

• Matrix Division

- The rules of Matrix division are more complicated and covered in Chapter 6 of the text.

- The operators for Matrix division are (/) and (\)

- There are a variety of conditions that must be met in order for this division to work properly

(more later)

Page 24: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 24

3) Matrix Operations

• Matrix Exponentiation

- Matrix exponentiation is defined as repeatedly multiplying the matrix by itself.

i.e.,

- This requires that the Matrix be a square (i.e., m = n)

>> S^2 →

Notice this is NOT simply raising each element to a power of 2

AAAA

AAA

3

2

43

21S

2215

107

Page 25: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 25

4) Polynomial Operations

• Polynomials

- Polynomials are entered into Matlab using a Row Vector

- Each of the entries in the row vector represents the coefficients of the terms in the polynomial

- The coefficients are entered with the highest order on the left and the lowest on the right.

Ex)

would be entered as: >> [3 1 -10 5]

- polynomial terms that don’t exists are entered with a coefficient of 0.

Ex)

would be entered as: >> [22 0 0 1 0]

5103 23 xxx

xx 422

Page 26: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 26

4) Polynomial Operations

• Polynomial Operations

- Matlab has built in operations to evaluate polynomials:

roots(a) % returns the roots of a polynomial

poly(x) % computes the coefficients of a polynomial give the roots

polyval(a,x) % evaluates the polynomial (a) at specified values of the % independent variable (x)

Page 27: Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element

Introduction to MatlabModule #2Page 27

Lab 2 Exercise Problems

- For each of these exercises, you will create a script file. Your script file will perform the calculations and then display the answers to the workspace.

- Create a directory on your Z drive called “Z:\Matlab_Course\Lab02”

- Change your pwd to “Z:\Matlab_Course\Lab02” (>> cd Z:\Matlab_Course\Lab02)

- Perform the following exercises:

2.1 - Create a script file called Lab02_2d1a.m - Print a comment to the screen for each solution using the command disp

2.2 - Create a script file called Lab02_2d2.m

2.3 - Create a script file called Lab02_2d3.m

2.8 - Create a script file called Lab02_2d8.m

2.9 - Create a script file called Lab02_2d9.m

2.13 - Create a script file called Lab02_2d13.m