egr 115 introduction to computing for engineers matlab basics 3: array operations monday 08 sept...

Post on 18-Jan-2016

218 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

EGR 115 Introduction to Computing for Engineers

MATLAB Basics 3: Array Operations

Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers

Lecture Outline

Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Data files,• Array Operations, and• Some of the built in functions

Slide 2 of 10

MATLAB basicsData files: Saving data

Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Often as engineers we will need to save the results of an analysis task or load data from a test procedure.

• MATLAB allows data to be saved from a session>> save filename var1 var2 var3

o A file called “filename.mat” will be created– This is a compact binary data file (not human readable)

o Var1 and Var2 refer to variables in the workspace

It is also possible to save to an ASCII data file (human readable)

>> save filename.txt var1 var2 -ascii

Slide 3 of 10

Creates the file “my_data.mat” in your

current working directory

MATLAB basicsData files: Loading data from files

Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers

• The load function is the complement of save and can retrieved data from a file>> load filename.mat

• An example:>> a = [1 2 3 4]; % variable 1>> b = [4; 8]; % variable 2>> save my_data a b; % Save variables ‘a’ and ‘b’>> clear all; % Clear the workspace>> load my_data; % Load the variables from the file

o Note that the variables are removed from the workspace and reappear after being loaded!!

Slide 4 of 10

Try saving both a and b to “my_data.txt” as an ascii file – Open the file

MATLAB basicsArray Operations

Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Array Addition (c = a + b) Only makes sense if ‘a’ and ‘b’ are the same size

An Example:

Slide 5 of 10

, , ,i j i j i jc a b

1 2 3 1 1 3

4 5 6 0 1 0

7 8 9 5 2 10

Col indexRow index

2 1 0

4 6 6

2 10 1

MATLAB basicsArray Operations - Multiplication

Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Array Multiplication (c = a * b) Only makes sense if ‘a’ and ‘b’ are compatible

o Number of columns of ‘a’ must be the same as the number of rows of ‘b’– A nXm matrix times a mXr matrix yields a nXr matrix

An Example: (a is a 2X3 matrix and ‘b’ a 3X4, thus ‘c’ is 2X4)

Slide 6 of 10

1 1 3 21 2 3

0 1 0 14 5 6

5 2 10 0

14

14 7

14 7 33

14 7 33 4

14 7 33 4

26 13 72 13

What is the result of: >> c = b * a ???

Create two square matrices ‘a’ & ‘b’: Is a* b = b * a ?

MATLAB basicsArray Operations – Element by Element Multiplication

Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Element by Element Array Multiplication (c = a .* b) Only makes sense if ‘a’ and ‘b’ are exactly the same size

An Example: (a is a 2X3 matrix and ‘b’ a 2X3, thus ‘c’ is 2X3)

Slide 7 of 10

1 2 3 1 3 2

4 5 6 1 0 1

1 6 6

4 0 6

MATLAB basicsArray Operations – Matrix Division?

Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Matrix Division (c = a / b or c = a * inv(b)) Actually performing

o ‘b’ must be square (and non-singular) to be invertible

An Example: (a is a 3X2 matrix and ‘b’ a 2X2, thus ‘c’ is 3X2)

Slide 8 of 10

1 3

1 0b

1

1 20 1

3 41/ 3 1/ 3

5 6

c ab

1 2

3 4

5 6

a

1 0 1

1/ 3 1/ 3b

2 / 3 1/ 3

4 / 3 5 / 3

2 3

What is the result of: >> c = inv(b) * a ???

MATLAB basicsSome of the built in Functions

Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers Slide 9 of 10

MATLAB basicsSome of the built in Functions

Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers Slide 10 of 10

• Some of the built in functions

Next Lecture

Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Intro to Plotting

Slide 11 of 10

top related