Transcript Slide 1

An Engineer’s Guide to MATLAB
Chapter 2
AN ENGINEER’S GUIDE TO MATLAB
3rd Edition
CHAPTER 2
VECTORS and MATRICES
Copyright © Edward B. Magrab 2009
1
An Engineer’s 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 Engineer’s 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 Engineer’s 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 (mn) and is written as
 a11
a
A =  21
 ...

am1
a12
a22
... a1n 

  (m × n)

...

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 Engineer’s Guide to MATLAB
Chapter 2
Diagonal Matrix: When n = m and aij = 0, i ≠j; thus
a11
0
A= 
 ...

0
0
a22
0 

  (n  n)

...

ann 
...
Identity Matrix: Diagonal matrix with aii = 1; thus
 1 0 ... 0 
0 1


I=
...
... 


0
1


Copyright © Edward B. Magrab 2009
5
An Engineer’s 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 Engineer’s 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
w = a
12
W = A =  21

...

 wn1 = a1n
Recall that
 a11
a
A =  21
 ...

am1
Copyright © Edward B. Magrab 2009
w12 = a21
w22 = a22
a12
a22
... w1 m = am1 

  (n  m )

...

wnm = amn 
... a1n 

  (m × n)

...

amn 
7
An Engineer’s Guide to MATLAB
Chapter 2
For a column vector -
If
 a1 
a 
a   2   (m  1)
 ... 
 
 am 
then
a   a1 a2 ... am   (1  m )
For a row vector If a   a1 a2 ... am   (1  m )
Copyright © Edward B. Magrab 2009
then
 a1 
a 
a    2   (m  1)
 
 
am 
8
An Engineer’s 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)
f = [a, x, b, …]
(Blanks optional)
or
Caution about blanks: If a = h + ds, then f is written as
either
f = [h+d^s x b ...]
or
No blanks permitted
within expression
f = [h+d^s, x, b, ...]
Copyright © Edward B. Magrab 2009
9
An Engineer’s 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 Engineer’s 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]
Copyright © Edward B. Magrab 2009
(Note: s+nd  f )
11
An Engineer’s 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 Engineer’s 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
x=
n=
0.2000
9
0.3000
0.4000
To create a column vector, we use
0.5000
x = (0.2:0.1:1)
n = length(x)
Copyright © Edward B. Magrab 2009

0.6000
0.7000
0.8000
0.9000
1.0000
n=
9
13
An Engineer’s 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+(n1)d]
Copyright © Edward B. Magrab 2009
14
An Engineer’s 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
x=
[10s
Copyright © Edward B. Magrab 2009
10s+d
10s+2d
…
10f]
d=
f s
n 1
15
An Engineer’s 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 Engineer’s 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 Engineer’s 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 Engineer’s 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
Copyright © Edward B. Magrab 2009
% or b(end)
19
An Engineer’s 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 Engineer’s 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 Engineer’s 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 Engineer’s 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
Copyright © Edward B. Magrab 2009
-7
31
23
An Engineer’s 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 Engineer’s 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 Engineer’s 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 Engineer’s 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 Engineer’s Guide to MATLAB
Chapter 2
One of the great advantages of MATLAB’s 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.8660 0.9848 0.6428 0.0000
Copyright © Edward B. Magrab 2009
0.3420
28
An Engineer’s 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 Engineer’s 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
ymin =
-0.9848
kmin =
3
Copyright © Edward B. Magrab 2009
y=
-0.0000 -0.6428 -0.9848 -0.8660 -0.3420
0.3420 0.8660 0.9848 0.6428 0.0000
30
An Engineer’s 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 Engineer’s 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 =
-0.6237
tMinValuef =
3.0775
1
0.5
0
-0.5
-1
0
Copyright © Edward B. Magrab 2009
1
2
3
4
5
6
32
An Engineer’s 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
a21 a22
a31 a32
a41 a42
a13; ...
a23; ...
a33; ...
a43]
where the ellipses (...) are required to indicate that the
expression continues on the next line.
Copyright © Edward B. Magrab 2009
33
An Engineer’s 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
a21 a22
a31 a32
a41 a42
Copyright © Edward B. Magrab 2009
a13 %<Enter>
a23 %<Enter>
a33 %<Enter>
a43]
34
An Engineer’s 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 Engineer’s 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
5.0000 + 6.0000i
W=
1.0000 - 2.0000i
3.0000 - 4.0000i
Copyright © Edward B. Magrab 2009
3.0000 + 4.0000i
7.0000 + 9.0000i
5.0000 - 6.0000i
7.0000 - 9.0000i
36
An Engineer’s Guide to MATLAB
Chapter 2
Special Matrices
A matrix of all 1’s
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
Copyright © Edward B. Magrab 2009
zer = zeros(3, 2)
zer =
0 0
0 0
0 0
37
An Engineer’s Guide to MATLAB
Chapter 2
Diagonal matrix –
Create an (nn) diagonal matrix
whose diagonal elements are a
vector a of length n
diag(a)
a = [4, 9, 1];
A = diag(a)
A=
4 0 0
0 9 0
0 0 1
Extract the diagonal elements of
a square matrix A
Ad = diag([11, 12, 13, 14; …
21, 22, 23, 24; …
31, 32, 33, 34; …
41, 42, 43, 44])
diag(A)
Ad =
11
22
33
44
Copyright © Edward B. Magrab 2009
38
An Engineer’s Guide to MATLAB
Chapter 2
Identity matrix whose size is (nn)
eye(n)
Copyright © Edward B. Magrab 2009
A = eye(3)
A=
1 0 0
0 1 0
0 0 1
39
An Engineer’s Guide to MATLAB
Chapter 2
Accessing Matrix Elements
Matrix created with –
A(1,1)
A = [3:2:11; …
linspace(20, 21, 5); …
ones(1, 5)];
A(1:3,3:5)
5
7
9
11 
 3
