vectors, arrays and matrices - physics internal website

31
9/3/2006 Page 1 Physics 241 -- New Mexico Tech Vectors, Arrays and Matrices Richard Sonnenfeld (with some material from W. Palm) ! ! Vectors Vectors Physics definition Physics definition Use in Use in Matlab Matlab Problems Problems ! ! Concept of an Array Concept of an Array ! ! Matrices in Matrices in Matlab Matlab Matrix multiplication Matrix multiplication

Upload: others

Post on 23-Nov-2021

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 1Physics 241 -- New Mexico Tech

Vectors, Arrays and MatricesRichard Sonnenfeld

(with some material from W. Palm)

!! VectorsVectorsPhysics definitionPhysics definitionUse in Use in MatlabMatlabProblemsProblems

!! Concept of an ArrayConcept of an Array!! Matrices in Matrices in MatlabMatlab

Matrix multiplicationMatrix multiplication

Page 2: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 2Physics 241 -- New Mexico Tech

Vectors and relative motionVectors and relative motion

!! To calculate relative To calculate relative motion or velocity, the motion or velocity, the important quantity is a important quantity is a vector difference.vector difference.

!! The result becomes The result becomes independent of the independent of the choice of coordinate choice of coordinate originorigin

1 2r r−v v

1rv

2rv

Origin 1Origin 2

1 2( )r r−v v2r ′v

1r ′v

1 2r r′ ′−v v

:30

Page 3: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 3Physics 241 -- New Mexico Tech

Vector relationships (physics definition)Vector relationships (physics definition)

VectorsVectors!! Allow ready Allow ready

representation of 3 representation of 3 (or more!) (or more!) components at once.components at once.

!! Equations written in Equations written in vector notation are vector notation are more compactmore compact

zdtdzy

dtdyx

dtdx

dtrd ˆˆˆ ++=v

xxxv

=ˆ rrrr vvv ⋅≡≡

∑=

=

≡⋅3

1

)cos(

iii sr

srsr θvv

Page 4: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 4Physics 241 -- New Mexico Tech

Matlab can easily represent vectors as defined in the classical physics sense.

The vector r can be specified by three components: x, y, and z, and can be written as:

r = [x, y, z].

However, MATLAB can use vectors having more than three elements.

r

Page 5: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 5Physics 241 -- New Mexico Tech

To create a row vector, separate the elements by commas. For example,

>>p = [3,7,9]p =

3 7 9

You can create a column vector by using the transpose notation (').Or by separating the elements by semicolons. For example,

>>p = [3,7,9]'p =

379

>>g = [3;7;9]g =

379

Page 6: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 6Physics 241 -- New Mexico Tech

You can create vectors by ''appending'' one vector to another.

For example, if

>> r = [2,4,20] and >> w = [9,-6,3], Then>> u = [r,w]. The result is the vector u = [2,4,20,9,-6,3].You can also create a subset of a vector.Assume we wanted to use the 2nd, 3rd, and 4th elements of u. How could this be done?

Page 7: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 7Physics 241 -- New Mexico Tech

The colon operator (:) easily generates a large vector of regularly spaced elements.

Typing

>>x = [m:q:n]

creates a vector x of values with a spacing q. The first value is m. The last value is n if m - n is an integer multiple of q. If not, the last value is less than n.

Page 8: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 8Physics 241 -- New Mexico Tech

For example>> x = [0:2:8]x =

[0,2,4,6,8]

>> x = [0:2:7]x =

[0,2,4,6]

To create row vector z consisting of the values from 5 to 8 in steps of 0.1, type z = [5:0.1:8].

If the increment q is omitted, then q=1. y = [-3:2]y = [-3,-2,-1,0,1,2].

Page 9: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 9Physics 241 -- New Mexico Tech

The linspace command also creates a linearly spaced row vector, but instead you specify the number of values rather than the increment.

The syntax is linspace(x1,x2,n), where x1 and x2are the lower and upper limits and n is the number of points.

For example, linspace(5,8,31) is equivalent to [5:0.1:8].

If n is omitted, the spacing is 1.

Example:t=linspace(0,5);x=3*t-4.9*t.^2;plot(t,x)

Page 10: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 10Physics 241 -- New Mexico Tech

Matlab flexibly and cleverly handles row and column vectors and matrices of different sizes.

Most operations are defined on vectors or matrices of the same size. For example, adding a row vector to acolumn vector causes an error.

>> a=[0:10]; b=a’+7; c=a+b

??? Error using ==> +Matrix dimensions must agree.

Get used to looking for these – they are one of the most common causes of strange behavior.

Potential confusionPotential confusion

Page 11: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 11Physics 241 -- New Mexico Tech

“Array” (noun) In programming languages, a name given to sequences of variables that have a relationship to eachother.

Examples:Successive points in a scientific data set. Adjacent values of a mathematical function.

Successive array elements are often allocated physically adjacent locations in computer memory.

In Matlab, arrays are referred to as “Vectors” or “Matrices”.

Programming concept called “Array”Programming concept called “Array”

Page 12: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 12Physics 241 -- New Mexico Tech

The linspace command also creates a linearly spaced row vector, but instead you specify the number of values rather than the increment.

The syntax is linspace(x1,x2,n), where x1 and x2are the lower and upper limits and n is the number of points.

For example, linspace(5,8,31) is equivalent to [5:0.1:8].

If n is omitted, the spacing is 1.

Example:t=linspace(0,5);x=3*t-4.9*t.^2;plot(t,x)

Vectors and relative motionVectors and relative motion

Page 13: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 13Physics 241 -- New Mexico Tech

A car and train move as shown i) Express vTrain and vCar as

vectors.ii) Calculate their relative

