Top Banner
1 MATLAB Short Course
42

1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

Dec 27, 2015

Download

Documents

Roy Wells
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

1

MATLAB Short Course

Page 2: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

History of Calculator

2

Page 3: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

3

Introduction to Matlab

• Matlab is short for Matrix Laboratory

• Matlab is also a programming language

Page 4: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

4

Matlab Basics

• Basic data type: Matrix

• A matrix is a two-dimensional array of numbers rows and columns.

• A vector is a one-dimensional matrix. It may have either one row or one column.

925

295

824

A

Page 5: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

5

Toolboxes

Collections of functions to solve problems of several applications.

Control Toolbox Neural Network Toolbox Fuzzy Logic Toolbox Genetic Algorithms Toolbox Communication Toolbox …………..

Page 6: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

6

Main Working Windows

After the “>>” symbol, you can type the commands

Page 7: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

7

Display Windows

Graphic (Figure) Window

Displays plots and graphs

Creates response to graphics commands

M-file editor/debugger window

Create and edit scripts of commands called M-files

Page 8: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

8

Getting Help

To get help:

MATLAB main menu

-> Help

-> MATLAB Help

Page 9: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

9

Getting Help

Type one of the following commands in the command window:

help – lists all the help topics help topic – provides help for the specified topic help command – provides help for the specified command helpwin – opens a separate help window for navigation Lookfor keyword – search all M-files for keyword

Online resource

Page 10: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

10

Variables

Variable names:

Must start with a letter.

May contain only letters, digits, and the underscore “_”.

MATLAB is case sensitive, for example one & ONE are different variables.

MATLAB only recognizes the first 31 characters in a variable name.

Assignment statement:

Variable = number; >> t=1234

Variable = expression; >>t=a+b

Page 11: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

11

Variables

Special variables: ans: default variable name for the result. pi: π = 3.1415926 …… eps: ε= 2.2204e-016, smallest value by which two numbers can differ inf: ∞, infinity NAN or nan: not-a-number

Commands involving variables: who: lists the names of the defined variables whos: lists the names and sizes of defined variables clear: clears all variables clear name: clears the variable name clc: clears the command window clf: clears the current figure and the graph window

Page 12: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

12

Calculations at the Command Line

» a = 2;

» b = 5;

» a^b

ans =

32

» x = 5/2*pi;

» y = sin(x)

y =

1

» z = asin(y)

z =

1.5708

» a = 2;

» b = 5;

» a^b

ans =

32

» x = 5/2*pi;

» y = sin(x)

y =

1

» z = asin(y)

z =

1.5708

Results assigned to “ans” if name not specified

() parentheses for function inputs

Semicolon suppresses screen output

MATLAB as a calculator Assigning Variables

A Note about Workspace:Numbers stored in double-precision floating point format

» -5/(4.8+5.32)^2ans = -0.0488» (3+4i)*(3-4i)ans = 25» cos(pi/2)ans = 6.1230e-017» exp(acos(0.3))ans = 3.5470

» -5/(4.8+5.32)^2ans = -0.0488» (3+4i)*(3-4i)ans = 25» cos(pi/2)ans = 6.1230e-017» exp(acos(0.3))ans = 3.5470

9

Page 13: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

13

A = [ 100 0 99 ; 7 -1 -25 ]

The semi colon separates the rows of a matrix.

To print the matrix, type the name.

A=100 0 997 -1 -25

Creating Matrices

Page 14: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

14

To print the element in the 2nd row, 3rd column, type:A(2, 3 )

Matlab prints ans = -25

Accessing Matrix Elements

By entering x = A(1,1) + A( 2,3)

Matlab returns x = 75

2517

90100

2

1

321

A

Page 15: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

15

To change an entry, enter the new value:A(2,3) = -50 Matlab prints the new matrix A

A = 100 0 99 7 -1 -50

Changing Matrix Elements

Page 16: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

16

Numerical Array Concatenation

» a=[1 2;3 4]

a =

1 2

3 4

» cat_a=[a, 2*a; 3*a, 4*a; 5*a, 6*a]cat_a = 1 2 2 4 3 4 6 8 3 6 4 8 9 12 12 16 5 10 6 12 15 20 18 24

» a=[1 2;3 4]

a =

1 2

3 4

» cat_a=[a, 2*a; 3*a, 4*a; 5*a, 6*a]cat_a = 1 2 2 4 3 4 6 8 3 6 4 8 9 12 12 16 5 10 6 12 15 20 18 24

Use [ ] to combine existing arrays as matrix “elements”

Row separator:semicolon (;)

Column separator:space / comma (,)

Use square brackets [ ]

4*a

Page 17: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

17

The : operatorx = 1 : 7 or x = [ 1 : 7 ] creates the same vector

as the command x = [ 1 2 3 4 5 6 7]

y = 0 : 3 : 12 is the same asy = [ 0 3 6 9 12 ]

y = 0 : 3 : 11 is the same asy = [ 0 3 6 9 ]

z = 15 : -4 : 3 is the same as z = [ 15 11 7 3 ]

