bruce mayer, pe licensed electrical & mechanical engineer [email protected]

36
[email protected] • ENGR-25_Arrays-2.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer [email protected] Engr/Math/Physics 25 Chp2 MATLAB Arrays: Part-2

Upload: abdalla

Post on 25-Feb-2016

24 views

Category:

Documents


1 download

DESCRIPTION

Engr/Math/Physics 25. Chp2 MATLAB Arrays: Part-2. Bruce Mayer, PE Licensed Electrical & Mechanical Engineer [email protected]. Learning Goals. Learn to Construct 1D Row and Column Vectors Create MULTI-Dimensional ARRAYS and MATRICES - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt1

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Bruce Mayer, PELicensed Electrical & Mechanical Engineer

[email protected]

Engr/Math/Physics 25

Chp2 MATLABArrays: Part-2

Page 2: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt2

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Learning Goals Learn to Construct 1D Row and Column

Vectors Create MULTI-Dimensional ARRAYS

and MATRICES Perform Arithmetic Operations on

Vectors and Arrays/Matrices Analyze Polynomial Functions

Page 3: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt3

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Last Time Vector/Array Scalar

Multiplication by Term-by-Term Scaling

Vector/Array Addition & Subtraction by Term-by-Term Operations• “Tip-toTail” Geometry

>> r = [ 7 11 19];>> v = 2*rv = 14 22 38 vrw

Page 4: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt4

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Scalar-Array Multiplication Multiplying an Array B by a scalar w

produces an Array whose elements are the elements of B multiplied by w.

8.514.44

6.293.33141289

7.3

>> B = [9,8;-12,14];>> 3.7*Bans = 33.3000 29.6000 -44.4000 51.8000

Using MATLAB

Page 5: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt5

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Array-Array Multiplication Multiplication of TWO ARRAYS is not

nearly as straightforward as Scalar-Array Mult.

MATLAB uses TWO definitions for Array-Array multiplication:1. ARRAY Multiplication

– also called element-by-element multiplication

2. MATRIX Multiplication (see also MTH6) DIVISION and EXPONENTIATION

must also be CAREFULLY defined when dealing with operations between two arrays.

Page 6: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt6

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Element-by-Element OperationsSymbol Operation Form Example

+ Scalar-array addition A + b [6,3]+2=[8,5]- Scalar-array subtraction A – b [8,3]-5=[3,-2]+ Array addition A + B [6,5]+[4,8]=[10,13]- Array subtraction A – B [6,5]-[4,8]=[2,-3].* Array multiplication A.*B [3,5].*[4,8]=[12,40]

./ Array right division A./B [2,5]./[4,8]=[2/4,5/8]= [0.5,0.625]

.\ Array left division A.\B [2,5].\[4,8]=[2\4,5\8]= [2,1.6]

.^ Array exponentiation A.^B[3,5].^2=[3^2,5^2]2.^[3,5]=[2^3,2^5]

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

Page 7: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt7

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Array-Array Operations Array or Element-by-Element

multiplication is defined ONLY for arrays having the SAME size. The definition of the product x.*y, where x and y each have n×n elements:

if x and y are row vectors. For example, if

x.*y = [x(1)y(1), x(2)y(2), ... , x(n)y(n)]

x = [2, 4, – 5], y = [– 7, 3, – 8]

then z = x.*y gives

40,12,1485,34,72 z

Page 8: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt8

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Array-Array Operations cont

If u and v are column vectors, the result of u.*v is a column vector. The Transpose operation z = (x’).*(y’) yields Note that x’ is a

column vector with size 3 × 1 and thus does not have the same size as y, whose size is 1 × 3

401214

853472

z

Thus for the vectors x and y the operations x’.*y and y.*x’ are NOT DEFINED in MATLAB and will generate an error message.

Page 9: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt9

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Array-Array Operations cont

The array operations are performed between the elements in corresponding locations in the arrays. For example, the array multiplication operation A.*B results in an array C that has the same size as A and B and has the elements cij = aij bij . For example, if Then C = A.*B

Yields

2687

49511

BA

8544077

246985711

C

Page 10: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt10

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Array-Array Operations cont

The built-in MATLAB functions such as sqrt(x) and exp(x) automatically operate on array arguments to produce an array result of the same size as the array argument x• Thus these functions are said to be

VECTORIZED Some Examples

>> r = [7 11 19];>> h = sqrt(r)h = 2.6458 3.3166 4.3589

>> u = [1,2,3];>> f = exp(u)f = 2.7183 7.3891 20.0855

Page 11: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt11

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Array-Array Operations cont

However, when multiplying or dividing these functions, or when raising them to a power, we must use element-by-element dot (.) operations if the arguments are arrays.