A   20.0 20.25 20.5 20.75 21.0 


1
1
1
1 
 1
A(:,2)
: means all
elements of
column 2
Copyright © Edward B. Magrab 2009
A(3,4)
A(2,:)
: means all
elements of
row 2
B = A(1:3,3:5)
B=
7.0000 9.0000 11.0000
20.5000 20.7500 21.0000
1.0000 1.0000 1.0000
40
An Engineer’s Guide to MATLAB
Chapter 2
In several of the following examples, we shall use
magic(n)
which creates an (nn) 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
5
9
4
2
11
7
14
3 13
10 8
6 12
15 1
Copyright © Edward B. Magrab 2009
41
An Engineer’s 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
Z = Z-diag(diag(Z))
11
0
11 0
6
0
0 6
which results in
1
0
0 0
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
=
0
0
0
1
42
An Engineer’s 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
Z=
5
5
9
4
Copyright © Edward B. Magrab 2009
2 3 13
5 10 8
7 5 12
14 15 5
magic(4) =
16 2 3
5 11 10
9 7 6
4 14 15
13
8
12
1
43
An Engineer’s 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 Engineer’s Guide to MATLAB
Chapter 2
Upon execution, we obtain
m=
8 1
3 5
4 9
subscr =
1 1
3 2
1 3
2 3
Copyright © Edward B. Magrab 2009
6
7
2
45
An Engineer’s Guide to MATLAB
Chapter 2
Example –
M = magic(4)
minM = min(M)
maxM = max(M)
which, upon execution, gives
M=
16 2
5 11
9 7
4 14
minM =
4 2
maxM =
16 14
Copyright © Edward B. Magrab 2009
Minimum and Maximum
Values in a Matrix
min and max operate on a
column by column basis.
3 13
10 8
6 12
15 1
To find the maximum of all
elements we use max twice:
3
which, upon execution, gives
15
1
13
M = magic(4);
maxM = max(max(M))
maxM =
16
46
An Engineer’s Guide to MATLAB
Chapter 2
Example - Creation of a special matrix
We shall create the following (9×9) array
0
1

1

0
A  4

4
0

7

7
1 1 0 2 2 0 3 3
0 1 2 0 2 3 0 3 
1 0 2 2 0 3 3 0

4 4 0 5 5 0 6 6
0 4 5 0 5 6 0 6

4 0 5 5 0 6 6 0
7 7 0 8 8 0 9 9

0 7 8 0 8 9 0 9

