>> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 arrays x and y are one dimensional arrays called...

34
>> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors In MATLAB all variables are arrays. They allow functions with many values to be described.

Upload: joleen-simmons

Post on 20-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

>> x = [1 2 3 4 5];y = 2*x

y =

2 4 6 8 10

Arrays

x and y are one dimensional arrays called vectors.

In MATLAB all variables are arrays. They allowfunctions with many values to be described.

Page 2: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

EGR 106 – Week 2 – Arrays

Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays Arrays chapter 2, pages 33 - 50 Scripts chapter 4, pages 85 - 93

Page 3: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Recall from Last Week

Variables: placeholders for numerical data– equal sign is an assignment operator

c = 7.5 c = c + 1– naming restrictions (not pi, etc. ) – can be complex valued ( x = 3 + i 7 )

Basic math on numbers and variables:

Precedence ( ) ^ * / + -

Page 4: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Names for special sizes– scalar: 1 x 1 array 4 or [4]

– row vector: 1 x C array

[ 9 7 5 4 2 ] is a 1 x 5 row vector

– column vector: R x 1 array

is a 3 x 1 column vector

134

Page 5: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Uniformly Spaced Vectors

Colon operatorfirst : increment : maximum

yields a row vector of equally spaced values– examples:

0 : 2 : 10 [ 0 2 4 6 8 10 ] 1 : 5 [ 1 2 3 4 5 ]

7 : -2 : -3 [ 7 5 3 1 -1 -3 ] 1 : 2 : 8 [ 1 3 5 7 ]

– default for increment is 1 Note – does not hit 8!!

Page 6: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Arrays

Fundamental data unit in Matlab– all variables are stored as arrays

Data values organized into rows and columns

– numeric or alphanumeric entries

4 5 3 91 0 4 6 6 2 01 8 -3 2 0

y ie ld =

M a rtyJ a m e sB o b

na m e =

Page 7: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Array Construction

Direct specification:– Name followed by an equal sign ( = ),

just like variables– List values within a pair of brackets ( [ ] )– Enter data one row at a time

left to right, top to bottom order space or comma between the values rows separated by semicolons or the enter key

Page 8: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Size or dimension of an array:– number of rows and columns– written as R by C or R x C

where R = number of rows

C = number of columns

e.g.

yield is 3 by 4

test is 1 by 5

4 5 3 91 0 4 6 6 2 01 8 -3 2 0

y ie ld =

4 5 3 5 0 te s t =

Page 9: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Building Arrays

>>a = [1 2 3; 4 5 6; 7 8 9; 10 11 12] >>a = [1:3; 4:6; 7:9; 10:12]

Page 10: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

– Can use simple math operations as well as numerics as the entries:

– Note the common format of all entries in the response (exp(1) = e = 2.71828, log10(100) = 2, 2-12 = 0.00024414)

– MATLAB scales the exponent to the largest entry !!

Page 11: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

– This scaling is sometimes deceptive:

Not really zero

Really zero

Page 12: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Concatenation – gluing arrays together

if a = [ 1 2 3 ] b = [ 4 5 6 ]

– Attaching left to right – use a comma [ a, b ]

– Attaching top to bottom – use a semicolon

[ a; b ] 1 2 3 4 5 6

1 2 3 4 5 6

semicolon

comma

Page 13: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

– Note that sizes must match for this to work:

if a = [ 1 2 3 ] then

[ a, b ] = ?? [ a; b ] = ??

– Size needs for concatenation:# of rows the same for side by side (comma)# of columns the same for top to bottom

(semicolon)

4 51 0 4

b =

Page 14: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Addressing and Sub-Arrays

How to indicate a particular element within an array:

– use parentheses after the array name– list desired row, comma, desired column– e.g. yield(2,4)

4 5 3 91 0 4 6 6 2 01 8 -3 2 0

y ie ld =

Page 15: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

How About More than One Entry?

Can specify a rectangular sub-array– again, use parenthesis after the array name– list desired rows, comma, desired columns

as a vector, typically in brackets – e.g. yield([1 2],[3 4])

4 5 3 91 0 4 6 6 2 01 8 -3 2 0

y ie ld =

Page 16: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Used to read a value from an array (right hand side of = )

Page 17: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Addressing Errors

Page 18: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Things that do Work

Single indexing of matrices counts down columns, starting at the top left

Page 19: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Some Useful Array Operators

Transpose (single quote symbol ' )– switches rows and columns

Page 20: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Useful Array Functions

length(A) is the number of elements in the vector A

[m n] = size(A), where A is a matrix with m rows and n columns

ones(n) is an n x n matrix of ones zeros(n) is an n x n matrix of zeros

Page 21: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

CHANGE THE MATRIX

sample =

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

>> sample(1,3)=9

sample =

1 2 9 4 5 6 7 8 9 10 11 12 13 14 15 16

Page 22: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Used to read a sub-array ( rhs of =)

Note – scalar row choice does not need brackets!

Page 23: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Character Arrays

Rows of the array are strings of alphanumeric characters, one array entry per character

Enter using a single quotation mark ( ' ) at each end

Page 24: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Assigning values with too large an index just grows the array

Page 25: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Scalars work for sub-array replacement – they just scale up to the right size

Page 26: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Replacing with a null matrix is the same as deleting – but it only works for entire rows or columns

Page 27: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Rules of the road for arrays:Symbols to use:

brackets to glue elements together to make an array (left to right or top to bottom)comma (or space) and semicolon (or enter) for separating column/row elementsparentheses after the array name for addressing

Be careful to match array sizesRemember – rows first, then columns in addressing

Page 28: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Scripts – Simple Programs

So far, commands have been typed in the command window: – Executed by pressing “enter”– Edited using the arrow keys or

the history window

Page 29: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Script (m-file) Concept

A file containing Matlab commands – Can be re-executed – Is easily changed/modified or e-mailed to someone

Commands are executed one by one sequentially– File is executed by typing its name (without .m)– Results appear in the command window (or use ; )

Can be created using any text editor – .m extension– Listed in Current Directory window

Page 30: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Sample Scripts

Page 31: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions
Page 32: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

DIFFUSION

Diffusion – is the movement of matter driven by

chemical and thermal processes such as

concentration gradients and heating. Both are

needed as it is an activation controlled

process.

Atoms will diffuse down a concentration gradient

provided they have overcome the

activation energy needed for the process.

Copper atoms will diffuse into the

Nickel until an equal concentration is

Achieved. Remember that Cu-Ni system

Is one of complete solid solubility.

Position

Cu

Page 33: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

Practical Example

Decarburization at 1200F after quench crack in material. The crack left enough open surfaceFor the carbon to diffuse out and leave a ferrite layer either side of the crack.

Page 34: >> x = [1 2 3 4 5]; y = 2*x y = 2 4 6 8 10 Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions

ARRAYS FOR DIFFUSION

DIFFUSION RATE AGAINST TEMPERATURE

STRENGTH AGAINST CARBON CONTENT