You are on page 1of 117

An Engineers Guide to MATLAB Chapter 2

AN ENGINEERS GUIDE TO MATLAB


3rd Edition

CHAPTER 2
VECTORS and MATRICES

Copyright Edward B. Magrab 2009 1


An Engineers Guide to MATLAB Chapter 2

Chapter 2 Objective
Introduce MATLAB syntax in the context of
vectors and matrices and their manipulation.

Copyright Edward B. Magrab 2009 2


An Engineers Guide to MATLAB Chapter 2

Topics

Definitions of Matrices and Vectors


Creation of Vectors
Creation of Matrices
Dot Operations
Mathematical Operations with Matrices
Addition and Subtraction
Multiplication
Determinants
Matrix Inverse
Solution of a System of Equations

Copyright Edward B. Magrab 2009 3


An Engineers Guide to MATLAB Chapter 2

Definitions of Matrices and Vectors


An array A of m rows and n columns is called a
matrix of order (mn) and is written as

a11 a12 ... a1n


a a22
A = 21 (m n)
... ...

am1 amn

The elements of the matrix are denoted aij, where i


indicates the row number and j the column number.

Square Matrix: n = m

Copyright Edward B. Magrab 2009 4


An Engineers Guide to MATLAB Chapter 2

Diagonal Matrix: When n = m and aij = 0, i j; thus

a11 0 ... 0
0 a22
A= (n n)
... ...

0 ann

Identity Matrix: Diagonal matrix with aii = 1; thus

1 0 ... 0
0 1
I=
... ...

0 1

Copyright Edward B. Magrab 2009 5


An Engineers Guide to MATLAB Chapter 2

Column Matrix - Vector:

When aij = ai1 (there is only one column), then


a11 a1
a a
a = 21 = 2 (m 1)
... ...

am 1 am
Row Matrix - Vector:

When aij = a1j, (there is only one row), then

a a11 a12 a1n a1 a2 an (1 n)

Default definition of a vector in MATLAB


Copyright Edward B. Magrab 2009 6
An Engineers Guide to MATLAB Chapter 2

Transpose of a Matrix and a Vector


The transpose of a matrix is denoted by an apostrophe ().
If A is defined as before and W = A, then

w11 = a11 w12 = a21 ... w1m = am1


w = a w22 = a22
W = A = 21 12 (n m )
... ...

wn1 = a1n wnm = amn
Recall that
a11 a12 ... a1n
a a22
A = 21 (m n)
... ...

am1 amn

Copyright Edward B. Magrab 2009 7


An Engineers Guide to MATLAB Chapter 2

For a column vector -


a1
a
If a 2 (m 1) then a a1 a2 ... am (1 m )
...

am

For a row vector -


a1
a
If a a1 a2 ... am (1 m ) then a 2 ( m 1)


am

Copyright Edward B. Magrab 2009 8


An Engineers Guide to MATLAB Chapter 2

Creation of Vectors
If a, x, b, are either variable names, numbers,
expressions, or strings, then vectors are expressed as
either
f = [a x b ] (Blanks required)
or
f = [a, x, b, ] (Blanks optional)

Caution about blanks: If a = h + ds, then f is written as


either
f = [h+d^s x b ...]
No blanks permitted
or within expression
f = [h+d^s, x, b, ...]

Copyright Edward B. Magrab 2009 9


An Engineers Guide to MATLAB Chapter 2

Ways to Assign Numerical Values to the Elements


of a Vector -
Specify the range of the values and the increment
between adjacent values called colon notation.
Used when the increment is either important or
has been specified.
Specify the range of the values and the number of
values desired.
Used when the number of values is important.

Copyright Edward B. Magrab 2009 10


An Engineers Guide to MATLAB Chapter 2

If s, d, and f are any combination of numerical values,


variable names, and expressions, then the colon notation
that creates a vector x is either
x = s:d:f or x = (s:d:f) or x = [s:d:f]
where
s = start or initial value
d = increment or decrement
f = end or final value
Thus, the following row vector x is created
x = [s, s+d, s+2d, , s+nd] (Note: s+nd f )

Copyright Edward B. Magrab 2009 11


An Engineers Guide to MATLAB Chapter 2

Also, when d is omitted MATLAB assumes that d = 1. Then


x = s:f
creates the vector
x = [s, s+1, s+2, , s+n]
where s+n f

The number of terms, called the length of the vector, that


this expression has created is determined from
length(x)

Copyright Edward B. Magrab 2009 12


An Engineers Guide to MATLAB Chapter 2

Example
Create a row vector that goes from 0.2 to 1.0 in
increments of 0.1.
x = 0.2:0.1:1
n = length(x)
When executed, we obtain
x=
0.20 0.30 0.40 0.50 0.60 0.70 0.80 0.90 1.00
n= x=
0.2000
9 0.3000
0.4000
To create a column vector, we use 0.5000
0.6000
x = (0.2:0.1:1) 0.7000

n = length(x) 0.8000
0.9000
1.0000
n=
9

Copyright Edward B. Magrab 2009 13


An Engineers Guide to MATLAB Chapter 2

Generation of n Equally Spaced Values


x = linspace(s, f, n)
where the increment (decrement) is computed by
MATLAB from
f s
d=
n 1
The values of s and f can be either positive or negative
and either s > f or s < f. When n is not specified, it is
assigned a value of 100. Thus, linspace creates the
vector
x = [s, s+d, s+2d, , f = s+(n1)d]

Copyright Edward B. Magrab 2009 14


An Engineers Guide to MATLAB Chapter 2

If equal spacing on a logarithmic scale is desired, then


x = logspace(s, f, n)
where the initial value is xstart = 10s, the final value is
xfinal = 10f and d is defined above.
This expression creates the row vector
f s
x= [10s 10s+d 10s+2d 10f] d=
n 1

Copyright Edward B. Magrab 2009 15


An Engineers Guide to MATLAB Chapter 2

Examples -
x = linspace(0, 2, 5)
gives
x=
0 0.5000 1.0000 1.5000 2.0000
and
x = logspace(0, 2, 5) % [100, 100.5, 101, 101.5, 102]
gives
x=
1.0000 3.1623 10.0000 31.6228 100.0000

Copyright Edward B. Magrab 2009 16


An Engineers Guide to MATLAB Chapter 2

Transpose of a Vector with Complex Values


If a vector has elements that are complex numbers,
then taking its transpose also converts the complex
quantities to their complex conjugate values.
Consider the following script
z = [1, 7+4j, -15.6, 3.5-0.12j];
w = z
which, upon execution, gives
w=
1.0000
7.0000 - 4.0000i
-15.6000
3.5000 + 0.1200i

Copyright Edward B. Magrab 2009 17


An Engineers Guide to MATLAB Chapter 2

If the transpose is desired, but not the conjugate, then