7 0 8 8 0 9 9 0
(Dashed lines have been added to enhance visual clarity.)
Copyright © Edward B. Magrab 2009
47
An Engineer’s 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
4
4
0
7
7
1
0
1
4
0
4
7
0
7
Copyright © Edward B. Magrab 2009
1
1
0
4
4
0
7
7
0
0
2
2
0
5
5
0
8
8
2
0
2
5
0
5
8
0
8
2
2
0
5
5
0
8
8
0
0
3
3
0
6
6
0
9
9
3
0
3
6
0
6
9
0
9
3
3
0
6
6
0
9
9
0
48
An Engineer’s Guide to MATLAB
Chapter 2
Example - Rearrangement of Sub Matrices of a Matrix
Consider the following (9×9) array
1
1
10

19

 28
A  37

 46
 55

 64

 73
2
2
3
4
5
6
7
8
11 12 13 14 15 16 17
20 21 22 23 24 25 26
29 30 31 32 33 34 35
38 39 40 41 42 43 44
47 48 49 50 51 52 53
56 57 58 59 60 61 62
65 66 67 68 69 70 71
74 75 76 77 78 79 80
4
9
18 
27 

36 
45 

54 
63 

72 

81 
3
(Dashed lines have been added to enhance visual clarity.)
Copyright © Edward B. Magrab 2009
49
An Engineer’s Guide to MATLAB
Chapter 2
For the four (33) 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
 61
 70

 79

 28
A  37

 46
7

16

 25
62 63
4
4
5
6
71 72 13 14 15
80 81 22 23 24
29 30 31 32 33
38 39 40 41 42
47 48 49 50 51
8 9 58 59 60
17 18 67 68 69
26 27 76 77 78
2
55 56 57 
64 65 66 
73 74 75 

34 35 36 
43 44 45

52 53 54 
1 2 3

10 11 12 

19 20 21
1
1
1
10

19

 28
A  37

 46
 55

 64

 73
2
3
4
5
6
7
8
11 12 13 14 15 16 17
20 21 22 23 24 25 26
29 30 31 32 33 34 35
38 39 40 41 42 43 44
47 48 49 50 51 52 53
56 57 58 59 60 61 62
65 66 67 68 69 70 71
74 75 76 77 78 79 80
4
Copyright © Edward B. Magrab 2009
2
9
18 
27 

36 
45 

54 
63 

72 

81 
3
50
An Engineer’s 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 Engineer’s Guide to MATLAB
Chapter 2
Upon execution, we obtain
Matr =
61
70
79
28
37
46
7
16
25
62
71
80
29
38
47
8
17
26
63
72
81
30
39
48
9
18
27
4
13
22
31
40
49
58
67
76
5
14
23
32
41
50
59
68
77
6
15
24
33
42
51
60
69
78
55
64
73
34
43
52
1
10
19
56
65
74
35
44
53
2
11
20
57
66
75
36
45
54
3
12
21
1
1
10

19

 28
A  37

 46
 55

 64

 73
2
3
4
5
6
7
9
18 
27 

36 
45 

54 
63 

72 

81 
8
11 12 13 14 15 16 17
20 21 22 23 24 25 26
29 30 31 32 33 34 35
38 39 40 41 42 43 44
47 48 49 50 51 52 53
56 57 58 59 60 61 62
65 66 67 68 69 70 71
74 75 76 77 78 79 80
4
Copyright © Edward B. Magrab 2009
2
3
52
An Engineer’s 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 Engineer’s 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 Engineer’s 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

V =  a1
 a1
Copyright © Edward B. Magrab 2009
a2
a2
a2
a3
a3
a3
a4 

a4 
a4 
Replicated
row
55
An Engineer’s Guide to MATLAB
Chapter 2
whereas
repmat(s, 3, 2)
creates the numerical equivalent of the matrix
 a1
V =  a1
 a1