w = 0 : 0.01 : 2 is the same as w = [ 0 0.01 0.02 ... 1.99 2.00 ]

Page 18: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

18

Deleting Rows and Columns

» A=[1 5 9;4 3 2.5; 0.1 10 3i+1]

A =

1.0000 5.0000 9.0000

4.0000 3.0000 2.5000

0.1000 10.0000 1.0000+3.0000i

» A(:,2)=[]

A =

1.0000 9.0000

4.0000 2.5000

0.1000 1.0000 + 3.0000i

» A(2,2)=[]

??? Indexed empty matrix assignment is not allowed.

» A=[1 5 9;4 3 2.5; 0.1 10 3i+1]

A =

1.0000 5.0000 9.0000

4.0000 3.0000 2.5000

0.1000 10.0000 1.0000+3.0000i

» A(:,2)=[]

A =

1.0000 9.0000

4.0000 2.5000

0.1000 1.0000 + 3.0000i

» A(2,2)=[]

??? Indexed empty matrix assignment is not allowed.

Page 19: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

19

Array Subscripting / Indexing

4 10 1 6 2

8 1.2 9 4 25

7.2 5 7 1 11

0 0.5 4 5 56

23 83 13 0 10

1

2

3

4

5

1 2 3 4 51 6 11 16 21

2 7 12 17 22

3 8 13 18 23

4 9 14 19 24

5 10 15 20 25

A =

A(3,1)A(3)

A(1:5,5)A(:,5) A(21:25)

A(4:5,2:3)A([9 14;10 15])

A(1:end,end) A(:,end)A(21:end)’

Page 20: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

20

Special Matrices and Commands

Z = zeros( 4, 5 ) Z =

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

K = ones( 4, 5 )is a matrix of 1’s

Id = eye( 4, 5 ) Id =

1 0 0 0 0

0 1 0 0 0

0 0 1 0 0

0 0 0 1 0

Page 21: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

21

Special Matrices and Commands

The transpose operator is ’transZ = Z’

Z =

1 2 3

4 5 6

transZ =

1 4

2 5

3 6

Page 22: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

22

>> A = [ 100 0 99 ; 7 -1 -25 ]

A =

100 0 99

7 -1 -25

>> size(A) a vector of rows and columns in A

ans =

2 3

>> length(A)

ans =

3

Special Matrices and Commands

Page 23: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

23

>> A = [ 100 0 99 ; 7 -1 -25 ]

A =

100 0 99

7 -1 -25

>> v=diag(A) a vector with the diagonal elements of A

v =

100

-1

>> B=diag(v) a matrix with v on the diagonal and 0’s off the diagonal

B =

100 0

0 -1

Special Matrices and Commands

The diag command shows that Matlab commands can depend on the nature of the arguments.

Page 24: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

24

Matrix Manipulation Functions

• zeros: Create an array of all zeros

• ones: Create an array of all ones

• eye: Identity Matrix

• rand: Uniformly distributed random numbers

• diag: Diagonal matrices and diagonal of a matrix

• size: Return array dimensions • fliplr: Flip matrices left-right

• flipud: Flip matrices up and down

• repmat: Replicate and tile a matrix

Page 25: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

25

Matrix Manipulation Functions• transpose (’): Transpose matrix • rot90: rotate matrix 90

• tril: Lower triangular part of a matrix

• triu: Upper triangular part of a matrix

• cross: Vector cross product

• dot: Vector dot product

• det: Matrix determinant

• inv: Matrix inverse

• eig: Evaluate eigenvalues and eigenvectors• rank: Rank of matrix

Page 26: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

26

Arithmetic OperationsA, B are matrices, x is a scalar

x*A multiplies each entry of A by x

x + A adds x to each entry of Asame as x*ones(size(A)) + A

x – A same as x*ones(size(A)) – A

A + B standard matrix addition

A – B standard matrix subtraction

Page 27: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

27

Arithmetic Operations (cont.)

A*B Standard matrix multiplicationcols A = rows B

A.*B element-wise multiplicationsize( A) = size(B)

 A.\B divide elements of B by those in A

A./B divide elements of A by those in B

Page 28: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

28

Matrix Multiplication

» a = [1 2 3 4; 5 6 7 8];

» b = ones(4,3);

» c = a*b

c =

10 10 10 26 26 26

» a = [1 2 3 4; 5 6 7 8];

» b = ones(4,3);

» c = a*b

c =

10 10 10 26 26 26

[2x4]

[4x3]

[2x4]*[4x3] [2x3]

a(2nd row).b(3rd column)

» a = [1 2 3 4; 5 6 7 8];

» b = [1:4; 1:4];

» c = a.*b

c =

1 4 9 16 5 12 21 32

» a = [1 2 3 4; 5 6 7 8];

» b = [1:4; 1:4];

» c = a.*b

c =

1 4 9 16 5 12 21 32 c(2,4) = a(2,4)*b(2,4)

Array Multiplication

Page 29: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

29

Linear Equations

Example: a system of 3 linear equations with 3 unknowns (x1, x2, x3) 3 x1 + 2x2 - x3 = 10 - x1 + 3x2 + 2x3 = 5 x1 - 2x2 - x3 = -1

