71
Review of MATLAB The basics of MATLAB Manipulating matrices Built-in functions Plotting Functions in Matlab Mathematical operations and system of eqs. NOTE: Covers Chapts. 1-6, but you should be pretty good if you mainly focus on the first 4 chapters Slides most important: 1-48, 53-57, and 59

Review of MATLAB

  • Upload
    tegan

  • View
    50

  • Download
    2

Embed Size (px)

DESCRIPTION

Review of MATLAB. The basics of MATLAB Manipulating matrices Built-in functions Plotting Functions in Matlab Mathematical operations and system of eqs . NOTE: Covers Chapts . 1-6, but you should be pretty good if you mainly focus on the first 4 chapters - PowerPoint PPT Presentation

Citation preview

Page 1: Review of  MATLAB

Review of MATLAB • The basics of MATLAB• Manipulating matrices• Built-in functions• Plotting• Functions in Matlab• Mathematical operations and system

of eqs.

NOTE: Covers Chapts. 1-6, but you should be pretty good if you mainly focus on the first 4 chapters

Slides most important: 1-48, 53-57, and 59

Page 2: Review of  MATLAB

The Basic Data Type in MATLAB: Matrices

Group of numbers arranged into rows and columnsSingle Value (Scalar) Matrix with one row and one column

Vector (One dimensional matrix) One row or one column

Matrix (Two dimensional)

Page 3: Review of  MATLAB

To create a row vector, enclose a list of values in brackets

Page 4: Review of  MATLAB

You may use either a space or a comma as a “delimiter” in a row vector

Page 5: Review of  MATLAB

Use a semicolon as a delimiter to create a new row

Page 6: Review of  MATLAB

Shortcuts• While a complicated matrix might have to be

entered by hand, evenly spaced matrices can be entered much more readily. The command 

b= 1:5

or the command

b = [1:5]

both return a row matrix 

Page 7: Review of  MATLAB

The default increment is 1, but if you want to use a different increment put it between the first and final values

Page 8: Review of  MATLAB

To calculate spacing between elements use linspace

initial value in the array

final value in the array

number of elements in the array

Page 9: Review of  MATLAB

“What’s the output”

>>A = [1 2 3; 3 5 6]

This statement stores the matrix:

1. A=1 2 3

3 5 6

2. A= 1 2 3 3 5 6

3. A=

1 3

2 5

3 6

4. A= 1 2 3;3 5 6

Page 10: Review of  MATLAB

What’s the output?

>>A = [0:0.1:0.5]

This statement stores the matrix:

1. A= 0 0.1 0.2 0.3 0.4 0.5 2. A=

3. A= 4. A =

0

0 0.1 0.5 0.1 0.5

Page 11: Review of  MATLAB

What’s the output?

>>A = 5*[0:0.1:0.5]

This statement stores the matrix:

1. A= 0 0.5 1.0 1.5 2.0 2.5 2. A=

3. A= 4. A =

0

0 0.1 0.5 0.1 0.5

Page 12: Review of  MATLAB

What’s the output?

>>A = linspace(0,10,3)

This statement stores the matrix:

1. A= 0 5 10 2. A=

3. A= 4. A =

0 10 3

0 3 6 9 0 10 10

Page 13: Review of  MATLAB

Manipulating Matrices

• Defining matrices• A matrix can be defined by typing in a list of numbers

enclosed in square brackets.• The numbers can be separated by spaces or

commas.• New rows are indicated with a semicolon.

A = [ 3.5 ];B = [1.5, 3.1]; or B =[1.5 3.1];C = [-1, 0, 0; 1, 1, 0; 0, 0, 2];

Page 14: Review of  MATLAB

Manipulating Matrices

• Defining matrices• Define a matrix by using another matrix that has

already been defined.

• Reference an element in a matrix• Both row and column indices start from 1.

B = [1.5, 3.1]; S = [3.0, B]

S = [3.0, 1.5, 3.1]; T = [1 2 3; S]

S = 3.0 1.5 3.1 T = 1 2 3 3.0 1.5 3.1