a2
a2
a2
a3
a3
a3
a4
a4
a4
a1
a1
a1
a2
a2
a2
a3
a3
a3
a4 
a4 
a4 
Replicated
1st row
Replicated
‘column’
Copyright © Edward B. Magrab 2009
56
An Engineer’s Guide to MATLAB
Chapter 2
On the other hand
V = repmat(s', 1, 3)
gives the numerical equivalent of the matrix
 a1
a
V = 2
 a3

 a4
a1
a2
a3
a4
a1 
a2 
a3 

a4 
Replicated
1st column
Copyright © Edward B. Magrab 2009
57
An Engineer’s Guide to MATLAB
Chapter 2
and
V = repmat(s', 2, 3)
gives the numerical equivalent of the matrix
 a1
a
 2
 a3

a4

V=
 a1

 a2
a
 3
 a4
Copyright © Edward B. Magrab 2009
a1
a2
a3
a4
a1
a2
a3
a4
a1 
a2 
a3 

a4 
a1 

a2 
a3 

a4 
58
An Engineer’s 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 Engineer’s Guide to MATLAB
Chapter 2
Example –
If
s = [s1 s2 s3 s4] % (1×4)
t = [t1 t2 t3]
% (1×3)
then
[U, V] = meshgrid(s, t) % Sizes of U and V are (3×4)
produces the numerical equivalent of two (34) matrices
 s1
U =  s1
 s1
Copyright © Edward B. Magrab 2009
s2
s2
s2
s3
s3
s3
s4 
s4 
s4 
 t1
V =  t 2
 t 3
t1
t2
t3
t1
t2
t3
t1 
t 2 
t 3 
60
An Engineer’s Guide to MATLAB
Example –
u = [1, 2, 3, 4];
v = [5, 6, 7];
[U, V] = meshgrid(u, v)
Upon execution, we obtain
U=
1
1
1
V=
5
6
7
2
2
2
3
3
3
4
4
4
5
6
7
5
6
7
5
6
7
Copyright © Edward B. Magrab 2009
Chapter 2
However,
u = [1, 2, 3, 4];
v = [5, 6, 7];
[V, U] = meshgrid(v, u)
gives
V=
5
5
5
5
U=
1
2
3
4
6
6
6
6
7
7
7
7
1
2
3
4
1
2
3
4
61
An Engineer’s 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 (25) matrix
 a11
A=
a21
a12
a22
a13
a23
a14
a24
a15 
 (2× 5)

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 Engineer’s Guide to MATLAB
Chapter 2
Then
a
fliplr( A) =  15
a25
a
flipud( A) =  21
 a11
a14
a24
a13
a23
a12
a22
a11 
 (2× 5)

a21 
a22
a12
a23
a13
a24
a14
a25 
 (2× 5)

a15 
a
a
a
a 
a
A =  11 12 13 14 15   (2× 5)
a21 a22 a23 a24 a25 
and
a
flipud (fliplr( A))   25
 a15
a24
a14
a23
a13
a22
a12
a21 
 (2× 5)

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 Engineer’s Guide to MATLAB
Chapter 2
Clarification of Notation –
Consider the following two (25) matrices A and B
 a11
A
a21
a12
a22
a13
a23
a14
a24
a15 
a25 
 b11
B
b21
b12
b22
b13
b23
b14
b24
b15 
b25 
Addition or subtraction: C = A  B
 a11 ± b11
C
a21 ± b21
Copyright © Edward B. Magrab 2009
a12 ± b12
a22 ± b22
a13 ± b13
a23 ± b23
a14 ± b14
a24 ± b24
a15 ± b15 
 (2× 5)

a25 ± b25 
64
An Engineer’s Guide to MATLAB
Chapter 2
Column augmentation: C = [A, B]
 a11
C
a21
a12
a22
a13
a23
a14
a24
a15
a25
b11
b21
b12
b22
b13
b23
b14
b24
b15 
 (2×10)

b25 
Row augmentation: C = [A; B]
 a11
a
C   21
 b11

 b21
Copyright © Edward B. Magrab 2009
a12
a13
a14
a22
a23
a24
b12
b13
b14
b22
b23
b24
a15 
a25 
 (4× 5)
b15 

b25 
65
An Engineer’s Guide to MATLAB
Chapter 2
Furthermore, if
x = [x1 x2 x3]
y = [y1 y2 y3]
then either
Z = [x', y'] or Z = [x; y]'
whereas
Z = [x'; y'] yields 
Copyright © Edward B. Magrab 2009
 x1
gives  Z   x2

 x3
y1 
y2   (2× 3)
y3 
 x1 
x 
 2
 x3 
Z     (6 ×1)
 y1 
 y2 
 
 y3 
66
An Engineer’s 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
x = [1, 2, 3];
y = [4, 5, 6];
Z1= [x', y']
Z2= [x; y]'
Z3= [x'; y']
Z3 =
1
2
3
4
5
6
Copyright © Edward B. Magrab 2009
Z2 =
1
2
3
Z1 =
1
2
3
4
5
6
4
5
6
67
An Engineer’s 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 (34) matrices
 x11
X   x21
 x31
x12
x22
x32
x13
x23
x33
x14 
x24 
x34 
 m11
M   m 21
 m 31
m12
m 22
m 32
m13
m 23
m 33
m14 
m 24 
m 34 
Copyright © Edward B. Magrab 2009
68
An Engineer’s Guide to MATLAB
Chapter 2
Then, the dot multiplication of X and M is
 x11 * m11
Z m  X.* M   x21 * m21
 x31 * m31
x12 * m12
x22 * m22
x32 * m32
x13 * m13
x23 * m23
x33 * m33
x14 * m14 
x24 * m24 
x34 * m34 
x13 /m13
x23 /m 23
x33 /m 33
x14 /m14 
x24 /m 24 
x34 /m 34 
The dot division of X by M is
 x11 /m11
Z d  X./M   x21 /m 21
 x31 /m 31
Copyright © Edward B. Magrab 2009
x12 /m12
x22 /m 22
x32 /m 32
69
An Engineer’s Guide to MATLAB
Chapter 2
The dot exponentiation of X and M is
 x11 ^ m11
Z e  X.^ M   x21 ^ m21
 x31 ^ m31
Copyright © Edward B. Magrab 2009
x12 ^ m12
x22 ^ m22
x32 ^ m32
x13 ^ m13
x23 ^ m 23
x33 ^ m33
x14 ^ m14 
x24 ^ m 24 
x34 ^ m34 
70
An Engineer’s Guide to MATLAB
Chapter 2
Use of Dot Operations
Let a, b, c, d, and g each be a (32) matrix, and
consider the expression

b
Z = tana - g  
c

d



2
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 Engineer’s Guide to MATLAB
Chapter 2
Example –
Evaluate
v  e  a1t
sin(b1t  c1 )
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
Copyright © Edward B. Magrab 2009
0.8590
0.7726
0.6900
0.6097
0.5316
72
An Engineer’s Guide to MATLAB
Chapter 2
Example –
Create the elements of an (N×N) 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
Copyright © Edward B. Magrab 2009
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
73
An Engineer’s 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 (3×4) matrices
we obtained the matrices
 s1
U =  s1
 s1
s2
s2
s2
s3
s3
s3
s4 
s4 
s4 
and
 t1
V =  t 2
 t 3
t1
t2
t3
t1
t2
t3
t1 
t 2 
t 3 
If we perform the dot multiplication
Z = U.*V
then the execution of the program
Copyright © Edward B. Magrab 2009
74
An Engineer’s Guide to MATLAB
Chapter 2
s= [s1, s2, s3, s4];
t = [t1, t2, t3];
[U, V] = meshgrid(s, t) % (3×4) matrices
Z = U.*V
results in the equivalent matrix
 s1 * t1
Z   s1 * t 2
 s1 * t 3
s 2 * t1
s2 * t 2
s2 * t 3
s 3 * t1
s3 * t 2
s3 * t 3
s4 * t 1 
s4 * t 2 
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 Engineer’s Guide to MATLAB
Chapter 2
Summation and Cumulative Summation Functions
sum: When v = [v1, v2, …, vn], then
S  sum (v ) 
length ( v )

vk
k 1
where S a scalar.
If Z is a (34) matrix with elements zij, then
 3
S  sum ( Z )    zn1 ,
 n =1
3
3
 zn 2 ,
 zn 3 ,
n =1
n =1

z

n 4   (1× 4)
n =1

3
To sum all the elements in an array, we use
3
3
3
3
n =1
n =1
n =1
n =1
4
3
S  sum(sum( Z ))   zn1 +  zn2 +  zn3 +  zn4   zni  (1×1)
Copyright © Edward B. Magrab 2009
i =1 n =1
76
An Engineer’s 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 Engineer’s 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
y  cumsum (v )    vk ,
 k =1

vk , ...  vk   (1× n)

k =1
k =1

2
n
If W is (mn) matrix composed of elements wjk, then
 1
  wk 1
 k =1
 2
wk 1


Y  cumsum (W )   k =1
 ...
m
 w
k1
 
k =1
Copyright © Edward B. Magrab 2009
1
 wk 2
k =1
2
w
k =1
k2

w

kn 
k =1


  ( m × n)


...

m
wkn 


k =1
1
...
78
An Engineer’s Guide to MATLAB
Chapter 2
Example –
Consider the convergence of the series
S
N =10

n =1

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 Engineer’s 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 
2
2
n =1,3,5  nπ  + 4 x
Thus,
nn = 1:2:305;
xx = linspace(0, 2, 5);
[x, n] = meshgrid(xx, nn);
s = 4*pi*sum(n.*(-1).^((n-1)/2)./…
((pi*n).^2+4*x.^2));
se = sech(xx);
compare = [xx ' s' se' (100*(s-se)./se)']
Copyright © Edward B. Magrab 2009
% (1153)
% (15)
% (1535)
% (15)
% (15)
% (52)
80
An Engineer’s Guide to MATLAB
Chapter 2
which, upon execution, gives
compare =
0.0000 1.0021
0.5000 0.8889
1.0000 0.6501
1.5000 0.4272
2.0000 0.2679
x
approx
Copyright © Edward B. Magrab 2009
1.0000
0.8868
0.6481
0.4251
0.2658
sech(x)
0.2080
0.2346
0.3210
0.4894
0.7827
% error
81
An Engineer’s Guide to MATLAB
Chapter 2
Mathematical Operations with Matrices
Addition and Subtraction
If we have two matrices A and B, each of the order
(mn), then
 a11 ± b11
a ±b
A ± B   21 21
 ...

am1 ± bm1
a12 ± b12
a22 ± b22
a1n ± b1n 

  ( m × n)

...

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 Engineer’s Guide to MATLAB
Chapter 2
Multiplication
If we have an (mk) matrix A and a (kn) matrix B, then
 c11 c12
c
c22
21

C  AB 


cm1
c1n 

  ( m  n)


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 Engineer’s 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
(mk)(kn)  (mn)
clp   alj b jp
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)  BA
If A is an identity matrix (A = I) and m = n, then
C  IB  BI  B
Copyright © Edward B. Magrab 2009
84
An Engineer’s 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 13 
A

 21 22 23
11 12 
B   21 22 
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 Engineer’s Guide to MATLAB
Chapter 2
Upon execution, we obtain
C=
776
1406
Ctran1 =
776
812
Ctran2 =
776
812
Copyright © Edward B. Magrab 2009
812
1472
1406
1472
A = [11, 12, 13; 21, 22, 23];
B = [11, 12; 21, 22; 31, 32];
C = A*B
Ctran1 = C'
Ctran2 = B'*A'
1406
1472
86
An Engineer’s 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 )   fl ( xi ) gl ( y j ) i = 1, 2,..., m j = 1, 2,..., n
l =1
as one element of a matrix W of order (mn) as follows.
Let F be a matrix of order (mk)
 f1 ( x1 )
 f (x )