Let:

1

2

3

3 2 1 10

1 3 2 5

1 1 1 1

x

x

x

A x b

Then, the system can be described as:

Ax = b

Page 30: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

30

Complex Numbers

Matlab works with complex numbers, sosqrt( -3 ) is a valid command.

Lowercase i is reserved for the special value sqrt(-1)

Page 31: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

31

Special Values

pi = 3.14159 ....Inf = infinity (divide by 0)

M = [ pi sqrt(-3); Inf 0]

M =

3.1416 0 + 1.7321i

Inf 0

Page 32: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

32

Linear Equations

Solution by Matrix Inverse:

Ax = b

A-1 Ax = A-1 b

Ax = b

MATLAB:

>> A = [3 2 -1; -1 3 2; 1 -1 -1];

>> b = [10;5;-1];

>> x = inv(A)*b

x =

-2.0000

5.0000

-6.0000

Solution by Matrix Division: Ax = b Can be solved by left division

b\A

MATLAB:

>> A = [3 2 -1; -1 3 2; 1 -1 -1];

>> b = [10;5;-1];

>> x =A \ b

x =

-2.0000

5.0000

-6.0000

Page 33: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

33

Example

Page 34: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

34

Example

Page 35: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

35

Example

P=0, Vz=0, My=0, Vy=-1000 lb, Mz=100,000 lb.in

Page 36: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

36

Elementary Math Function• abs, sign: Absolute value and Signum

Function• sin, cos, asin, acos…: Triangular functions• exp, log, log10: Exponential, Natural and

Common (base 10) logarithm• ceil, floor: Round toward infinities• fix: Round toward zero

Page 37: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

37

Elementary Math Function

round: Round to the nearest integer gcd: Greatest common devisor lcm: Least common multiple sqrt: Square root function real, imag: Real and Image part of complex rem: Remainder after division

Page 38: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

38

Elementary Math Function

• max, min: Maximum and Minimum of arrays• mean, median: Average and Median of arrays • std, var: Standard deviation and variance • sort: Sort elements in ascending order• sum, prod: Summation & Product of Elements• trapz: Trapezoidal numerical integration• cumsum, cumprod: Cumulative sum, product• diff, gradient: Differences and Numerical

Gradient

Page 39: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

39

Polynomials and Interpolation

Polynomials Representing Roots (>> roots) Evaluation (>> polyval) Derivatives (>> polyder) Curve Fitting (>> polyfit) Partial Fraction Expansion

(residue)Interpolation One-Dimensional (interp1) Two-Dimensional (interp2)

Page 40: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

40

polysam=[1 0 0 8];roots(polysam)ans = -2.0000 1.0000 + 1.7321i 1.0000 - 1.7321iPolyval(polysam,[0 1 2.5 4 6.5])ans = 8.0000 9.0000 23.6250 72.0000 282.6250polyder(polysam)ans = 3 0 0[r p k]=residue(polysam,[1 2 1])r = 3 7p = -1 -1k = 1 -2

polysam=[1 0 0 8];roots(polysam)ans = -2.0000 1.0000 + 1.7321i 1.0000 - 1.7321iPolyval(polysam,[0 1 2.5 4 6.5])ans = 8.0000 9.0000 23.6250 72.0000 282.6250polyder(polysam)ans = 3 0 0[r p k]=residue(polysam,[1 2 1])r = 3 7p = -1 -1k = 1 -2

Example

Page 41: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

41

x = [0: 0.1: 2.5];y = erf(x); p = polyfit(x,y,6)p = 0.0084 -0.0983 0.4217 -0.7435 0.1471 1.1064 0.0004

x = [0: 0.1: 2.5];y = erf(x); p = polyfit(x,y,6)p = 0.0084 -0.0983 0.4217 -0.7435 0.1471 1.1064 0.0004

Example

interp1(x,y,[0.45 0.95 2.2 3.0])ans = 0.4744 0.8198 0.9981 NaN

interp1(x,y,[0.45 0.95 2.2 3.0])ans = 0.4744 0.8198 0.9981 NaN

Page 42: 1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

42

Logical Operations

» Mass = [-2 10 NaN 30 -11 Inf 31];» each_pos = Mass>=0each_pos = 0 1 0 1 0 1 1» all_pos = all(Mass>=0)all_pos = 0» all_pos = any(Mass>=0)all_pos = 1» pos_fin = (Mass>=0)&(isfinite(Mass))pos_fin = 0 1 0 1 0 0 1

» Mass = [-2 10 NaN 30 -11 Inf 31];» each_pos = Mass>=0each_pos = 0 1 0 1 0 1 1» all_pos = all(Mass>=0)all_pos = 0» all_pos = any(Mass>=0)all_pos = 1» pos_fin = (Mass>=0)&(isfinite(Mass))pos_fin = 0 1 0 1 0 0 1

= = equal to

> greater than

< less than

>= Greater or equal

<= less or equal

~ not

& and

| or

isfinite(), etc. . . .

all(), any()

find

Note:• 1 = TRUE• 0 = FALSE