1 chapter 3 matrix algebra with matlab basic matrix definitions and operations were covered in...

Post on 15-Jan-2016

221 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Chapter 3Matrix Algebra with MATLAB

Basic matrix definitions and operations were covered in Chapter 2. We will now consider how these operations are performed in MATLAB. All variables in MATLAB are considered as matrices. A simple scalar is considered as a 1 x 1 matrix. For MATLAB variables containing higher dimensions, certain special rules are required to deal with them.

2

Entering a Matrix in MATLAB

2 -3 5

-1 4 6

A

MATLAB Format>> A = [2 -3 5; -1 4 5]A = 2 -3 5 -1 4 5

3

Entering a Row Vector in MATLAB

MATLAB Format>> x = [1 4 7]x = 1 4 7

[1 4 7]x

4

Entering a Column Vector in MATLAB

MATLAB Format>> x = [1; 4; 7]x = 1 4 7

1

4

7

x

5

Alternate Way to Enter a Column Vector

>> x = [1 4 7]'x = 1 4 7

6

Matrix Addition and Subtraction

Matrix addition and subtraction with MATLAB are achieved in the same manner as with scalars provided that the matrices have the same size. Typical expressions are shown below.

>> C = A + B

>> D = A - B

7

Error Messages

MATLAB has many error messages that indicate problems with operations. If the matrices have different sizes, the message is

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

8

Matrix Multiplication

Matrix multiplication with MATLAB is achieved in the same manner as with scalars provided that the number of columns of the first matrix is equal to the number of rows of the second matrix. A typical expression is

>> E = A*B

9

Array Multiplication

There is another form of multiplication of matrices in which it is desired to multiply corresponding elements in a fashion similar to that of addition and subtraction. This operation arises frequently with MATLAB, and we will hereafter refer to the process as the array product to distinguish it from the standard matrix multiplication form.

10

Array Multiplication Continuation

For the array product to be possible, the two matrices must have the same size, as was the case for addition and subtraction. The resulting array product will have the same size. If F represents the resulting matrix, a given element of F, denoted by fij is determined by the corresponding product from the two matrices as

ij ij ijf a b

11

MATLAB Array Multiplication

To form an array product in MATLAB, a period must be placed after the first variable. The operation is commutative. The following two operations produce the same result.

>> F=A.*B

>> F=B.*A

12

MATLAB Array Multiplication Continuation

If there are more than two matrices for which array multiplication is desired, the periods should follow all but the last one in the expression; e. g., A.*B.*C in the case of three matrices. Alternately, nesting can be used; e.g. (A.*B).*C for the case of three matrices.

13

MATLAB Array Multiplication Continuation

The array multiplication concept arises in any operation in which the command could be “confused” for a standard matrix operation. For example, suppose it is desired to form a matrix B from a matrix A by raising each element of A to the 3rd power, The MATLAB command is

>> B = A.^3

14

Determinant of a Matrix

The determinant of a square matrix in MATLAB is determined by the simple command det(A). Thus, if a is to represent the determinant, we would type and enter>> a = det(A)Note that a is a scalar (1 x 1 "matrix").

15

Inverse Matrix

The inverse of a square matrix in MATLAB is determined by the simple command inv(A). Thus, if B is to represent the inverse of A , the command would be

>> B = inv(A)

16

Simultaneous Equation Solution

Ax = b

MATLAB Format:>> x = inv(A)*b

Alternate MATLAB Format:>> x = A\b

-1x = A b

17

Example 3-1. Enter the matrices below in MATLAB. They will be used in the next several examples.

2 -3 5

-1 4 6

A

>> A = [2 -3 5; -1 4 6];

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

2 1

7 -4

3 1

B

18

Example 3-2. Determine the transpose of B and denote it as C.

>> C = B'

C =

2 7 3 1 -4 1

The 3 x 2 matrix has been converted to a 2 x 3 matrix.

19

Example 3-3. Determine the sum of A and C and denote it as D.

>> D = A + C

D =

4 4 8 0 0 7

20

Example 3-4. Determine the product of A and B with A first.

>> A*B

ans =

-2 19 44 -11

21

Example 3-5. Determine the product of B and A with B first.

>> B*A

ans =

3 -2 16 18 -37 11 5 -5 21

22

Example 3-6. Determine the array product of A and C and denote it as E.

>> E = A.*C

E =

4 -21 15 -1 -16 6

23

Example 3-7. Enter the matrix A. It will be used in several examples.

1 2 -1

-1 1 3

3 2 1

A

>> A = [1 2 -1; -1 1 3; 3 2 1]A = 1 2 -1 -1 1 3 3 2 1

24

Example 3-7. Continuation. Determine the determinant of A and denote it as a.

>> a = det(A)

a =

20

25

Example 3-8. Determine the inverse matrix of A and denote it as Ainv.

>> Ainv = inv(A)

Ainv =

-0.2500 -0.2000 0.3500 0.5000 0.2000 -0.1000 -0.2500 0.2000 0.1500

26

Example 3-9. Use MATLAB to solve the simultaneous equations below.

1 2 32 8x x x

1 2 33 7x x x

1 2 33 2 4x x x

27

Example 3-9. Continuation.

Assume that A is still in memory.

>> b = [-8; 7; 4]

b =

-8 7 4

28

Example 3-9. Continuation.

>> x = inv(A)*b

x =

2.0000 -3.0000 4.0000

29

Example 3-9. Continuation.Alternately,

>> x = A\b

x =

2.0000 -3.0000 4.0000

top related