Bruce Mayer, PE Licensed Electrical &amp; Mechanical Engineer [email protected]

1 / 36

# Bruce Mayer, PE Licensed Electrical & Mechanical Engineer [email protected] - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Bruce Mayer, PE Licensed Electrical & Mechanical Engineer [email protected]' - abdalla

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Engr/Math/Physics 25

Chp2 MATLABArrays: Part-2

Bruce Mayer, PE

Licensed Electrical & Mechanical [email protected]

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
Last Time
• Vector/Array Scalar Multiplication by Term-by-Term Scaling

>> r = [ 7 11 19];

>> v = 2*r

v =

14 22 38

• Vector/Array Addition & Subtraction by Term-by-Term Operations
• “Tip-toTail” Geometry
Multiplying an Array B by a scalar w produces an Array whose elements are the elements of B multiplied by w. Scalar-Array Multiplication
• Using MATLAB

>> B = [9,8;-12,14];

>> 3.7*B

ans =

33.3000 29.6000

-44.4000 51.8000

Multiplication of TWO ARRAYS is not nearly as straightforward as Scalar-Array Mult.

MATLAB uses TWO definitions for Array-Array multiplication:

ARRAY Multiplication

also called element-by-element multiplication

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

Array-Array Multiplication
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:Array-Array Operations

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

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

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

• then z = x.*y gives
If u and v are column vectors, the result of u.*v is a column vector. The Transpose operation z = (x’).*(y’) yieldsArray-Array Operations cont
• 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
• 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.
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, ifArray-Array Operations cont
• Then C = A.*B Yields
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

Array-Array Operations cont

>> 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

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 = (eusinr)•cos2r, enter command

Array-Array Operations cont

>> z = exp(u).*sin(r).*(cos(r)).^2

z =

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.
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

Array-Array DIVISION

>> z = r./u

z =

7.0000 5.5000 6.3333

ConsiderArray-Array DIVISION cont.
• Taking C = A./B yields

A =

24 20

-9 4

B =

-4 5

3 2

>> A./B

ans =

-6 4

-3 2

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].

Array EXPONENTIATION
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.^B

C =

0.0016 0.0046 0.0204

0.1250 1.0000 8.0000

49.0000 216.0000 625.0000

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

Matrix-Matrix Multiplication
• Then
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

Matrix-Mult. Mechanics
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

Matrix-Vector Multiplication
• Then the Dims: a(1x2), B(2x3), c(1x3)
Greek letter sigma (Σ, for sum) is another convenient way of handling several terms or variables – The Definition Summation Notation Digression
• For the previous example
In General the product of Conformable Matrices A & B whenMatrix Mult by Σ-Notation
• Then Any Element, cij, of Matrix C for
• i = 1 to k (no. Rows) j = 1 to n (no. Cols)

e.g.;

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
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

>> C = A*B

C =

58.4900 68.3870

-28.1370 -121.3941

4.2060 -0.1170

-54.7280 -134.4352

49.9090 -55.2210

• B is Then 3x2
• Result, C, is Then 5x2
Matrix multiplication is generally not commutative. That is, AB ≠ BAevenif BA is conformable

Consider an Illustrative Example

Matrix-Mult NOT Commutative
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

Commutation Exceptions
• Strictly speaking 0 & I are always SQUARE
Identity Matrix is a square matrix and also it is a diagonal matrix with 1’s along the diagonal

similar to scalar “1”

Identity and Null Matrices
• Null Matrix is one in which all elements are 0
• similar to scalar “0”
• Both are “idempotent” Matrices: for A = 0 or I →
Use the eye(n) command to Form an nxn Identity Matrixeye and zeros

>> 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

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.

PolyNomial Mult & Div
Find the PRODUCT forPolyNomial Mult Example

>> f = [2 -7 9 -6];

>> g = [13,-5,3];

>> prod = conv(f,g)

prod =

26 -101 158 -144 57 -18

Find the QUOTIENT PolyNomial Quotient Example

>> f = [2 -7 9 -6];

>> g = [13,-5,3];

>> [quot1,rem1] = deconv(f,g)

quot1 =

0.1538 -0.4793

rem1 =

0 0.0000 6.1420 -4.5621

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.PolyNomial Roots

>> r = roots([2, 14, 20])

r =

-5

-2

>> rf = roots(f)

rf =

2.0000

0.7500 + 0.9682i

0.7500 - 0.9682i

>> rg = roots(g)

rg =

0.1923 + 0.4402i

0.1923 - 0.4402i

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

Plotting PolyNomials

Time For

Live Demo

All Done for Today
• Not Covered in Chapter 2
• §2.6 = Cell Arrays
• §2.7 = Structure Arrays

Cell-Arrays&StructureArrays

Engr/Math/Physics 25

Appendix

Bruce Mayer, PE

Licensed Electrical & Mechanical [email protected]

Chp2 Demo

x = 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\')

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