S(2)

T(2, 2)

T(4)

The element value on row 2 and column 2 of matrix T

Count down column 1, then down column 2, and finally down column 2 to the correct element of matrix T.

Page 15: Review of  MATLAB

Manipulating Matrices

• Change values in a matrix• S(2) = -1.0;

changes the 2nd value in the matrix S

from 1.5 to -1.0

• Extend a matrix by defining new elements.

S(4) = 5.5;Extend the matrix S to four elements instead of three.

S(8) = 9.5;Matrix S will have eight values, and the values of S(5), S(6), S(7) will be set to 0.

T(3, 3) = 10;T(4, 5) = 20;

Page 16: Review of  MATLAB

Manipulating Matrices

• Using the colon operator• Define an evenly spaced matrix

H = 1:8 --- The default spacing is 1

time = 0.0:0.5:3.0 --- The middle value becomes the spacing.

• Extract data from matrices x = M( :, 1) --- extract column 1 from matrix M

y = M( :, 4) --- extract column 4 from matrix M

z = M(2, : ) --- extract row 2 from matrix M

a = M(2:3, : ) --- extract rows 2 and 3 from matrix M

b = M( :, 2:4) --- extract column 2 to column 4 from matrix M

c = M(2:3, 3:5) --- extract not whole rows or whole columns

from matrix M

• Converts a two dimensional matrix to a single column M( : )

M = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7

Page 17: Review of  MATLAB

What’s does B store?

>>A = [5 6 ; 7 8]>>B = A(:,2)

This statement stores the matrix:

1. B= 6 8

2. B= 7 8

3. B= 5 7 4. B= 7 8

Page 18: Review of  MATLAB

NEXT TOPIC: MATLAB BUILT-IN FUNCTIONS

Page 19: Review of  MATLAB

MATLAB uses function names consistent with most major programming languages

For example sqrt sin cos log

Page 20: Review of  MATLAB

Function Input can be either scalars or matrices

Page 21: Review of  MATLAB

Trigonometric Functions

sin(x) sinecos(x) cosinetan(x) tangentasin(x) inverse sinesinh(x) hyperbolic sineasinh(x) inverse hyperbolic sinesind(x) sine with degree inputasind(x) inverse sin with degree output

Page 22: Review of  MATLAB

Data Analysis

max(x)min(x)mean(x)median(x)sum(x)prod(x)sort(x)

Page 23: Review of  MATLAB

When x is a matrix, the max is found for each column

Page 24: Review of  MATLAB

max value

element number where the max value occurs

Returns both the smallest value in a vector x and its location in vector x.

Page 25: Review of  MATLAB

Vector of maximums

Vector of row numbers

Returns a row vector containing the minimum element from each column of a matrix x, and returns a row vector of the location of the minimum in each column of matrix x.

Page 26: Review of  MATLAB

What does B store?

>>A = [1 2 3; 3 5 6;1 2 1]>>B = max(A)

c) 6 5 6

b) 3 6 2a) 6

d) 3 5 6

Page 27: Review of  MATLAB

What does C store?

>>A = [1 2 3; 3 5 6;1 2 1]>>B = max(A)>>C=max(B)

c) 6 5 6

b) 3 6 2a) 6

d) 3 5 6

Page 28: Review of  MATLAB

What does B and C store?

>>A = [1 2 3; 3 5 6;1 2 1]>>[B,C] = max(A)

c) B = 6 5 6 C = 1 1 1

b) B = 3 6 2 C = 3 3 2

a) B= 6 C = 4

d) B = 3 5 6 C = 2 2 2

Page 29: Review of  MATLAB

What does D and E store?

>>A = [1 2 3; 3 5 6;1 2 1]>>[B,C] = max(A)>>[D,E] = max(B)

c) D = 6 E = 2

b) D = 6 E = 3

a) D= 6 E = 4

d) D = 3 5 6 E = 3

Page 30: Review of  MATLAB

What is B?

>>A = [4 4 4 1 3 1 1]>>B = median(A)