the script is written as
z = [1, 7+4j, -15.6, 3.5-0.12j];
w = conj(z')
which, upon execution, gives
w=
1.0000
7.0000 + 4.0000i
-15.6000
3.5000 - 0.1200i

Copyright Edward B. Magrab 2009 18


An Engineers Guide to MATLAB Chapter 2

Accessing Elements of Vectors Subscript Notation


Consider the row vector
b = [b1 b2 b3 bn]
Access to one or more elements of this vector is
accomplished using subscript notation in which the
subscript refers to the location in the vector as follows:
b(1) b1

b(k) bk
...
b(n) bn % or b(end)

Copyright Edward B. Magrab 2009 19


An Engineers Guide to MATLAB Chapter 2

Example
x = [-2, 1, 3, 5, 7, 9, 10];
b = x(4)
c = x(6)
xlast = x(end)
When executed, we obtain
b=
5
c=
9
xlast =
10

Copyright Edward B. Magrab 2009 20


An Engineers Guide to MATLAB Chapter 2

Accessing Elements of Vectors Using Subscript Colon


Notation
The subscript colon notation is a shorthand method of
accessing a group of elements.
For a vector b with n elements, one can access a group
of them using the notation
b(k:d:m)
where 1 k < m n, k and m are positive integers and,
in this case, d is a positive integer.
Thus, if one has 10 elements in a vector b, the third
through seventh elements are selected by using
b(3:7)

Copyright Edward B. Magrab 2009 21


An Engineers Guide to MATLAB Chapter 2

Example
Consider the following script
z = [-2, 1, 3, 5, 7, 9, 10];
z(2) = z(2)/2;
z(3:4) = z(3:4)*3-1;
z
When executed, we obtain
z=
-2.00 0.50 8.00 14.00 7.00 9.00 10.00

Copyright Edward B. Magrab 2009 22


An Engineers Guide to MATLAB Chapter 2

Example
Consider the script
y = [-1, 6, 15, -7, 31, 2, -4, -5];
x = y(3:5)
whose execution creates the three-element vector
x=
15 -7 31

Copyright Edward B. Magrab 2009 23


An Engineers Guide to MATLAB Chapter 2

Example
We shall show that there are several ways to create a
vector x that is composed of the first two and the last two
elements of a vector y. Thus,
y = [-1, 6, 15, -7, 31, 2, -4, -5];
x = [y(1), y(2), y(7), y(8)]
or
y = [-1, 6, 15, -7, 31, 2, -4, -5];
index = [1, 2, 7, 8]; % or [1:2, length(y)-1, length(y)]
x = y(index)
or more compactly
y = [-1, 6, 15, -7, 31, 2, -4, -5];
x = y([1, 2, 7, 8]) % or y([1:2, length(y)-1, length(y)])

Copyright Edward B. Magrab 2009 24


An Engineers Guide to MATLAB Chapter 2

Two useful functions: sort and find


sort: Sorts a vector y in ascending order (most negative to
most positive) or descending order (most positive to most
negative)
[ynew, indx] = sort(y, mode) % mode = 'ascend or 'descend'
where ynew is the vector with the rearranged (sorted) elements
of y and indx is a vector containing the original locations of the
elements in y.

find: Determines the locations (not the values) of all the


elements in a vector (or matrix) that satisfy a user-specified
relational and/or logical condition or expression Expr
indxx = find(Expr)

Copyright Edward B. Magrab 2009 25


An Engineers Guide to MATLAB Chapter 2

Example
y = [-1, 6, 15, -7, 31, 2, -4, -5];
z = [10, 20, 30, 40, 50, 60, 70, 80];
[ynew, indx] = sort(y, 'ascend')
znew = z(indx)
when executed, gives
ynew =
-7 -5 -4 -1 2 6 15 31
indx =
4 8 7 1 6 2 3 5
znew =
40 80 70 10 60 20 30 50

Copyright Edward B. Magrab 2009 26


An Engineers Guide to MATLAB Chapter 2

Example
y = [-1, 6, 15, -7, 31, 2, -4, -5];
indxx = find(y<=0)
s = y(indxx)
Upon execution, we obtain
indxx =
1 4 7 8
s=
-1 -7 -4 -5

Copyright Edward B. Magrab 2009 27


An Engineers Guide to MATLAB Chapter 2

One of the great advantages of MATLABs implicit vector


and matrix notation is that it provides the user with a
compact way of performing a series of operations on an
array of values.
Example
The script
x = linspace(-pi, pi, 10);
y = sin(x)
yields the following vector
y=
-0.0000 -0.6428 -0.9848 -0.8660 -0.3420 0.3420
0.8660 0.9848 0.6428 0.0000

Copyright Edward B. Magrab 2009 28


An Engineers Guide to MATLAB Chapter 2

Minimum and Maximum Values of a Vector: min and max


To find the magnitude of the smallest element xmin and its
location locmin in a vector, we use
[xmin, locmin] = min(x)
To find the magnitude of the largest element xmax and its
location locmax in a vector, we use
[xmax, locmax] = max(x)

Copyright Edward B. Magrab 2009 29


An Engineers Guide to MATLAB Chapter 2

Example
x = linspace(-pi, pi, 10);
y = sin(x);
[ymax, kmax] = max(y)
[ymin, kmin] = min(y)
Upon execution, we find that
ymax =
0.9848
kmax =
8 y=
-0.0000 -0.6428 -0.9848 -0.8660 -0.3420
ymin =
0.3420 0.8660 0.9848 0.6428 0.0000
-0.9848
kmin =
3

Copyright Edward B. Magrab 2009 30


An Engineers Guide to MATLAB Chapter 2

Example - Analysis of the elements of a vector


Consider 50 equally-spaced points of the following
function
f (t ) sin(t ) 0 t 2

We shall
(a) determine the time at which the minimum
positive value of f(t) occurs
(b) the average value of its negative values
The script is

Copyright Edward B. Magrab 2009 31


An Engineers Guide to MATLAB Chapter 2

t = linspace(0, 2*pi, 50);


f = sin(t);
fAvgNeg = mean(f(find(f<0)))
MinValuef = min(f(find(f>0)));
tMinValuef = t(find(f==MinValuef))
Upon execution, we find that
fAvgNeg = 1

-0.6237
0.5
tMinValuef =
3.0775
0

-0.5

-1
0 1 2 3 4 5 6

Copyright Edward B. Magrab 2009 32


An Engineers Guide to MATLAB Chapter 2

Creation of Matrices
The basic syntax to create a matrix is
A = [a11 a12 a13; a21 a22 a23; a31 a32 a33; a41 a42 a43]
where the semicolons are used to indicate the end of a
row and the aij can be numbers, variable names,
expressions, or strings.
Alternate ways are:
A = [a11 a12 a13; ...
a21 a22 a23; ...
a31 a32 a33; ...
a41 a42 a43]
where the ellipses (...) are required to indicate that the
expression continues on the next line.
Copyright Edward B. Magrab 2009 33
An Engineers Guide to MATLAB Chapter 2

For the second alternate way,

One can omit the ellipsis () and instead to use the


Enter key to indicate the end of a row. In this case,
the expression will look like
A = [a11 a12 a13 %<Enter>
a21 a22 a23 %<Enter>
a31 a32 a33 %<Enter>
a41 a42 a43]

Copyright Edward B. Magrab 2009 34


An Engineers Guide to MATLAB Chapter 2

For a third alternate way,


Create four separate row vectors, each with the
same number of columns, and then combine these
vectors to form the matrix as follows
v1 = [ a11 a12 a13];
v2 = [ a21 a22 a23];
v3 = [ a31 a32 a33];
v4 = [ a41 a42 a43];
A = [ v1; v2; v3; v4]
where the semicolons in the first four lines are used
to suppress display to the command window.

The order of the matrix is determined by


[r, c] = size(A)

Copyright Edward B. Magrab 2009 35


An Engineers Guide to MATLAB Chapter 2

Transpose of a matrix with complex elements


If a matrix has elements that are complex numbers,
then taking its transpose also converts the complex
quantities to their complex conjugate values.
Consider the following script
Z = [1+2j, 3+4j; 5+6j, 7+9j]
W = Z'
the execution of which gives
Z=
1.0000 + 2.0000i 3.0000 + 4.0000i
5.0000 + 6.0000i 7.0000 + 9.0000i
W=
1.0000 - 2.0000i 5.0000 - 6.0000i
3.0000 - 4.0000i 7.0000 - 9.0000i
Copyright Edward B. Magrab 2009 36
An Engineers Guide to MATLAB Chapter 2

Special Matrices
A matrix of all 1s
ones(r, c) on(1:r,1:c) = 1 on = ones(2, 5)
on =
1 1 1 1 1
1 1 1 1 1

A null matrix
zeros(r, c) zer(1:r,1:c) = 0
zer = zeros(3, 2)
zer =
0 0
0 0
0 0

Copyright Edward B. Magrab 2009 37


An Engineers Guide to MATLAB Chapter 2

Diagonal matrix
a = [4, 9, 1];
Create an (nn) diagonal matrix A = diag(a)
whose diagonal elements are a A=
vector a of length n 4 0 0
0 9 0
diag(a)
0 0 1
Extract the diagonal elements of
a square matrix A Ad = diag([11, 12, 13, 14;
diag(A) 21, 22, 23, 24;
31, 32, 33, 34;
41, 42, 43, 44])
Ad =
11
22
33
44
Copyright Edward B. Magrab 2009 38
An Engineers Guide to MATLAB Chapter 2

Identity matrix whose size is (nn)


eye(n) A = eye(3)
A=
1 0 0
0 1 0
0 0 1

Copyright Edward B. Magrab 2009 39


An Engineers Guide to MATLAB Chapter 2

Accessing Matrix Elements Matrix created with


A = [3:2:11;
A(1,1) A(1:3,3:5) linspace(20, 21, 5);
ones(1, 5)];
3 5 7 9 11 A(2,:)
A 20.0 20.25 20.5 20.75 21.0

1 1 1 1 1 : means all
elements of
A(:,2) A(3,4)
row 2

: means all
B = A(1:3,3:5)
elements of
B=
column 2
7.0000 9.0000 11.0000
20.5000 20.7500 21.0000
1.0000 1.0000 1.0000

Copyright Edward B. Magrab 2009 40


An Engineers Guide to MATLAB Chapter 2

In several of the following examples, we shall use


magic(n)
which creates an (nn) matrix in which the sum of the
elements in each column and the sum of the elements
of each row and the sum of the elements in each
diagonal are equal.
For example,
magic(4)
creates
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
Copyright Edward B. Magrab 2009 41
An Engineers Guide to MATLAB Chapter 2

Example
We shall set all the diagonal elements of magic(4) to
zero; thus
diag(Z) = diag(diag(Z)) =
Z = magic(4); 16 16 0 0 0
Z = Z-diag(diag(Z)) 11 0 11 0 0
which results in 6 0 0 6 0
1 0 0 0 1
Z=
0 2 3 13
5 0 10 8 magic(4) =
9 7 0 12 16 2 3 13
4 14 15 0 5 11 10 8
9 7 6 12
4 14 15 1

Copyright Edward B. Magrab 2009 42


An Engineers Guide to MATLAB Chapter 2

Example
We shall replace all the diagonal elements of magic(4)
with the value 5; thus,
Z = magic(4);
Z = Z-diag(diag(Z))+5*eye(4)
which results in magic(4) =
Z= 16 2 3 13
5 2 3 13 5 11 10 8
5 5 10 8 9 7 6 12
9 7 5 12 4 14 15 1
4 14 15 5

Copyright Edward B. Magrab 2009 43


An Engineers Guide to MATLAB Chapter 2

Use of find with Matrices


For a matrix,
[row, col] = find(Expr)
where row and col are column vectors of the locations
in the matrix that satisfied the condition represented
by Expr.
Let us find all the elements of magic(3) that are greater
than 5. The script is
m = magic(3)
[r, c] = find(m > 5);
subscr = [r c] % Column augmentation

Copyright Edward B. Magrab 2009 44


An Engineers Guide to MATLAB Chapter 2

Upon execution, we obtain


m=
8 1 6
3 5 7
4 9 2
subscr =
1 1
3 2
1 3
2 3

Copyright Edward B. Magrab 2009 45


An Engineers Guide to MATLAB Chapter 2

Example Minimum and Maximum


M = magic(4) Values in a Matrix
minM = min(M) min and max operate on a
maxM = max(M) column by column basis.
which, upon execution, gives
M=
16 2 3 13 To find the maximum of all
5 11 10 8 elements we use max twice:
9 7 6 12 M = magic(4);
4 14 15 1 maxM = max(max(M))
minM =
4 2 3 1 which, upon execution, gives
maxM = maxM =
16 14 15 13 16

Copyright Edward B. Magrab 2009 46


An Engineers Guide to MATLAB Chapter 2

Example - Creation of a special matrix


We shall create the following (99) array

0 1 1 0 2 2 0 3 3
1 0 1 2 0 2 3 0 3

1 1 0 2 2 0 3 3 0

0 4 4 0 5 5 0 6 6
A 4 0 4 5 0 5 6 0 6

4 4 0 5 5 0 6 6 0
0 7 7 0 8 8 0 9 9

7 0 7 8 0 8 9 0 9

7 7 0 8 8 0 9 9 0

(Dashed lines have been added to enhance visual clarity.)

Copyright Edward B. Magrab 2009 47


An Engineers Guide to MATLAB Chapter 2

The script is
a = ones(3, 3)-eye(3);
A = [a, 2*a, 3*a; 4*a, 5*a, 6*a; 7*a, 8*a, 9*a;]
Upon execution, we obtain
A=
0 1 1 0 2 2 0 3 3
1 0 1 2 0 2 3 0 3
1 1 0 2 2 0 3 3 0
0 4 4 0 5 5 0 6 6
4 0 4 5 0 5 6 0 6
4 4 0 5 5 0 6 6 0
0 7 7 0 8 8 0 9 9
7 0 7 8 0 8 9 0 9
7 7 0 8 8 0 9 9 0

Copyright Edward B. Magrab 2009 48


An Engineers Guide to MATLAB Chapter 2

Example - Rearrangement of Sub Matrices of a Matrix


Consider the following (99) array
1 2
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18

19 20 21 22 23 24 25 26 27

28 29 30 31 32 33 34 35 36
A 37 38 39 40 41 42 43 44 45

46 47 48 49 50 51 52 53 54
55 56 57 58 59 60 61 62 63

64 65 66 67 68 69 70 71 72

73 74 75 76 77 78 79 80 81
4 3

(Dashed lines have been added to enhance visual clarity.)

Copyright Edward B. Magrab 2009 49


An Engineers Guide to MATLAB Chapter 2

For the four (33) sub matrices identified by the circled


numbers 1 to 4, we shall perform a series of swaps of
these sub matrices to produce the following array
3 4
61 62 63 4 5 6 55 56 57
70 71 72 13 14 15 64 65 66

79 80 81 22 23 24 73 74 75

28 29 30 31 32 33 34 35 36
A 37 38 39 40 41 42 43 44 45 1 2

1 9
46 47 48 49 50 51 52 53 54 10
2 3 4 5 6 7 8
18
7 8 9 58 59 60 1 2 3 11 12 13 14 15 16 17
19 27

20 21 22 23 24 25 26

16 17 18 67 68 69 10 11 12 28 29 30 31 32 33 34 35 36
A 37 45

38 39 40 41 42 43 44

25 26 27 76 77 78 19 20 21 46 47 48 49 50 51 52 53 54
55 56 57 58 59 60 61 62 63

2 1 64 65 66 67 68 69 70 71 72

73 74 75 76 77 78 79 80 81
4 3

Copyright Edward B. Magrab 2009 50


An Engineers Guide to MATLAB Chapter 2

The script is
Matr = [1:9; 10:18; 19:27; 28:36; 37:45; ...
46:54; 55:63; 64:72; 73:81];
Temp1 = Matr(1:3, 1:3);
Matr(1:3, 1:3) = Matr(7:9, 7:9);
Matr(7:9, 7:9) = Temp1;
Temp1 = Matr(1:3, 7:9);
Matr(1:3, 7:9) = Matr(7:9, 1:3);
Matr(7:9, 1:3) = Temp1;
Matr

Copyright Edward B. Magrab 2009 51


An Engineers Guide to MATLAB Chapter 2

Upon execution, we obtain


Matr =
61 62 63 4 5 6 55 56 57
70 71 72 13 14 15 64 65 66
79 80 81 22 23 24 73 74 75
28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54
7 8 9 58 59 60 1 2 3
16 17 18 67 68 69 10 11 12
1 2
25 26 27 76 77 78 19 20 21 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18

19 20 21 22 23 24 25 26 27

28 29 30 31 32 33 34 35 36
A 37 38 39 40 41 42 43 44 45

46 47 48 49 50 51 52 53 54
55 56 57 58 59 60 61 62 63

64 65 66 67 68 69 70 71 72

73 74 75 76 77 78 79 80 81
4 3
Copyright Edward B. Magrab 2009 52
An Engineers Guide to MATLAB Chapter 2

Manipulation of Arrays
Two functions that create matrices by replicating a
specified number of times either a scalar, a column or row
vector, or a matrix are
repmat
and
meshgrid
which uses repmat. The general form of repmat is
repmat(x, r, c)
where x is either a scalar, vector, or matrix, r is the total
number of rows of x that will be replicated, and c is the
total number of columns of x will be replicated.

Copyright Edward B. Magrab 2009 53


An Engineers Guide to MATLAB Chapter 2

Example
We shall produce the following matrix three ways
W=
45.7200 45.7200 45.7200
45.7200 45.7200 45.7200
45.7200 45.7200 45.7200
Thus,
W = repmat(45.72, 3, 3) % Function
or
W = [45.72, 45.72, 45.72; % Hard code
45.72, 45.72, 45.72;
45.72, 45.72, 45.72]
or
W(1:3,1:3) = 45.72 % Subscript colon notation

Copyright Edward B. Magrab 2009 54


An Engineers Guide to MATLAB Chapter 2

Example
Consider the vector
s = [a1 a2 a3 a4]
The expression
V = repmat(s, 3, 1)
creates the numerical equivalent of the matrix
a1 a2 a3 a4
V = a1 a2 a3 a4
Replicated
a1 a2 a3 a4 row

Copyright Edward B. Magrab 2009 55


An Engineers Guide to MATLAB Chapter 2

whereas
repmat(s, 3, 2)
creates the numerical equivalent of the matrix

a1 a2 a3 a4 a1 a2 a3 a4
V = a1 a2 a3 a4 a1 a2 a3 a4
a1 a2 a3 a4 a1 a2 a3 a4
Replicated
1st row
Replicated
column

Copyright Edward B. Magrab 2009 56


An Engineers Guide to MATLAB Chapter 2

On the other hand


V = repmat(s', 1, 3)
gives the numerical equivalent of the matrix

a1 a1 a1
a a2 a2
V = 2
a3 a3 a3

a4 a4 a4

Replicated
1st column

Copyright Edward B. Magrab 2009 57


An Engineers Guide to MATLAB Chapter 2

and
V = repmat(s', 2, 3)
gives the numerical equivalent of the matrix
a1 a1 a1
a a2 a2
2
a3 a3 a3

a4 a4 a4
V =
a1 a1 a1

a2 a2 a2
a a3 a3
3
a4 a4 a4

Copyright Edward B. Magrab 2009 58


An Engineers Guide to MATLAB Chapter 2

meshgrid
If we have two row vectors s and t, then
[U, V] = meshgrid(s, t)
gives the same result as that produced by the two
commands
U = repmat(s, length(t), 1)
V = repmat(t', 1, length(s)) % Notice transpose
In either case, U and V are each matrices of order
(length(t)length(s))

Copyright Edward B. Magrab 2009 59


An Engineers Guide to MATLAB Chapter 2

Example
If
s = [s1 s2 s3 s4] % (14)
t = [t1 t2 t3] % (13)
then
[U, V] = meshgrid(s, t) % Sizes of U and V are (34)
produces the numerical equivalent of two (34) matrices
s1 s2 s3 s4 t1 t1 t1 t1
U = s1 s2 s3 s4 V = t 2 t2 t2 t 2
s1 s2 s3 s4 t 3 t3 t3 t 3

Copyright Edward B. Magrab 2009 60


An Engineers Guide to MATLAB Chapter 2

Example However,
u = [1, 2, 3, 4];
u = [1, 2, 3, 4]; v = [5, 6, 7];
v = [5, 6, 7]; [V, U] = meshgrid(v, u)
[U, V] = meshgrid(u, v) gives
Upon execution, we obtain V=
U= 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
V= U=
5 5 5 5 1 1 1
6 6 6 6 2 2 2
7 7 7 7 3 3 3
4 4 4
Copyright Edward B. Magrab 2009 61
An Engineers Guide to MATLAB Chapter 2

There are two matrix manipulation functions that are


useful in certain applications
fliplr(A)
which flips the columns and
flipud(A)
which flips the rows.
Consider the (25) matrix

a11 a12 a13 a14 a15


A= (2 5)
a21 a22 a23 a24 a25
which is created with the statement
A = [a11 a12 a13 a14 a15; a21 a22 a23 a24 a25]

Copyright Edward B. Magrab 2009 62


An Engineers Guide to MATLAB Chapter 2

Then
a a14 a13 a12 a11
fliplr( A) = 15 (2 5)
a25 a24 a23 a22 a21
a a22 a23 a24 a25
flipud( A) = 21 (2 5)
a11 a12 a13 a14 a15
a a a a a
A = 11 12 13 14 15 (25)
and a21 a22 a23 a24 a25

a a24 a23 a22 a21


flipud(fliplr( A)) 25 (2 5)
a15 a14 a13 a12 a11

These results are obtained with the colon notation. For


example,
C = fliplr(A)
produces the same results as
C = A(:,length(A):-1:1)

Copyright Edward B. Magrab 2009 63


An Engineers Guide to MATLAB Chapter 2

Clarification of Notation
Consider the following two (25) matrices A and B
a a12 a13 a14 a15 b b12 b13 b14 b15
A 11 B 11
a21 a22 a23 a24 a25 b21 b22 b23 b24 b25

Addition or subtraction: C = A B

a11 b11 a12 b12 a13 b13 a14 b14 a15 b15
C (2 5)
a21 b21 a22 b22 a23 b23 a24 b24 a25 b25

Copyright Edward B. Magrab 2009 64


An Engineers Guide to MATLAB Chapter 2

Column augmentation: C = [A, B]

a a12 a13 a14 a15 b11 b12 b13 b14 b15


C 11 (210)
a21 a22 a23 a24 a25 b21 b22 b23 b24 b25

Row augmentation: C = [A; B]

a11 a12 a13 a14 a15


a a22 a23 a24 a25
C 21 (4 5)
b11 b12 b13 b14 b15

b21 b22 b23 b24 b25

Copyright Edward B. Magrab 2009 65


An Engineers Guide to MATLAB Chapter 2

Furthermore, if
x = [x1 x2 x3]
y = [y1 y2 y3]
then either x1 y1
Z = [x', y'] or Z = [x; y]' gives Z x2 y2 (2 3)

x3 y3

x1
x
whereas 2
x3
Z = [x'; y'] yields Z (61)
y1
y2

y3

Copyright Edward B. Magrab 2009 66


An Engineers Guide to MATLAB Chapter 2

We illustrate these results with the


following two vectors: a = [1, 2, 3]
and b = [4, 5, 6]. The script is
Z1 =
x = [1, 2, 3]; 1 4
y = [4, 5, 6]; 2 5
Z1= [x', y'] 3 6
Z2= [x; y]'
Z3= [x'; y'] Z2 =
Z3 = 1 4
1 2 5
2 3 6
3
4
5
6

Copyright Edward B. Magrab 2009 67


An Engineers Guide to MATLAB Chapter 2

Dot Operations
A means of performing, on matrices of the same order,
arithmetic operations on an element-by-element basis.
Consider the following (34) matrices
x11 x12 x13 x14
X x21 x22 x23 x24
x31 x32 x33 x34

m11 m12 m13 m14


M m21 m22 m23 m24
m31 m32 m33 m34

Copyright Edward B. Magrab 2009 68


An Engineers Guide to MATLAB Chapter 2

Then, the dot multiplication of X and M is

x11 * m11 x12 * m12 x13 * m13 x14 * m14


Z m X.* M x21 * m21 x22 * m22 x23 * m23 x24 * m24
x31 * m31 x32 * m32 x33 * m33 x34 * m34

The dot division of X by M is

x11 /m11 x12 /m12 x13 /m13 x14 /m14


Z d X./M x21 /m 21 x22 /m22 x23 /m23 x24 /m24
x31 /m 31 x32 /m32 x33 /m33 x34 /m34

Copyright Edward B. Magrab 2009 69


An Engineers Guide to MATLAB Chapter 2

The dot exponentiation of X and M is

x11 ^ m11 x12 ^ m12 x13 ^ m13 x14 ^ m14


Z e X.^ M x21 ^ m21 x22 ^ m22 x23 ^ m23 x24 ^ m24
x31 ^ m31 x32 ^ m32 x33 ^ m33 x34 ^ m34

Copyright Edward B. Magrab 2009 70


An Engineers Guide to MATLAB Chapter 2

Use of Dot Operations


Let a, b, c, d, and g each be a (32) matrix, and
consider the expression
2
b
d

Z = tana - g
c
Then the MATLAB expression is
Z = (tan(a)-g.*(b./c).^d).^2;

which is equivalent to
(tan(a11 ) - g11 * (b11 /c11 )^ d11 )^ 2 (tan(a12 ) - g12 * (b12 /c12 )^ d12 )^ 2
Z (tan(a21 ) - g21 * (b21 /c21 )^ d 21 )^ 2 (tan(a22 ) - g22 * (b22 /c22 )^ d 22 )^ 2
(tan(a31 ) - g31 * (b31 /c31 )^ d 31 )^ 2 (tan(a32 ) - g32 * (b32 /c32 )^ d 32 )^ 2

Copyright Edward B. Magrab 2009 71


An Engineers Guide to MATLAB Chapter 2

Example
Evaluate
sin(b1t c1 )
v e a1t
t c1
for 6 equally-spaced values of t in the interval 0 t 1
when a1 = 0.2, b1 = 0.9, and c1 = /6. The script is
a1 = 0.2; b1 = 0.9; c1 = pi/6;
t = linspace(0, 1, 6);
v = exp(-a1*t).*sin(b1*t+c1)./(t+c1)
Upon execution, we obtain
v=
0.9549 0.8590 0.7726 0.6900 0.6097 0.5316

Copyright Edward B. Magrab 2009 72


An Engineers Guide to MATLAB Chapter 2

Example
Create the elements of an (NN) matrix H when each
element is given by
1
hmn m, n 1, 2,...N
m n 1
The script to generate this array for N = 4 is
N = 4;
mm = 1:N; nn = mm;
[n, m] = meshgrid(nn, mm)
h = 1./(m+n-1)
Upon execution, we get h=
1.0000 0.5000 0.3333 0.2500
0.5000 0.3333 0.2500 0.2000
0.3333 0.2500 0.2000 0.1667
0.2500 0.2000 0.1667 0.1429
Copyright Edward B. Magrab 2009 73
An Engineers Guide to MATLAB Chapter 2

We return to meshgrid and again consider the two vectors


s = [s1 s2 s3 s4] and t = [t1 t2 t3]. Using the MATLAB
expression
[U, V] = meshgrid(s, t) % U and V are (34) matrices
we obtained the matrices
s1 s2 s3 s4 t1 t1 t1 t1
U = s1 s2 s3 s4 and V = t 2 t2 t2 t 2
s1 s2 s3 s4 t 3 t3 t3 t 3

If we perform the dot multiplication


Z = U.*V
then the execution of the program

Copyright Edward B. Magrab 2009 74


An Engineers Guide to MATLAB Chapter 2

s= [s1, s2, s3, s4];


t = [t1, t2, t3];
[U, V] = meshgrid(s, t) % (34) matrices
Z = U.*V
results in the equivalent matrix

s1 * t1 s2 * t1 s3 * t1 s4 * t1
Z s1 * t 2 s2 * t 2 s3 * t 2 s4 * t 2
s1 * t 3 s2 * t 3 s3 * t 3 s4 * t 3
Thus, we have the product of all combinations of the
elements of vectors s and t.

Copyright Edward B. Magrab 2009 75


An Engineers Guide to MATLAB Chapter 2

Summation and Cumulative Summation Functions


sum: When v = [v1, v2, , vn], then
length ( v )
S sum(v )
k 1
vk

where S a scalar.
If Z is a (34) matrix with elements zij, then
3 3 3
3
S sum ( Z ) zn1 , zn 2 , zn 3 , zn4 (1 4)
n =1 n =1 n =1 n =1
To sum all the elements in an array, we use
3 3 3 3 4 3
S sum (sum ( Z )) zn1 + zn 2 + zn 3 + zn4 zni (11)
n =1 n =1 n =1 n =1 i =1 n =1

Copyright Edward B. Magrab 2009 76


An Engineers Guide to MATLAB Chapter 2

Example
Evaluate
4
z mm
m =1
Thus,
m = 1:4;
z = sum(m.^m)
which, when executed, gives
z=
288

Copyright Edward B. Magrab 2009 77


An Engineers Guide to MATLAB Chapter 2

cumsum: For a vector v composed of n elements vj, cumsum


creates another vector of length n whose elements are
1 2 n

y cumsum (v ) vk , vk , ... v k (1 n)
k =1 k =1 k =1

If W is (mn) matrix composed of elements wjk, then


1 1 1

wk 1 wk 2 ... w kn
k =1 k =1 k =1

2 2


Y cumsum (W ) k =1
wk 1 w k2 ( m n)
k =1
... ...
m m
w wkn

k =1
k1
k =1

Copyright Edward B. Magrab 2009 78


An Engineers Guide to MATLAB Chapter 2

Example
Consider the convergence of the series
N=10
1
S
n =1 n 2
N


Thus,
S = cumsum(1./(1:10).^2)
which, upon execution, gives
S=
1.0000 1.2500 1.3611 1.4236 1.4636 1.4914
1.5118 1.5274 1.5398 1.5498

Copyright Edward B. Magrab 2009 79


An Engineers Guide to MATLAB Chapter 2

Example
We shall evaluate the following series expression for the
hyperbolic secant for N = 305 and for five equally spaced
values of x from 0 x 2 and compare the results to the
exact values.
N
n(-1)( n-1)/2
sech x 4
n =1,3,5 n + 4 x
2 2

Thus,
nn = 1:2:305; % (1153)
xx = linspace(0, 2, 5); % (15)
[x, n] = meshgrid(xx, nn); % (1535)
s = 4*pi*sum(n.*(-1).^((n-1)/2)./
((pi*n).^2+4*x.^2)); % (15)
se = sech(xx); % (15)
compare = [xx ' s' se' (100*(s-se)./se)'] % (52)
Copyright Edward B. Magrab 2009 80
An Engineers Guide to MATLAB Chapter 2

which, upon execution, gives


compare =
0.0000 1.0021 1.0000 0.2080
0.5000 0.8889 0.8868 0.2346
1.0000 0.6501 0.6481 0.3210
1.5000 0.4272 0.4251 0.4894
2.0000 0.2679 0.2658 0.7827
x approx sech(x) % error

Copyright Edward B. Magrab 2009 81


An Engineers Guide to MATLAB Chapter 2

Mathematical Operations with Matrices


Addition and Subtraction
If we have two matrices A and B, each of the order
(mn), then

a11 b11 a12 b12 ... a1n b1n


a b a22 b22
A B 21 21 ( m n)
... ...

am1 bm1 amn bmn

The MATLAB expression for matrix addition or


subtraction is
W = A + B or W = A B

Copyright Edward B. Magrab 2009 82


An Engineers Guide to MATLAB Chapter 2

Multiplication
If we have an (mk) matrix A and a (kn) matrix B, then
c11 c12 c1n
c c22
C AB 21 ( m n)


cm1 cmn
where
k
clp alj b jp
j 1

For the conditions stated, the MATLAB expression for


matrix multiplication is
W = A*B

Copyright Edward B. Magrab 2009 83


An Engineers Guide to MATLAB Chapter 2

Note: The product of two matrices is defined only


when the adjacent integers of their respective orders
are equal; k in this case. In other words, k
clp alj b jp
(mk)(kn) (mn) j 1

where the notation indicates that we have summed the


k terms.

Matrix Transpose
It can be shown that if C = AB, then its transpose is
C ( AB) BA
If A is an identity matrix (A = I) and m = n, then
C IB BI B

Copyright Edward B. Magrab 2009 84


An Engineers Guide to MATLAB Chapter 2

Example
We shall multiply the following two matrices and show
numerically that the transpose can be obtained either
of the two ways indicated previously.
11 12
11 12 13
A B 21 22
21 22 23 31 32
The script is
A = [11, 12, 13; 21, 22, 23];
B = [11, 12; 21, 22; 31, 32];
C = A*B
Ctran1 = C'
Ctran2 = B'*A'

Copyright Edward B. Magrab 2009 85


An Engineers Guide to MATLAB Chapter 2

Upon execution, we obtain


C=
776 812
1406 1472
A = [11, 12, 13; 21, 22, 23];
Ctran1 = B = [11, 12; 21, 22; 31, 32];
776 1406 C = A*B
812 1472 Ctran1 = C'
Ctran2 = Ctran2 = B'*A'
776 1406
812 1472

Copyright Edward B. Magrab 2009 86


An Engineers Guide to MATLAB Chapter 2

Now consider the following series


k
w ( x, y) f j ( x ) g j ( y )
j=1

Suppose that we are interested in the value of w(x, y) over


a range of values for x and y: x = x1, x2, , xm and y = y1,
y2, , yn. Then one can consider
k
w( xi , y j ) f l ( xi ) gl ( y j ) i = 1, 2,..., m j = 1, 2,..., n
l =1

as one element of a matrix W of order (mn) as follows.


Let F be a matrix of order (mk)
f1 ( x1 ) f 2 ( x1 ) ... f k ( x1 )
f (x ) f 2 ( x2 )
F 1 2 (m k )
... ...

f1 ( x m ) ... f k ( xm )
Copyright Edward B. Magrab 2009 87
An Engineers Guide to MATLAB Chapter 2

and G be a matrix of order (kn)


g1 ( y1 ) g1 ( y2 ) ... g1 ( yn )
g (y ) g (y ) ...
G 2 1 2 2 ( k n)
... ...

k 1
g ( y ) ... g k ( y n
)
then
w11 w12 ... w1n
w w22 ...
W FG 21 ( m n)
... ...

wm1 ... wmn

where
k
wij w( xi , y j ) fl ( xi ) gl ( y j )
l 1

Copyright Edward B. Magrab 2009 88


An Engineers Guide to MATLAB Chapter 2

Thus, matrix multiplication performs the summation of the


series at each combination of values for x and y.

We now consider three special cases of this general


matrix multiplication:
1. The product of a row and a column vector
2. The product of a column and a row vector
3. The product of a row vector and a matrix

Copyright Edward B. Magrab 2009 89


An Engineers Guide to MATLAB Chapter 2

Product of a Row Vector and Column Vector


Let a be the row vector c11 c12 c1n
c c
a = [a1 a2 ak] (1k) C AB 21 22


and b be the column vector cm1 cmn
k

b = [b1 b2 bk] (k1) clp alj b jp


j 1

Then the product d = ab is the scalar


(1k)(k1)

b1 l =1 and p = 1
b k k
d ab a1 a2 ... ak a j b j = a j b j (11)
1

... j =1 j =1

bk

Copyright Edward B. Magrab 2009 90


An Engineers Guide to MATLAB Chapter 2

This is called the dot product of two vectors.


The MATLAB notation for this operation is either
d = a*b
or
d = dot(a, b)

Copyright Edward B. Magrab 2009 91


An Engineers Guide to MATLAB Chapter 2

Product of a Column and Row Vector


Let a be an (m1) column vector and b a (1n) row
vector. Then the product H = ba is
a1 a1b1 a1b2 ... a1bn
a a b a2 b2 ...
H ba 2 b1 b2 ... bn 2 1 (m n)
... ... ...

am am b1 ... am bn

The elements of H, which are hij = biaj, are the individual


products of all the combinations of the elements of b and
a.
k
clp alj b jp l = 1, 2, , m
j 1 p = 1, 2, , n

Copyright Edward B. Magrab 2009 92


An Engineers Guide to MATLAB Chapter 2

Example - Polar to Cartesian Coordinates


y
Consider a vector of radial values
r = [r1 r2 rm]
y = rsin r and a vector of angular values
= [ 1 2 n]
then
x = rcos x r1
r
X r * cos( ) 2 cos1 cos2 ... cosn
...

rm
r1cos1 r1cos2 ... r1cosn
r cos r cos
2 1 2 2
... ...

mr cos1 ... rm cos n

Copyright Edward B. Magrab 2009 93


An Engineers Guide to MATLAB Chapter 2

and
r1
r
Y r * sin( ) 2 sin1 sin2 ... sinn
...

rm
r1sin1 r1sin2 ... r1sinn
r sin r sin
2 1 2 2
... ...

mr sin 1 ... rm sin n

A typical MATLAB program would look like


r = [0:0.05:1]'; % (211)
phi = 0:pi/20:2*pi; % (141)
x = r*cos(phi); % (2141)
y = r*sin(phi); % (2141)

Copyright Edward B. Magrab 2009 94


An Engineers Guide to MATLAB Chapter 2

Product of a Row Vector and a Matrix


Let B be an (mn) matrix and a a (1m) row vector. Then,
the product g = aB is
b11 b12 ... b1n
b b22
g aB a1 a2 ... am 21
... ...

bm 1 ... bmn
m m m
a j b j1 a j bj 2
k
... a j jn (1 n)
b clp alj b jp
j =1 j=1 j=1
j 1

l=1
p = 1, 2, , n

Copyright Edward B. Magrab 2009 95


An Engineers Guide to MATLAB Chapter 2

Now consider the series


m
r ( x ) pk hk ( x )
k =1

Suppose that we are interested in the value of r(x) over a


range of values x1, x2, , xn. Then
m
r ( xi ) pk hk ( xi ) i = 1, 2, ..., n
k =1

is one element of a vector r of order (1n) as follows.


Let p be a vector of order (1m) with elements pk and V be
the (mn) matrix
h1 ( x1 ) h1 ( x2 ) ... h1 ( xn )
h (x ) h (x )
V 2 1 2 2 ( m n)
... ...

m 1
h ( x ) hm ( x n
)

Copyright Edward B. Magrab 2009 96


An Engineers Guide to MATLAB Chapter 2

Then, r = pV gives

m m m

r pV pk hk ( x1 ) pk hk ( x2 ) ... p h
k k ( x n (1 n)
)
k =1 k =1 k =1

Copyright Edward B. Magrab 2009 97


An Engineers Guide to MATLAB Chapter 2

Example - Summation of a Fourier Series


The Fourier series representation of a rectangular pulse
of duration d and period T is given by
d
K sin kd/T

f ( ) 1 + 2 cos(2k )
T k =1 kd/T
where = t/T. Let us sum 150 terms of f() (K = 150) and
plot it from 1/2 1/2 when d/T = 0.25.
We see that
r ( xi ) f ( )
sin kd/T m
r ( xi ) pk hk ( xi )
pk i = 1, 2, ..., n
kd/T k =1

hk ( xi ) hk ( i ) cos(2k )

Copyright Edward B. Magrab 2009 98


An Engineers Guide to MATLAB Chapter 2

The program is
k = 1:150; % (1150)
tau = linspace(-0.5, 0.5, 100); % (1100)
sk = sin(pi*k/4)./(pi*k/4); % (1150)
cntau = cos(2*pi*k'*tau); % (150100)
f = 0.25*(1+2*sk*cntau); % (1150) (150100) (1100)
plot(tau, f)

Copyright Edward B. Magrab 2009 99


An Engineers Guide to MATLAB Chapter 2

Example -

N
1 cos(n )
u( , ) 4 e n sin(n )
n
3
n 1

Assume that the length of is Nx and that the length


of is Ne. Then, we note that


1 cos n
n 3

en sin n
(1 N ) (1 Ne )
(1 N ) (1 N x )
(1 N ) [Not permissible] [Not permissible]

Copyright Edward B. Magrab 2009 100


An Engineers Guide to MATLAB Chapter 2

In order to have the matrices compatible for


multiplication, we take the transpose of vector n

1 cos n
n 3


en sin n
( N 1)(1 Ne )
( N 1)(1 N x )
(1 N ) ( N N x ) ( N N e )
From
meshgrid(, f(n)) ( N N x )


1 cos n
n 3
en sin n

( N Ne )
(N Nx )
[dot multiplication]

Copyright Edward B. Magrab 2009 101


An Engineers Guide to MATLAB Chapter 2

Now we take the transpose and perform matrix


multiplication

1 cos n
n 3
en sin n ( N N )
x e
( N Ne )
( N N x ) ( N x N )

From the matrix multiplication of these two


expressions, we are summing over n, which is
what we set out to do.

Copyright Edward B. Magrab 2009 102


An Engineers Guide to MATLAB Chapter 2

n = (1:25)*pi; % (125)
eta = 0:0.025:1; % (141)
xi = 0:0.05:0.7; % (115)
[X1, temp1] = meshgrid(xi, (1-cos(n))./n.^3); % (2515)
temp2 = exp(-n'*xi); % (2515)
Rnx = temp1.*temp2; % (2515)
temp3 = sin(n'*eta); % (2541)
u = 4*Rnx'*temp3; % (1541)
mesh(eta, xi, u)
0.35

0.3

0.25

0.2

0.15

0.1

0.05

0
0.8

0.6 1
0.8
0.4
0.6
0.2 0.4
0.2
0 0

Copyright Edward B. Magrab 2009 103


An Engineers Guide to MATLAB Chapter 2

Determinants
A determinant of an array A of order (nn) is represented
symbolically as
a11 a12 ... a1n
a21 a22 ...
A
... ...
a n1 ... ann

For n = 2:
A a11a22 a12 a21

For n = 3:
A a11a22 a33 + a12 a23 a31 + a13 a21a32 a13 a22 a31
a11a23 a32 a12 a21a33

Copyright Edward B. Magrab 2009 104


An Engineers Guide to MATLAB Chapter 2

The MATLAB expression for the determinant is


det(a)

Example
If A is defined as
1 3
A
4 2
then the determinant of A is obtained from
A = [1, 3; 4, 2];
d = det(A)
which, upon execution, gives
d=
-10
Copyright Edward B. Magrab 2009 105
An Engineers Guide to MATLAB Chapter 2

A class of problems, called eigenvalue problems, results in


a determinant of the form
A B 0
where A and B are (nn) matrices and j, j = 1, 2, , , n are
the roots (called eigenvalues) of this equation.
One form of the MATLAB expression to obtain is
lambda = eig(A, B)

Copyright Edward B. Magrab 2009 106


An Engineers Guide to MATLAB Chapter 2

Example - Eigenvalues of a Spring-Mass System


The eigenvalues of a three-degree-of-freedom spring-
mass system are obtained from the characteristic
equation
K - 2 M 0
where
50 -30 0 3 0 0
K -30 70 -40 N/m and M 0 1.4 0 kg
0 -40 50 0 0 5

and j = j

A B 0

Copyright Edward B. Magrab 2009 107


An Engineers Guide to MATLAB Chapter 2

The program is
K = [50, -30, 0; -30, 70, -40; 0, -40, 50];
M = diag([3, 1.4, 5]);
w = sqrt(eig(K, M))
which, upon execution, gives
w=
1.6734 50 -30 0
3.7772 K -30 70 -40
7.7201 0 -40 50
% rad/s
3 0 0
M 0 1.4 0
0 0 5

Copyright Edward B. Magrab 2009 108


An Engineers Guide to MATLAB Chapter 2

Matrix Inverse
The inverse of a square matrix A is an operation
such that
A-1 A = AA-1 = I
provided that A is not singular, that is, its determinant is
not equal to zero (|A| 0).
The expression for obtaining the inverse of matrix A is
either
inv(A)
or
A^-1
Note: 1/A A^-1; 1/A will cause the system to respond
with an error message. However, 1./A is valid, but it is
not the inverse.
Copyright Edward B. Magrab 2009 109
An Engineers Guide to MATLAB Chapter 2

Example - Inverse of Matrix


We shall determine the inverse of the magic matrix and
the product of the inverse with the original matrix. The
program is
invM = inv(magic(3))
IdentMat = invM*magic(3)
Its execution gives
invM =
0.1472 -0.1444 0.0639
-0.0611 0.0222 0.1056
-0.0194 0.1889 -0.1028
IdentMat =
1.0000 0 -0.0000
0 1.0000 0
0 0.0000 1.0000
Copyright Edward B. Magrab 2009 110
An Engineers Guide to MATLAB Chapter 2

Solution of a System of Equations


Consider the following system of n equations and n
unknowns xk, k = 1, 2, , n
a11 x1 + a12 x2 + ... + a1n xn = b1
a21 x1 + a22 x2 + ... + a2 n xn = b2
...
an1 x1 + an 2 x2 + ... + ann xn = bn
We rewrite this system of equations in matrix notation
as follows:
Ax = b
where A is the following (nn) matrix

Copyright Edward B. Magrab 2009 111


An Engineers Guide to MATLAB Chapter 2

a11 a12 ... a1n


a a22 ...
A 21 ( n n)
... ...

a n1 ... ann
and
x1 b1
x b
x 2 (n 1) and b 2 (n 1)
... ...

xn b n
The symbolic solution is obtained by pre-multiplying both
sides of the matrix equation by A-1. Thus,
A1 Ax A1b
x A1b
since A-1A = I, the identity matrix, and Ix = x.

Copyright Edward B. Magrab 2009 112


An Engineers Guide to MATLAB Chapter 2

The preferred expression for solving this system of


equations is
x = A\b
where the backslash operator indicates matrix division
and is referred to by MATLAB as left matrix divide. An
equivalent way of solving a system of equations is by
using

x = linsolve(A, b)

Copyright Edward B. Magrab 2009 113


An Engineers Guide to MATLAB Chapter 2

Example
Consider the following system of equations
8 x1 + x2 + 6 x3 = 7.5
3 x1 + 5 x2 + 7 x3 = 4
4 x1 + 9 x2 + 2 x3 = 12
which, in matrix notation, is

8 1 6 x1 7.5
3 5 7 x 4
2
4 9 2 x3 12
We shall determine xk and verify the results.

Copyright Edward B. Magrab 2009 114


An Engineers Guide to MATLAB Chapter 2

A = [8, 1, 6; 3, 5, 7; 4, 9, 2];
b = [7.5, 4, 12]'; 8 1 6 x1 7.5
x = A\b 3 5 7 x 4
2
z = A*x 4 9 2 x3 12
which, upon execution, gives
x=
1.2931
0.8972
-0.6236
z=
7.5000
4.0000
12.0000

Copyright Edward B. Magrab 2009 115


An Engineers Guide to MATLAB Chapter 2

Example Temperatures in a Slab


A thin square metal plate has a uniform temperature
of 80 C on two opposite edges and a temperature of
120 C on the third edge and a temperature of 60 C
on the remaining edge.
A mathematical procedure to approximate the
temperature at six uniformly spaced interior points
results in the following equations.
4T1 T2 T6 200
T1 4T2 T3 T5 80
T2 4T3 T4 140
T3 4T4 T5 140
T2 T4 4T5 T6 80
T1 T5 4T6 200

Copyright Edward B. Magrab 2009 116


An Engineers Guide to MATLAB Chapter 2

The temperatures are determined from the following script.


c = [4 -1 0 0 0 -1 4T1 T2 T6 200
-1 4 -1 0 -1 0 T1 4T2 T3 T5 80
0 -1 4 -1 0 0 T2 4T3 T4 140
T3 4T4 T5 140
0 0 -1 4 -1 0
T2 T4 4T5 T6 80
0 -1 0 -1 4 -1 T1 T5 4T6 200
-1 0 0 0 -1 4];
d = [200, 80, 140, 140, 80, 200];
T = linsolve(c, d')
T=
The execution of this script gives 94.2857
82.8571
74.2857
74.2857
82.8571
94.2857
Copyright Edward B. Magrab 2009 117

You might also like