F  1 2
 ...

 f1 ( x m )
Copyright © Edward B. Magrab 2009
f 2 ( x1 ) ...
f 2 ( x2 )
...
...
f k ( x1 ) 

  (m  k )


f k ( xm ) 
87
An Engineer’s Guide to MATLAB
Chapter 2
and G be a matrix of order (kn)
 g1 ( y1 ) g1 ( y2 ) ... g1 ( yn ) 
g ( y ) g ( y )

...
2
1
2
2
  (k  n)
G
 ...

...


g
(
y
)
...
g
(
y
)
k
n 
 k 1
then
 w11
w
W  FG   21
 ...

 wm1
w12
w22
...
... w1n 
... 
 ( m  n)

...

wmn 
where
k
wij  w( xi , y j )   fl ( xi ) gl ( y j )
l 1
Copyright © Edward B. Magrab 2009
88
An Engineer’s 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 Engineer’s Guide to MATLAB
Chapter 2
Product of a Row Vector and Column Vector
Let a be the row vector
a = [a1 a2 … ak]  (1k)
and b be the column vector
b = [b1 b2 … bk]  (k1)
Then the product d = ab is the scalar
d  ab   a1
Copyright © Edward B. Magrab 2009
a2
 c11
c
C  AB   21


cm1
c12
c22
c1n 