c) B = 7

b) B = 3a) B = 4

d) B = 1

Page 31: Review of  MATLAB

Will this piece of code work? If not, why?

>>A = [1 2 3; 3 5 6]>>B = [2 4 6; 6 10 18]>>C = B*A

This code will not work because ‘*’ is used for matrixmultiplication and in matrix multiplication you need the same number of columns in B as rows in A.

Page 32: Review of  MATLAB

Will this piece of code work? If not, why?

>>A = [1 2 3; 3 5 6]>>B = [2 4 6; 6 10 18]>>C = B.*A

This code will work because ‘.*’ means multiply eachelement of B with the same element of A. In order touse ‘.*’, A and B need to be the same dimensions.

Page 33: Review of  MATLAB

What does C store?

>>A = [1 2 3; 3 5 6;1 2 1]>>B = [2 4 ; 6 1]>>C = B.*A(2:3,1:2)

c) 4 12 30 6

b) 2 8 18 5

a) 6 20 6 2

d) 10 24 12 1

Page 34: Review of  MATLAB

PLOTTING IN MATLAB

Page 35: Review of  MATLAB

Define x and y and call the plot function

Page 36: Review of  MATLAB

Engineers always add …

Title title(‘y = cos(x)’)

X axis label, complete with units xlabel(‘x-axis’)

Y axis label, complete with units ylabel(‘y-axix’)

Often it is useful to add a grid grid on

Single quotes are used.

Page 37: Review of  MATLAB

Line, Color and Mark Style

You can change the appearance of your plots by selecting user defined line styles mark styles colorTry using

help plotfor a list of available styles

Page 38: Review of  MATLAB

Specify your choices in a string