velocity vector as well as, its magnitude and direction.

iii) Assume both car and train are 60 miles from the origin and express the distance between them as a time-dependent vector.

iv) Calculate the magnitude of the distance vs. time

v) Plot it and use the plot to determine the time of closest approach.

mph

Relative motion Relative motion (RS5.1)(RS5.1)

Page 14: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 14Physics 241 -- New Mexico Tech

Three types of vector (and matrix) multiplication

Element-by-element>> x = [2 3 5 1]>> x.*x (or x.^2)ans =[4 9 25 1]

Inner (dot) product>> x = [2 3 5 1]

>> x*x’ans =

39

Outer-product (not too useful)>> x = [2 3 5 1]>> x’*xans =

4 6 10 26 9 15 310 15 25 52 3 5 1

Page 15: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 15Physics 241 -- New Mexico Tech

Three types of vector (and matrix) multiplication

Inner (dot) product>> x = [2 3 5 1]>> x’ ans = 2 3 5 1

>> x*x’ans =

39

Same as matrix product from math[2 3 5 1] [2

3 = 395 1]

Page 16: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 16Physics 241 -- New Mexico Tech

Magnitude, Length, and Absolute Value of a Vector

In physics, length, magnitude and absolute value of a vector all mean the same thing. In Matlab, they do not.

Magnitude of a vector x having elements x1, x2, …, xn is a scalar, given by √(x1

2 + x22 + … + xn

2), and is the same as the physics definition of magnitude.

length command gives the number of elements in the vector.

The absolute value of a vector x is a vector whose elements are the absolute values of the elements of x.

Page 17: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 17Physics 241 -- New Mexico Tech

For example:>> x = [2,-4,5]

• its length is 3 (length(x))

• its magnitude is √[22 + (–4)2 + 52] = 6.7082 (sqrt(x’*x))

• its absolute value is [2,4,5] (abs(x)).

Page 18: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 18Physics 241 -- New Mexico Tech

Vector Relationships Vector Relationships “The dot“The dot--product trick”product trick”

Given vectors Given vectors AA and and B B which correspond to which correspond to symmetry axes of a crystal:symmetry axes of a crystal:

Calculate:Calculate:

Where theta is angle between A and BWhere theta is angle between A and B

xA ˆ2=v

zyxB ˆ3ˆ3ˆ3 ++=v

θ,, BAvv

A

B

Page 19: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 19Physics 241 -- New Mexico Tech

Matrices

A matrix has multiple rows and columns. For example, the matrix

has four rows and three columns.

Vectors are special cases of matrices having one row or one column.

M =2 4 10

16 3 7 8 4 9 3 12 15

Page 20: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 20Physics 241 -- New Mexico Tech

Creating Matrices

If the matrix is small you can type it row by row, separating the elements in a given row with spaces or commas and separating the rows with semicolons. For example, typing

>>A = [2,4,10;16,3,7];

creates the following matrix:

2 4 1016 3 7

Remember, spaces or commas separate elements in different columns, whereas semicolons separate elements in different rows.

A =

Page 21: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 21Physics 241 -- New Mexico Tech

Creating Matrices from Vectors