cmn 
k
clp   alj b jp
j 1
(1k)(k1)
l =1 and p = 1
 b1 
b   k
k

1
... ak       a j b j  =  a j b j  (1×1)
 ...   j =1
 j =1
 
bk 
90
An Engineer’s 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 Engineer’s Guide to MATLAB
Chapter 2
Product of a Column and Row Vector
Let a be an (m1) column vector and b a (1n) row
vector. Then the product H = ba is
 a1 
a 
H  ba   2  b1
 ... 
 
 am 
b2
 a1b1
a b
... bn    2 1
 ...

am b1
a1b2
a2 b2
a1bn 
... 
 (m  n)

...

... 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
j 1
Copyright © Edward B. Magrab 2009
l = 1, 2, …, m
p = 1, 2, …, n
92
An Engineer’s Guide to MATLAB
Chapter 2
Example - Polar to Cartesian Coordinates
y
y = rsin
Consider a vector of radial values
r = [r1 r2 … rm]
and a vector of angular values
 = [  1  2 …  n]
then
r

x = rcos
 r1 
r 
X  r  * cos(θ )   2  cosθ1
 ... 
 
 rm 
x
 r1cosθ1
 r cosθ
1
 2
 ...

 rm cosθ1
Copyright © Edward B. Magrab 2009
r1cosθ2
r2 cosθ2
cosθ2
... cosθn 
r1cosθn 