To Calc: z = (eu sinr)•cos2r, enter command

>> z = exp(u).*sin(r).*(cos(r)).^2z = 1.0150 -0.0001 2.9427

MATLAB returns an error message if the size of r is not the same as the size of u. The result z will have the same size as r and u.

Page 12: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt12

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Array-Array DIVISION The definition of array division is similar

to the definition of array multiplication except that the elements of one array are divided by the elements of the other array. Both arrays must have the same size. The symbol for array right division is ./

Recall r = [ 7 11 19] u = [1,2,3] then z = r./u gives

333.65.57

31921117z

>> z = r./uz = 7.0000 5.5000 6.3333

Page 13: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt13

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Array-Array DIVISION cont.

Consider

>> A./Bans = -6 4 -3 2

Taking C = A./B yields

2354

492024

BA

2346

2439520424

C

A = 24 20 -9 4B = -4 5 3 2

Page 14: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt14

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Array EXPONENTIATION MATLAB enables us not only to raise

arrays to powers but also to raise scalars and arrays to ARRAY powers.

Use the .^ symbol to perform exponentiation on an element-by-element basis• if x = [3, 5, 8], then typing x.^3 produces

the array [33, 53, 83] = [27, 125, 512] We can also raise a scalar to an array

power. For example, if p = [2, 4, 5], then typing 3.^p produces the array [32, 34, 35] = [9, 81, 243].

Page 15: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt15

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Array to Array Power>> A = [5 6 7; 8 9 8; 7 6 5]A = 5 6 7 8 9 8 7 6 5

>> B = [-4 -3 -2; -1 0 1; 2 3 4]B = -4 -3 -2 -1 0 1 2 3 4

>> C = A.^BC = 0.0016 0.0046 0.0204 0.1250 1.0000 8.0000 49.0000 216.0000 625.0000

Page 16: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt16

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Matrix-Matrix Multiplication Multiplication of MATRICES requires

meeting the CONFORMABILITY condition

The conformability condition for multiplication is that the COLUMN dimensions (k x m) of the LEAD matrix A must be EQUAL to the ROW dimension of the LAG matrix B (m x n)

If

12589

7431026

BA Then

Error BACCAB 3x2 is

Page 17: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt17

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Matrix-Mult. Mechanics Multiplication of A (k x m) and B (m x n)

CONFORMABLE Matrices produces a Product Matrix C with Dimensions (k x n)

The elements of C are the sum of the products of like-index Row Elements from A, and Column Elements from B; to whit

1161116752464

12784579412381053910122865296

12589

7431026

C

Page 18: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt18

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Matrix-Vector Multiplication A Vector and Matrix May be Multiplied if

they meet the Conformability Critera: (1xm)*(mxp) or (kxm)*(mx1)• Given Vector-a, Matrix-B, and aB; Find

Dims for all

cbbb

bbbaaaB

232221

1312111211

231213112212121121121111131211 babababababaccc

Then the Dims: a(1x2), B(2x3), c(1x3)

Page 19: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt19

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Summation Notation Digression Greek letter sigma (Σ, for sum) is

another convenient way of handling several terms or variables – The Definition

For the previous example

qqq

q

pp xxxxxxxsum

1232

11

231213112212121121121111131211 babababababaccc

2

111

pppba

2

121

pppba

2

131

pppba

Page 20: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt20

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Matrix Mult by Σ-Notation In General the

product of Conformable Matrices A & B when

Then Any Element, cij, of Matrix C for• i = 1 to k (no. Rows) j = 1 to n (no. Cols)

knkk

n

n

ccc

cccccc

21

22221

11211

CAB

mp

ppjipij bac

1

e.g.;

7

13553

p

pppbac

nmmk BA

Page 21: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt21

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Matrix Mult Example

>> A = [3 1.7 -7; 8.1 -0.31 4.6; -1.2 2.3 0.73; 4 -.32 8; 7.7 9.9 -0.17]

A =

3.0000 1.7000 -7.0000 8.1000 -0.3100 4.6000 -1.2000 2.3000 0.7300 4.0000 -0.3200 8.0000 7.7000 9.9000 -0.1700

A is Then 5x3

Page 22: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt22

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Matrix Mult Example cont

>> B = [0.67 -7.6; 4.4 .11; -7 -13]

B =

0.6700 -7.6000 4.4000 0.1100 -7.0000 -13.0000

B is Then 3x2

>> C = A*B

C =

58.4900 68.3870 -28.1370 -121.3941 4.2060 -0.1170 -54.7280 -134.4352 49.9090 -55.2210

Result, C, is Then 5x2

Page 23: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt23

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Matrix-Mult NOT Commutative Matrix multiplication is generally not