Suppose a = [1,3,5] and b = [7,9,11] (row vectors). Note the difference between the results given by [a b] and [a;b] in the following session:

>>c = [a b];c =

1 3 5 7 9 11>> D = [[1,3,5];[7,9,11]]

alternatively>>D = [a;b]D =

1 3 57 9 11

Page 22: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 22Physics 241 -- New Mexico Tech

Array Addressing

The colon operator selects individual elements, rows, columns, or ''subarrays'' of arrays.

Examples:! v(:) represents all the elements of the vector v.! v(2:5) represents the second through fifth

elements; that is v(2), v(3), v(4), v(5). ! A(:,3) denotes all the elements in the third column

of the matrix A.! A(:,2:5) denotes all the elements in the second

through fifth columns of A.! A(3,:) denotes all the elements in the third row

Page 23: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 23Physics 241 -- New Mexico Tech

You can use array indices to extract a smaller array from another array. For example, if you first create the array B

B =

C =16 3 7

8 4 9

2 4 10 1316 3 7 18 8 4 9 253 12 15 17

then type C = B(2:3,1:3), you can produce the following array:

Page 24: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 24Physics 241 -- New Mexico Tech

size(A) Returns a row vector [m n]containing the sizes of them x n array A.

sort(A) Sorts each column of the array A in ascending order and returns an array the same size as A.

sum(A) Sums the elements in each column of the array A andreturns a row vector containing the sums.

max(A) ?

Additional Array Functions

Page 25: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 25Physics 241 -- New Mexico Tech

The Workspace Browser

Page 26: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 26Physics 241 -- New Mexico Tech

The Array Editor

Page 27: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 27Physics 241 -- New Mexico Tech

Element-by-element operations: Table 2.3–1

Symbol

+

-

+

-

.*

./

.\

.^

Examples

[6,3]+2=[8,5]

[8,3]-5=[3,-2]

[6,5]+[4,8]=[10,13]

[6,5]-[4,8]=[2,-3]

[3,5].*[4,8]=[12,40]

[2,5]./[4,8]=[2/4,5/8]

[2,5].\[4,8]=[2\4,5\8]

[3,5].^2=[3^2,5^2]

2.^[3,5]=[2^3,2^5]

[3,5].^[2,4]=[3^2,5^4]

Operation

Scalar-array addition

Scalar-array subtraction

Array addition

Array subtraction

Array multiplication

Array right division

Array left division

Array exponentiation

Form

A + b

A – b

A + B

A – B

A.*B

A./B

A.\B

A.^B

Page 28: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 28Physics 241 -- New Mexico Tech

The array operations are performed between the elements in corresponding locations in the arrays. For example, the array multiplication operation A.*B results in a matrix C that has the same size as A and B and has the elements ci j = ai j bi j . For example, if

then C = A.*B gives this result:

A = 11 5–9 4

B = –7 86 2

C = 11(–7) 5(8)–9(6) 4(2)

= –77 40–54 8

Page 29: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 29Physics 241 -- New Mexico Tech

Matrix-Matrix Multiplication

In the product of two matrices AB, the number of columns in A must equal the number of rows in B. The row-column multiplications form column vectors, and these column vectors form the matrix result. The product AB has the same number of rows as A and the same number of columns as B. For example,

6 –2 10 3

4 7

9 8–5 12 =

64 24 75 1161 116

= (2.4–4)

(6)(9) + (– 2)(– 5) (6)(8) + (– 2)(12)(10)(9) + (3)(– 5) (10)(8) + (3)(12)

(4)(9) + (7)(– 5) (4)(8) + (7)(12)

Page 30: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 30Physics 241 -- New Mexico Tech

Use the operator * to perform matrix multiplication in MATLAB. The following MATLAB session shows how to perform the matrix multiplication shown in (2.4–4).

>>A = [6,-2;10,3;4,7];>>B = [9,8;-5,12];>>A*Bans =

64 2475 1161 116

Page 31: Vectors, Arrays and Matrices - Physics Internal Website

9/3/2006 Page 31Physics 241 -- New Mexico Tech

Matrix multiplication does not have the commutative property; that is, in general, AB ≠ BA. A simple example will demonstrate this fact:

AB = 6 –210 3

9 8–12 14

= 78 2054 122

(2.4–6)

BA = 9 8–12 14

6 –210 3

= 134 668 65 (2.4–7)

whereas

Reversing the order of matrix multiplication is a common and easily made mistake.