...

... rm cosθn 
...
93
An Engineer’s Guide to MATLAB
Chapter 2
and
 r1 
r 
Y  r  * sin(θ )   2  sinθ1 sinθ2 ... sinθn 
 ... 
 
 rm 
 r1sinθ1 r1sinθ2 ... r1sinθn 
 r sinθ r sinθ

2
1
2
2


 ...

...


r
sin
θ
...
r
sin
θ
1
m
n
m
A typical MATLAB program would look like
r = [0:0.05:1]';
phi = 0:pi/20:2*pi;
x = r*cos(phi);
y = r*sin(phi);
Copyright © Edward B. Magrab 2009
% (211)
% (141)
% (2141)
% (2141)
94
An Engineer’s Guide to MATLAB
Chapter 2
Product of a Row Vector and a Matrix
Let B be an (mn) matrix and a a (1m) row vector. Then,
the product g = aB is
g  aB  a1
m
  a j b j1
 j =1
a2
 b11
b
... am   21
 ...

bm1
m
 a j bj 2
j=1
... b1n 



...

... bmn 

a
b

j jn   (1  n)
j=1

m
...
b12
b22
k
clp   alj b jp
j 1
l=1
p = 1, 2, …, n
Copyright © Edward B. Magrab 2009
95
An Engineer’s 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 (1n) as follows.
Let p be a vector of order (1m) with elements pk and V be
the (mn) matrix
 h1 ( x1 ) h1 ( x2 ) ... h1 ( xn ) 
h (x ) h (x )

2
1
2
2
  (m  n)
V 
 ...

...


h
(
x
)
h
(
x
)
m
n 
 m 1
Copyright © Edward B. Magrab 2009
96
An Engineer’s Guide to MATLAB
Chapter 2
Then, r = pV gives
m
r  pV    pk hk ( x1 )
 k =1
Copyright © Edward B. Magrab 2009
m
 pk hk ( x2 ) ...
k =1

p
h
(
x
)

k k
n   (1  n)
k =1

m
97
An Engineer’s 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
f ( ) 
T
K  sin kπd/T




cos(2πk )
1 + 2 
 kπd/T 
k =1


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 (  )
pk 
sin  kπd/T 
 kπd/T 