commutative. That is, AB ≠ BA even if BA is conformable• Consider an

Illustrative ExampleBA

7610

&4321

25241312

7413640372116201

AB

402743

4726371641203110

BA

Page 24: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt24

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Commutation Exceptions Two EXCEPTIONS to the

NONcommutative property are the NULL or ZERO matrix, denoted by 0 and the IDENTITY, or UNITY, matrix, denoted by I.• The NULL matrix contains all ZEROS and is

NOT the same as the EMPTY matrix [ ], which has NO elements.

Commutation of the Null & Identity Matrices

AAIIA0A00A Strictly speaking 0 & I are always SQUARE

Page 25: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt25

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Identity and Null Matrices Identity Matrix is a

square matrix and also it is a diagonal matrix with 1’s along the diagonal• similar to scalar “1” Null Matrix is one in

which all elements are 0• similar to scalar “0”

etc.

100010001

.or 1001

000000000

Both are “idempotent” Matrices: for A = 0 or I → 32

and

AAAAA

T

Page 26: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt26

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

eye and zeros Use the eye(n)

command to Form an nxn Identity Matrix

>> I = eye(5)I = 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1

Use the zeros(mxn) to Form an mxn 0-Filled Matrix• Strictly Speaking a

NULL Matrix is SQUARE

>> Z24 = zeros(2,4)

Z24 =

0 0 0 0 0 0 0 0

Page 27: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt27

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

PolyNomial Mult & Div Function conv(a,b) computes the

product of the two polynomials described by the coefficient arrays a and b. The two polynomials need not be the same degree. The result is the coefficient array of the product polynomial.

function [q,r] = deconv(num,den) produces the result of dividing a numerator polynomial, whose coefficient array is num, by a denominator polynomial represented by the coefficient array den. The quotient polynomial is given by the coefficient array q, and the remainder polynomial is given by the coefficient array r.

Page 28: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt28

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

PolyNomial Mult Example Find the PRODUCT for

35136972 223 xxxgxxxxf

>> f = [2 -7 9 -6];>> g = [13,-5,3];>> prod = conv(f,g)prod = 26 -101 158 -144 57 -18

185714415810126 2345 xxxxxxprod

Page 29: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt29

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

PolyNomial Quotient Example Find the QUOTIENT

35136972

2

23

xxxxxquot

>> f = [2 -7 9 -6];>> g = [13,-5,3];>> [quot1,rem1] = deconv(f,g)quot1 = 0.1538 -0.4793rem1 = 0 0.0000 6.1420 -4.5621

5621.4142.6 rem4973.01538.0 xxxquot

Page 30: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt30

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

PolyNomial Roots The function roots(h) computes the

roots of a polynomial specified by the coefficient array h. The result is a column vector that contains the polynomial’s roots.>> r = roots([2, 14, 20])r = -5 -2>> rf = roots(f)rf = 2.0000 0.7500 + 0.9682i 0.7500 - 0.9682i

25020142 2 xxxx

>> rg = roots(g)rg = 0.1923 + 0.4402i 0.1923 - 0.4402i

Page 31: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt31

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Plotting PolyNomials The function polyval(a,x)evaluates a

polynomial at specified values of its independent variable x, which can be a matrix or a vector. The polynomial’s coefficients of descending powers are stored in the array a. The result is the same size as x.

Plot over (−4 ≤ x ≤ 7) the function 6972 23 xxxxf

Page 32: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt32

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

The Demo Plot

Page 33: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt33

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

All Done for Today Not Covered in

Chapter 2• §2.6 = Cell Arrays• §2.7 = Structure

Arrays

Cell-Arrays&

StructureArrays

Page 34: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt34

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Bruce Mayer, PELicensed Electrical & Mechanical Engineer

[email protected]

Engr/Math/Physics 25

Appendix

Page 35: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt35

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Chp2 Demox = linspace(-4,7,20);p3 = [2 -7 9 -6];y = polyval(p3,x);plot(x,y, x,y, 'o', 'LineWidth', 2), grid, xlabel('x'),...ylabel('y = f(x)'), title('f(x) = 2x^3 - 7x^2 + 9x - 6')

-4 -2 0 2 4 6 8-300

-200

-100

0

100

200

300

400

500

x

y =

f(x)

f(x) = 2x3 - 7x2 + 9x - 6

Page 36: Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

[email protected] • ENGR-25_Arrays-2.ppt36

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Chp2 Demo

>> f = [2 -7 9 -6];>> x = [-4:0.02:7];>> fx = polyval(f,x);>> plot(x,fx),xlabel('x'),ylabel('f(x)'), title('chp2 Demo'), grid