For exampleplot(x, y, ‘:ok')

strings are identified with single quotes the : means use a dotted line the o means use a circle to mark each

point the letter k indicates that the graph

should be drawn in black (b indicates blue)

Page 39: Review of  MATLAB

Available choicesTable 5. 2 Line, Mark and Color Options

Line Type Indicator Point Type Indicator Color Indicator

solid - point . blue b

dotted : circle o green g

dash-dot -. x-mark x red r

dashed -- plus + cyan c

star * magenta m

square s yellow y

diamond d black k

triangle down v

triangle up ^

triangle left <

triangle right >

pentagram p

hexagram h

Page 40: Review of  MATLAB

specify the drawing parameters for each line after the ordered pairs that define the line

Page 41: Review of  MATLAB

Subplots

The subplot command allows you to subdivide the graphing window into a grid of m rows and n columnssubplot(m,n,p)

rows columns location

Page 42: Review of  MATLAB

subplot(2,2,1)

2 rows

2 columns

1 2

3 4

-20

2-2

02

-5

0

5

x

Peaks

y

Page 43: Review of  MATLAB

2 rows and 1 column

Page 44: Review of  MATLAB

Other Types of 2-D Plots

Polar PlotsLog/semi-log (semilogx,semilogy,etc.)

Bar GraphsPie ChartsHistogramsX-Y graphs with 2 y axesFunction Plots

Page 45: Review of  MATLAB

CREATING FUNCTIONS IN MATLAB

Page 46: Review of  MATLAB

A simple function (poly2)

Save the file as poly2.m (same as the name of the function)

Page 47: Review of  MATLAB

The function is available from the command window or from other M-file programs

Page 48: Review of  MATLAB

Comments

You should comment functions, just as you would any computer codeThe comment lines immediately after the first line are returned when you query the help function

Page 49: Review of  MATLAB

Using find and logical operators in Matlab

Page 50: Review of  MATLAB

Logical Operators

& and~ not| or

Page 51: Review of  MATLAB

find

The find command searches a matrix and identifies which elements in that matrix meet a given criteria.

Page 52: Review of  MATLAB

index numbers

element values

Page 53: Review of  MATLAB

Mathematical manipulations etc…

Page 54: Review of  MATLAB

MATLAB interprets * to mean matrix multiplication. The arrays a and b are not the correct size for matrix multiplication in this example

Multiplication between arrays is performed on corresponding elements if the .* operator is used

Page 55: Review of  MATLAB

Array Operations (element-by-element)

Array multiplication .*Array division ./Array exponentiation .^

In each case the size of the arrays must match

Page 56: Review of  MATLAB

What’s the output?

>>A = [1 2 3; 3 5 6]>>B = [2 4 6; 6 10 18]>>C = B./A

This statement stores the matrix:

1. C=2 2 2

2 2 3

2. C= 2 2 2 2 2 3

3. C=

2 2

2 2

2 3

4. Can’t divide these matrices

Page 57: Review of  MATLAB

What’s the output?

>>A = [1 2 3; 3 5 6]>>B = [2 4 6; 6 10 18]>>C = B*A

This statement stores the matrix:

1. C=2 2 2

2 2 3

2. C= 2 2 2 2 2 3

3. C=

2 2

2 2

2 3

4. Can’t multiply these matrices: not the correct dimension – A is 2x3 B is 2x3

Page 58: Review of  MATLAB

What is A’?

>> A = [1, 2, 3; 4, 3, 6];>> A’

A. 1 4 2 3 3 6

B. 4 1 3 2 6 3

C. 1 2 3 4 3 6

D. 4 3 6 1 2 3

This is the transpose= the rows and columns are flipped

Page 59: Review of  MATLAB

What’s the output?

>>A = [1 2 3; 3 5 6]>>B = [1 1 1; 1 1 1]>>C = B*A’

This statement stores the matrix:

1. C=6 14

6 14

2. C=

3. C=14 14

6 6

4. Can’t multiply these matrices: not the correct dimension – A is 2x3 B is 2x3

2 2 2

2 2 3

Page 60: Review of  MATLAB

What is C?

>> A = [1, 2, 3];>> B = [4, 2, 1];>> C=dot(A,B)

A. 1 4 2 2 3 1

B. 11

C. 1 2 3 4 2 1

D. 4 8 3

Dot product = sum of the elements in vector A multiplied by same elements in vector B

Page 61: Review of  MATLAB

What is C?

>> A = [1, 2, 3; 3, 2, 1];>> B = [1, 1; 1, 1; 1, 1];>> C=A*B

A. 1 4 2 2 3 1

B. 11

C. 6 6 6 6

D. 4 8 3

Matrix multiplication= dot product of each row of A with each column of B

Page 62: Review of  MATLAB

Matrix Inverse

MATLAB offers two approaches The matrix inverse function

inv(A) Raising a matrix to the -1 power

A-1

Page 63: Review of  MATLAB

A matrix times its inverse is the identity matrix

Equivalent approaches to finding the inverse of a matrix

Page 64: Review of  MATLAB

Not all matrices have an inverse

Called Singular Ill-conditioned matrices

Attempting to take the inverse of a singular matrix results in an error statement

Page 65: Review of  MATLAB

Determinants

Related to the matrix inverseIf the determinant is equal to 0, the matrix does not have an inverseThe MATLAB function to find a determinant is det(A)

Page 66: Review of  MATLAB

What is B?

>> A = [1, 2; 3, 2];>> B=det(A)

A. 0 B. 4

C. -4 D. 8

Don’t have to know the mathematics of the determinent

Page 67: Review of  MATLAB

Solutions to Systems of Linear Equations

3 2 10

3 2 5

1

x y z

x y z

x y z

Page 68: Review of  MATLAB

Using Matrix Nomenclature

111

231

123

A

z

y

x

X

1

5

10

B

and

AX=B

Page 69: Review of  MATLAB

We can solve this problem using the matrix inverse approach

This approach is easy to understand, but its not the more efficient computationally

Page 70: Review of  MATLAB

Matrix left division uses Gaussian elimination, which is much more efficient, and less prone to round-off error

Page 71: Review of  MATLAB

What is X?

>> A = [1, 0; 0, 1];>> B = [5; 2]>> X = inv(A)*B

A. 1 1 B. 5 2

C. 5 1 D. 5 2