m
r ( xi )   pk hk ( xi ) i = 1, 2,..., n
k =1
hk ( xi )  hk (τ i )  cos(2πk  )
Copyright © Edward B. Magrab 2009
98
An Engineer’s Guide to MATLAB
Chapter 2
The program is
k = 1:150;
% (1150)
tau = linspace(-0.5, 0.5, 100); % (1100)
sk = sin(pi*k/4)./(pi*k/4);
% (1150)
cntau = cos(2*pi*k'*tau);
% (150100)
f = 0.25*(1+2*sk*cntau); % (1150) (150100)  (1100)
plot(tau, f)
Copyright © Edward B. Magrab 2009
99
An Engineer’s Guide to MATLAB
Chapter 2
Example N 
1  cos( n )
n 1
 n 
u( , )  4 
3
e  n sin( n )
Assume that the length of  is Nx and that the length
of  is Ne. Then, we note that

 1  cos n
 n 3
  
(1 N )
Copyright © Edward B. Magrab 2009

 



en

(1 N ) (1 N x )
[Not permissible]

 sin n 
(1 N ) (1 Ne )
[Not permissible]
100
An Engineer’s 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
  
(1 N )
From

meshgrid(, f(n)) ( N  N x )

 




en 


( N 1)(1 N x )
( N  N x )

 1  cos n
 n 3
  
 sin n 
( N 1)(1 Ne )
( N  N e )

 en   sin n 


( N  Ne )
(N Nx )
[dot multiplication]
Copyright © Edward B. Magrab 2009


101
An Engineer’s Guide to MATLAB
Chapter 2
Now we take the transpose and perform matrix
multiplication

 1  cos n
 n 3
  

 en   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 Engineer’s Guide to MATLAB
Chapter 2
n = (1:25)*pi;
eta = 0:0.025:1;
xi = 0:0.05:0.7;
[X1, temp1] = meshgrid(xi, (1-cos(n))./n.^3);
temp2 = exp(-n'*xi);
Rnx = temp1.*temp2;
temp3 = sin(n'*eta);
u = 4*Rnx'*temp3;
mesh(eta, xi, u)
% (125)
% (141)
% (115)
% (2515)
% (2515)
% (2515)
% (2541)
% (1541)
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
0.8
1
0.6
0.8
0.4
0.6
0.4
0.2
0.2
0
Copyright © Edward B. Magrab 2009
0
103
An Engineer’s Guide to MATLAB
Chapter 2
Determinants
A determinant of an array A of order (nn) is represented
symbolically as
A
a11
a12
... a1n
a21
a22
...
...
a n1
...
...
ann
For n = 2:
A  a11a22  a12 a21
For n = 3:
A  a11a22 a33 + a12 a23 a31 + a13 a21a32  a13a 22a 31
 a11a23 a32  a12 a21a33
Copyright © Edward B. Magrab 2009
104
An Engineer’s 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 Engineer’s 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 (nn) 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 Engineer’s Guide to MATLAB
Chapter 2
Example - Eigenvalues of a Spring-Mass System
The eigenvalues of a three-degree-of-freedom springmass system are obtained from the characteristic
equation
K - ω2 M  0
where
 50 -30 0 
K  -30 70 -40  N/m
 0 -40 50 
and ωj =
and
3 0 0
M   0 1.4 0  kg
 0 0 5 
λj
A  λB  0
Copyright © Edward B. Magrab 2009
107
An Engineer’s 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
3.7772
7.7201
% rad/s
 50 -30 0 
K  -30 70 -40 
 0 -40 50 
3 0 0
M   0 1.4 0 
 0 0 5 
Copyright © Edward B. Magrab 2009
108
An Engineer’s 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 Engineer’s 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 Engineer’s 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 (nn) matrix
Copyright © Edward B. Magrab 2009
111
An Engineer’s Guide to MATLAB
 a11
a
A   21
 ...

 a n1
and
a12
a22
...
 x1 
x 
x   2   (n  1)
 ... 
 
 xn 
Chapter 2
... a1n 
... 
 ( n  n)

...

ann 
 b1 
b 
and b   2   (n  1)
 ... 
 
b n 
The symbolic solution is obtained by pre-multiplying both
sides of the matrix equation by A-1. Thus,
A1 Ax  A1b
x  A1b
since A-1A = I, the identity matrix, and Ix = x.
Copyright © Edward B. Magrab 2009
112
An Engineer’s 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 Engineer’s 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 Engineer’s Guide to MATLAB
A = [8, 1, 6; 3, 5, 7; 4, 9, 2];
b = [7.5, 4, 12]';
x = A\b
z = A*x
Chapter 2
 8 1 6   x1  7.5 
3 5 7   x    4 

 2  
 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 Engineer’s 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 Engineer’s Guide to MATLAB
Chapter 2
The temperatures are determined from the following script.
c = [4 -1 0 0 0 -1
-1 4 -1 0 -1 0
0 -1 4 -1 0 0
0 0 -1 4 -1 0
0 -1 0 -1 4 -1
-1 0 0 0 -1 4];
d = [200, 80, 140, 140, 80, 200];
T = linsolve(c, d')
The execution of this script gives 
Copyright © Edward B. Magrab 2009
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
T=
94.2857
82.8571
74.2857
74.2857
82.8571
94.2857
117